Intro
Github仓库地址
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201126103224522.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzAzMTA5Mg==,size_16,color_FFFFFF,t_70#pic_center)
Code
'''
@File : mpline.py
@Time : 2020/11/19 19:09:22
@Author : Kearney
@Version : 0.0.0
@Contact : 191615342@qq.com
@License : GPL 3.0
@Desc : 计算机图形学-DAA改进后的中点算法实现,基于pygame的鼠标事件,
鼠标点下一个点后画点,再点下一个点后由中点算法划线,可划多条,右键清空画面重置
首先画出来的是由pygame划线函数画出来的红线,延时1s后用中点算法划白线,以此作为对比
参考《计算机图形学基础 OpenGL版》 徐文鹏 清华大学出版社
3.1.4 中点划线法
'''
import time
import pygame
import sys
width = 920
height = 640
isp = False
x1 = y1 = x2 = y2 = -1
ps = (x1, y1)
pe = (x2, y2)
def swap(a, b):
return b, a
def MidPointLine(sur, x1, y1, x2, y2):
tag = 0
dx = x2 - x1
dy = y2 - y1
if (abs(dx) < abs(dy)):
x1, y1 = swap(x1, y1)
x2, y2 = swap(x2, y2)
tag = 1
if (x1 > x2):
x1, x2 = swap(x1, x2)
y1, y2 = swap(y1, y2)
a = y1 - y2
b = x2 - x1
d = a + (b / 2)
if (y1 < y2):
x = x1
y = y1
while (x < x2):
if (d < 0):
y = y + 1
x = x + 1
d = d + a + b
else:
d = d + a
x = x + 1
if (tag):
pygame.draw.circle(sur, (255, 255, 255), (y, x), 1, 1)
else:
pygame.draw.circle(sur, (255, 255, 255), (x, y), 1, 1)
else:
x = x2
y = y2
while (x > x1):
if (d < 0):
y = y + 1
x = x - 1
d = d - a + b
else:
x = x - 1
d = d - a
if (tag):
pygame.draw.circle(sur, (255, 255, 255), (y, x), 1, 1)
else:
pygame.draw.circle(sur, (255, 255, 255), (x, y), 1, 1)
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("MidPointLine_Kearney")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if x1 < 0:
x1, y1 = event.pos
else:
x2, y2 = event.pos
pygame.draw.circle(screen, (255, 255, 255), event.pos, 1, 1)
print('(', x1, ',', y1, ')', ' (', x2, ',', y2, ')')
if (x2 > 0):
pygame.draw.line(screen, (255, 0, 0), (x1, y1),
(x2, y2))
pygame.display.update()
isp = True
if (pygame.mouse.get_pressed()[2]):
x1 = y1 = x2 = y2 = -1
screen.fill((0, 0, 0))
print("Reset done")
if (isp):
isp = False
time.sleep(1)
MidPointLine(screen, x1, y1, x2, y2)
x1 = y1 = x2 = y2 = -1
pygame.display.update()
sys.exit()