[Python] 纯文本查看 复制代码
import time
import win32gui
dc = win32gui.GetDC(0)
# 中点算法画圆
def draw_circle(x, y, r,delay=0.001,lineClour=0x123456):
x0 = 0
y0 = r
d = 3 - 2 * r
while x0 <= y0:
win32gui.SetPixel(dc, x + x0, y + y0, lineClour)
win32gui.SetPixel(dc, x + y0, y + x0, lineClour)
win32gui.SetPixel(dc, x - y0, y + x0, lineClour)
win32gui.SetPixel(dc, x - x0, y + y0, lineClour)
win32gui.SetPixel(dc, x - x0, y - y0, lineClour)
win32gui.SetPixel(dc, x - y0, y - x0, lineClour)
win32gui.SetPixel(dc, x + y0, y - x0, lineClour)
win32gui.SetPixel(dc, x + x0, y - y0, lineClour)
time.sleep(delay)
if d < 0:
d += 4 * x0 + 6
else:
d += 4 * (x0 - y0) + 10
y0 -= 1
x0 += 1
# 画线
def bresenham(x0, y0, x1, y1,lineClour=0x123456):
dx = abs(x1 - x0)
dy = abs(y1 - y0)
sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1
err = dx - dy
while True:
# time.sleep(0.01)
win32gui.SetPixel(dc, x0, y0, lineClour)
if x0 == x1 and y0 == y1:
break
e2 = 2 * err
if e2 > -dy:
err -= dy
x0 += sx
if e2 < dx:
err += dx
y0 += sy
# 画实心圆
def draw_circle_fill(x0, y0, r,delay=0.001,lineClour=0x123456):
x = 0
y = r
d = 3 - 2 * r
while x <= y:
bresenham(x0 + x, y0 + y, x0 - x, y0 + y, lineClour)
bresenham(x0 + x, y0 - y, x0 - x, y0 - y, lineClour)
time.sleep(delay)
bresenham(x0 + y, y0 + x, x0 - y, y0 + x, lineClour)
bresenham(x0 + y, y0 - x, x0 - y, y0 - x, lineClour)
if d < 0:
d += 4 * x + 6
else:
d += 4 * (x - y) + 10
y -= 1
x += 1
# 画多边形
def draw_polygon(points,delay=0.001,lineClour=0x123456):
for i in range(len(points)):
x0 = points[i][0]
y0 = points[i][1]
x1 = points[(i + 1) % len(points)][0]
y1 = points[(i + 1) % len(points)][1]
bresenham(x0, y0, x1, y1,lineClour)
time.sleep(delay)
# 画椭圆
def draw_ellipse(x0, y0, a, b,delay=0.001,lineClour=0x123456):
x = 0
y = b
a2 = a * a
b2 = b * b
d = b2 - a2 * b + a2 / 4
while b2 * x <= a2 * y:
win32gui.SetPixel(dc, x0 + x, y0 + y, lineClour)
win32gui.SetPixel(dc, x0 - x, y0 + y, lineClour)
win32gui.SetPixel(dc, x0 + x, y0 - y, lineClour)
win32gui.SetPixel(dc, x0 - x, y0 - y, lineClour)
if d < 0:
d += b2 * (2 * x + 3)
else:
d += b2 * (2 * x - 2 * y + 5)
y -= 1
x += 1
d1 = b2 * (x + 0.5) * (x + 0.5) + a2 * (y - 1) * (y - 1) - a2 * b2
while y >= 0:
time.sleep(delay)
win32gui.SetPixel(dc, x0 + x, y0 + y, lineClour)
win32gui.SetPixel(dc, x0 - x, y0 + y, lineClour)
win32gui.SetPixel(dc, x0 + x, y0 - y, lineClour)
win32gui.SetPixel(dc, x0 - x, y0 - y, lineClour)
if d1 > 0:
d1 -= a2 * (2 * y - 1)
d1 += b2 * (2 * x + 3)
x += 1
y -= 1
# 画矩形
def draw_rectangle(x0, y0, x1, y1,delay=0.001,lineClour=0x123456):
bresenham(x0, y0, x1, y0,lineClour)
bresenham(x1, y0, x1, y1,lineClour)
bresenham(x1, y1, x0, y1,lineClour)
bresenham(x0, y1, x0, y0,lineClour)
time.sleep(delay)
if __name__=='__main__':
while True:
# 画线
# bresenham(400, 900, 1000, 700)
# 中心圆
draw_circle(1000, 200, 100, 0.001,0x0000FF)
# 椭圆
draw_ellipse(1500, 200, 100, 100, 0.001,0xFF0000)
# 矩形
draw_rectangle(1100, 400, 1200, 500, 0.001,0x00FF00)
# 多边形
draw_polygon([(900, 1000), (800, 800), (1000, 900), (1100, 1000)], 0.001,0x123456)
# 三角形
draw_polygon([(400, 200), (500, 300), (600, 200)], 0.001,0x123456)