pygame用自带函数绘制三角形 计算重心坐标

在这里插入图片描述

在这里插入图片描述
三角形重心坐标公式
三角形重心的坐标可以通过其三个顶点的坐标计算得出,公式为((X1+X2+X3)/3,(Y1+Y2+Y3)/3)。12

这是因为在平面直角坐标系中,重心的坐标是顶点坐标的算术平均数
中间黑点是重心坐标

import pygame
from pygame.locals import *
import sys
import math

pygame.init()

width, height = 800, 600
screen = pygame.display.set_mode((width, height))

vertices = [(100, 400, 1), (200, 200, 1), (400, 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
def rotate_point(point, angle):
    x, y, z = point
    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))
    return (new_x, y, new_z)

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 = []
    for vertex in vertices:
        rotated_vertex = rotate_point(vertex, angle)
        points.append(rotated_vertex)
    #pygame.draw.polygon(screen, (255, 0, 0), [(width/2 + p[0], height/2 - p[1]) for p in points])
    transformed_points = []
    for p in points:
        if ci==0:

            ax=p[0]*30/suofang +300 # 将x坐标用透视投影在屏幕上
            ay=p[1]*30/suofang +200# 将y坐标用透视投影在屏幕上
            transformed_points.append((ax, ay))  # 将转换后的坐标添加到列表中
            #print(']]',0)
        if ci==1:
            bx=p[0]*30/suofang +200# 将y坐标用透视投影在屏幕上
            by=p[1]*30/suofang +200# 将y坐标用透视投影在屏幕上
            transformed_points.append((bx, by))  # 将转换后的坐标添加到列表中
            #print("]",1)
        if ci==2:
            cx=p[0]*30/suofang +200# 将y坐标用透视投影在屏幕上
            cy=p[1]*30/suofang +200# 将y坐标用透视投影在屏幕上
            transformed_points.append((cx, cy))  # 将转换后的坐标添加到列表中
            #print("]",2)
        # x = p[0]*30/suofang +300 # 将x坐标用透视投影在屏幕上
        # y =  p[1]*30/suofang +200# 将y坐标用透视投影在屏幕上
        #transformed_points.append((x, y))  # 将转换后的坐标添加到列表中
        if (ci>=2):
            ci=0
        else:
            ci=ci+1
        print(p,';;')
    # 绘制多边形
    pygame.draw.polygon(screen, (200, 200, 200), transformed_points)



while True:
    screen.fill((255, 255, 255))
    c.tick(70)
    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-1
    draw_triangle(vertices)
    angle += rotation_speed
    print("....",ax,ay,bx,by,cx,cy)
    zx = (ax + bx + cx) / 3
    zy = (ay + by + cy) / 3
    pygame.Surface.set_at(screen, (int(zx), int(zy)),
                          (190, 90, 90))
    pygame.display.flip()
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值