pygame.Surface.set_at(screen, (int(w), int(h)),
(int(255zhongxina),int(255zhongxinb),int(255zhongxinc)))
颜色是由三个重心坐标权重abc255求出的
import pygame
from pygame.locals import *
import sys
import math
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
vertices = [(160, 300, 1), (400, 40, 1), (600, 300, 1)]
angle = 0
rotation_speed = 2 # 可根据需要调整旋转速度
c=pygame.time.Clock()
f=False
suofang=100
ax=0
ay=0
bx=0
by=0
cx=0
cy=0
ci=0
zx=0
zy=0
zhongxina=0
zhongxinb=0
zhongxinc=0
def draw_triangle(vertices):
#points = [rotate_point(vertex, angle) for vertex in vertices]
global ci
global ax
global ay
global bx
global by
global cx
global cy
points = []
new_vertices = []
for x, y, z in vertices:
new_x = x * math.cos(math.radians(angle)) - z * math.sin(math.radians(angle))
new_z = x * math.sin(math.radians(angle)) + z * math.sin(math.radians(angle))
new_vertices.append((new_x, y, new_z))
(ax, ay, az), (bx, by, bz), (cx, cy, cz) = new_vertices
#print(p,';;')
# 绘制多边形
#pygame.draw.polygon(screen, (200, 200, 200), transformed_points)
#print(transformed_points)
while True:
screen.fill((255, 255, 255))
#c.tick(7)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
f = True
if event.type == pygame.KEYUP:
f = False
if f==True:
suofang=suofang-10
draw_triangle(vertices)
#angle += rotation_speed
#print("....","ax",ax ,"ay",ay,"bx",bx,"by",by,"cx",cx,"cy",cy)
for h in range(600):
for w in range(600):
zx = w
zy = h
# 重心坐标在求出中心后就不需要了,后面需要用这个坐标来判断屏幕中每个像素是否在三角形内
#print(zx,zy)
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
if (0<zhongxina<1 and 0<zhongxinb<1 and 0<zhongxinc<1):
#print(h,w)
pygame.Surface.set_at(screen, (int(w), int(h)),
(int(255*zhongxina),int(255*zhongxinb),int(255*zhongxinc)))
# zx =(ax + bx + cx) / 3
# zy =(ay + by + cy) / 3
# #重心坐标在求出中心后就不需要了,后面需要用这个坐标来判断屏幕中每个像素是否在三角形内
#
# '''
# x,y=500 abc三点不满足大于0小于1
# -0.7076923076923077
# 1.3282051282051281
# 0.3794871794871797
# 1.0
#
# x,y=143 都满足大于0小于1,在三角形内
# 0.94 0.04666666666666667 0.013333333333333384 1.0
#
# x,y=300 都满足,在三角形内
# 0.2153846153846154 0.6102564102564103 0.1743589743589743 1.0
#
# x,y=0 不满足,不在三角形内
# 1.6 -0.4666666666666667 -0.13333333333333341 0.9999999999999999
#
# x=770,y=143 不满足 不在三角形内
# -0.6676923076923077 -1.0251282051282051 2.692820512820513 1.0
#
# x=230,y=190满足,在
# 0.6205128205128205 0.147008547008547 0.23247863247863246 1.0
# '''
# 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,zhongxina+zhongxinb+zhongxinc,"zx,",zx,"zy",zy)
pygame.Surface.set_at(screen, (int(zx), int(zy)),
(190, 90, 90))
pygame.display.flip()