其实就是在扫描线填充三角形的基础上,对三个顶点的xy坐标计算三角形重心坐标,然后在填充时,原来三角形重心坐标填充是遍历整个屏幕像素,然后判断是否在在三角形内,变成根据直线方程计算x起点与y起点,然后将需要求重心坐标的点zx,zy,从遍历屏幕像素的点xy,换成扫描线起始点xy
go
for gao in range(280):
xz1 = (jz - (gao+by)) / zk
sx = abs(xz1)
xy1 = (jy - (gao+by)) / yk
ex = abs(xy1)+2#加2是为了填补空白
xcishu=ex-sx
#print("gao==",gao,"第一次循环",int(sx),int(ex))
for i in range(int(xcishu)):
#print("i==",i,"第二次循环",int(sx),int(ex))
zx = int(sx)+i
zy = gao+by
将原来的
#pygame.Surface.set_at(screen, (int(sx)+i , by+gao), (255, 0, 0))
换成
if (0 < zhongxina < 1 and 0 < zhongxinb < 1 and 0 < zhongxinc < 1): pygame.Surface.set_at(screen, (int(sx)+i, int(gao+by )),(int(255 * zhongxina), int(255 * zhongxinb), int(255 * zhongxinc)))
加by是因为
for gao in range(280):
280是三角形上顶点与下边之间的高度,还需要加上上顶点的y才能填充580这个高度的三角形,不然出现两个三角形
go
import pygame
import sys
from pygame import gfxdraw
(width, height) = (600, 600)
pygame.init()
screen = pygame.display.set_mode((width, height))
image = pygame.image.load("1.png").convert_alpha()
masked_result = image.copy()
white_color = (0, 0, 0)
polygon =[(0, 0), (800, 600), (0, 600)]
#(260,0),(200,0),(200,130),(260,130)
#[(0.0, 307.6923076923077), (723.0769230769231, 307.6923076923077), (972.4137931034483, 413.7931034482759), (0.0, 413.7931034482759)]
image = pygame.image.load("1.png")
car = pygame.transform.scale(image, (100, 100))
#screen.fill((0,0,0))
#screen.blit(image, (140,140))
screensurf = pygame.display.get_surface()#获取当前显示的 Surface 对象
color=(255, 255, 255)
ix=0#纹理坐标
iy=0
px=310#画出来填充像素坐标
py=310
ax=70
ay=580
bx=160
by=300
cx=200
cy=580
#(70, 580), (160, 300), (200, 580)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#gfxdraw.pixel(screen, 90, 90, color)#画一个像素
screen.fill(color)
pygame.draw.polygon(screen, (0, 0, 255), ((ax,ay), (bx, by), (cx, cy)))#左,中,右
#(70, 580), (160, 300), (200, 580)
zk=(ay-by)/(ax-bx)#左边斜率
yk=(by-cy)/(bx-cx)
#y=kx+b -b=kx-y 但是没法用-b当变量,下面就-abs手动转负数
jiejuz=(bx*zk)-by#截距
jz=0
if jiejuz<0:
jz=abs(jiejuz)
else:
jz=-abs(jiejuz)
#print(jiejuz,jz)
'''yz=zk*(70+0.5)+jz
#y1 = zk * 90 + jieju
#print(int(46600/90),int(y1), int(yz))
pygame.Surface.set_at(screen, (90, int(46600/90)), (255,0,0))
pygame.Surface.set_at(screen, (int(70+0.5), int(yz)), (255,0, 0))
'''
# (70, 580), (160, 300), (200, 580)
yk = (by - cy) / (bx - cx)
jiejuy=(bx*yk)-by#截距
jy = 0
if jiejuy < 0:
jy = abs(jiejuy)
else:
jy = -abs(jiejuy)
yy = yk * cx + jy
pygame.Surface.set_at(screen, (cx, int(yy)), (0, 255, 0))
yz = zk * (ax + 0.5) + jz
#y=kx+b -kx=b-y
#xz=((5/3)-(8/3))/(1/3)
xz = (jz - by) /zk
zuoxianx=abs(xz)
xy=(jy-by)/yk
youxianx=abs(xy)
# y1 = zk * 90 + jieju
# print(int(46600/90),int(y1), int(yz))
pygame.Surface.set_at(screen, (int(youxianx), by), (255, 0, 0))
pygame.Surface.set_at(screen, (int(zuoxianx), by), (255, 0, 0))
starx=int(zuoxianx)
endx=int(youxianx)
#print(yk,"截距左", jz, jy, "右扫描线y", yy, "左边扫描线y", yz,"左扫描线x",zuoxianx,"左扫描线x",youxianx)
dong=0
stary=by
endy=580
for gao in range(280):
xz1 = (jz - (gao+by)) / zk
sx = abs(xz1)
xy1 = (jy - (gao+by)) / yk
ex = abs(xy1)+2#加2是为了填补空白
xcishu=ex-sx
#print("gao==",gao,"第一次循环",int(sx),int(ex))
for i in range(int(xcishu)):
#print("i==",i,"第二次循环",int(sx),int(ex))
zx = int(sx)+i
zy = gao+by
# # 重心坐标在求出中心后就不需要了,后面需要用这个坐标来判断屏幕中每个像素是否在三角形内
#
za1 = -(zx - bx) * (cy - by) + (zy - by) * (cx - bx)
za2 = -(ax - bx) * (cy - by) + (ay - by) * (cx - bx)
zhongxina = za1 / za2
zb1 = -(zx - cx) * (ay - cy) + (zy - cy) * (ax - cx)
zb2 = -(bx - cx) * (ay - cy) + (by - cy) * (ax - cx)
zhongxinb = zb1 / zb2
zhongxinc = 1 - zhongxina - zhongxinb
print(zhongxina, zhongxinb, zhongxinc)
#
if (0 < zhongxina < 1 and 0 < zhongxinb < 1 and 0 < zhongxinc < 1):
pygame.Surface.set_at(screen, (int(sx)+i, int(gao+by)),(int(255 * zhongxina), int(255 * zhongxinb), int(255 * zhongxinc)))
# pygame.Surface.set_at(screen, (i, int(580)), (0, 255, 0))
print(zx, zy)
#pygame.Surface.set_at(screen, (int(sx)+i , by+gao), (255, 0, 0))
pygame.display.update()