如何在控制台打印一个标准圆window?
How to print a standard circle in the console window?
我正在尝试通过回调函数打印任意数学函数,execute() 函数将遍历所有整数坐标 (x,y),如果回调 returns 为真,则 canvas[x][y] = '*'.
但是我的实现只能打印直线,打印空心圆总是失败
width = 80
height = 30
canvas = [[' '] * width for i in range(height)]
def PrintCanvas():
for i in range(len(canvas)):
for j in range(len(canvas[0])):
print(canvas[i][j], end='')
print()
def execute(callback):
for i in range(len(canvas)):
for j in range(len(canvas[0])):
if callback(i, j):
canvas[i][j] = '*'
# The ratio of the height and width of the grid in the console
# If you don't set x=x*rate, the resulting graph will be flat
zoom = 819/386
# Since x and y are both integers, when x is multiplied by a floating-point number, y!=f(x)
# So we use y-f(x)<=deviation here
deviation = 0.5
# print x size 20
def fun_x(x, y):
r = 20
x *= zoom
return x < r and abs(y-x) <= deviation or abs(y-(-x+r-1)) <= deviation
# How to print a standard circle in the console window?
# print circle size 13
def fun_circle(x, y):
r = 13
x *= zoom
a = (pow(x-r, 2)+pow(y-r, 2)) - r*r
r = r - 2
b = (pow(x-r, 2)+pow(y-r, 2)) - r*r
return a <= 0 and b >= 0
# execute(fun_x)
execute(fun_circle)
PrintCanvas()
下面两张图分别是打印出来的x形和错误的环形
你这里也转移了中心
r = r - 2
b = (pow(x-r, 2)+pow(y-r, 2)) - r*r
考虑不改变中心如下
r2 = r - 2
b = (pow(x-r, 2)+pow(y-r, 2)) - r2*r2
附带说明一下,现在您也知道如何画新月了。
我正在尝试通过回调函数打印任意数学函数,execute() 函数将遍历所有整数坐标 (x,y),如果回调 returns 为真,则 canvas[x][y] = '*'.
但是我的实现只能打印直线,打印空心圆总是失败
width = 80
height = 30
canvas = [[' '] * width for i in range(height)]
def PrintCanvas():
for i in range(len(canvas)):
for j in range(len(canvas[0])):
print(canvas[i][j], end='')
print()
def execute(callback):
for i in range(len(canvas)):
for j in range(len(canvas[0])):
if callback(i, j):
canvas[i][j] = '*'
# The ratio of the height and width of the grid in the console
# If you don't set x=x*rate, the resulting graph will be flat
zoom = 819/386
# Since x and y are both integers, when x is multiplied by a floating-point number, y!=f(x)
# So we use y-f(x)<=deviation here
deviation = 0.5
# print x size 20
def fun_x(x, y):
r = 20
x *= zoom
return x < r and abs(y-x) <= deviation or abs(y-(-x+r-1)) <= deviation
# How to print a standard circle in the console window?
# print circle size 13
def fun_circle(x, y):
r = 13
x *= zoom
a = (pow(x-r, 2)+pow(y-r, 2)) - r*r
r = r - 2
b = (pow(x-r, 2)+pow(y-r, 2)) - r*r
return a <= 0 and b >= 0
# execute(fun_x)
execute(fun_circle)
PrintCanvas()
下面两张图分别是打印出来的x形和错误的环形
你这里也转移了中心
r = r - 2
b = (pow(x-r, 2)+pow(y-r, 2)) - r*r
考虑不改变中心如下
r2 = r - 2
b = (pow(x-r, 2)+pow(y-r, 2)) - r2*r2
附带说明一下,现在您也知道如何画新月了。