PIT绘制椭圆代码
程序员文章站
2022-03-23 12:55:55
...
介绍
代码
import numpy as np
from PIL import Image
array = np.ndarray((660, 660, 3), np.uint8)
array[:, :, 0] = 255
array[:, :, 1] = 255
array[:, :, 2] = 255
for i in range(0,660,1):
array[i,330]=(0,0,0)
for j in range(0,660,1):
array[330,j]=(0,0,0)
def draw(x,y):
array[x+330,y+330]=(255,0,0)
array[x+330,330-y]=(255,0,0)
array[330-x,330-y]=(255,0,0)
array[330-x,y+330]=(255,0,0)
def MidpointEllipse(a,b):
x=0
y=b
d1=b*b+a*a*(-b+0.25)
draw(x,y)
while(b*b*(x+1)<a*a*(y-0.5)):
if(d1<0):
d1+=b*b*(2*x+3)
else:
d1+=(b*b*(2*x+3)+a*a*(-2*y+2))
y-=1
x += 1
draw(x,y)
d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b
while(y>0):
if(d2<0):
d2+=b*b*(2*x+2)+a*a*(-2*y+3)
x+=1
y-=1
else:
d2+=a*a*(-2*y+3)
y-=1
draw(x,y)
def main():
MidpointEllipse(100, 150)
image = Image.fromarray(array)
image.show()
if __name__ == "__main__":
main()
上一篇: 详解JavaScript中的强制类型转换
下一篇: P5.js完成动态旋转图的临摹和创作