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()

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 16:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 16:22:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 16:22:02       82 阅读
  4. Python语言-面向对象

    2024-03-29 16:22:02       91 阅读

热门阅读

  1. C#——系统学习(类与对象)

    2024-03-29 16:22:02       41 阅读
  2. 笔记82:关于 C++ 中的 swap 函数

    2024-03-29 16:22:02       40 阅读
  3. SQL中如何添加数据【保姆】

    2024-03-29 16:22:02       36 阅读
  4. sql server用nest typeorm实现索引的方式

    2024-03-29 16:22:02       38 阅读
  5. cesium NearFarScalar pixelOffsetScaleByDistance

    2024-03-29 16:22:02       40 阅读
  6. Compose UI 之 FloatingActionButton 按钮

    2024-03-29 16:22:02       40 阅读
  7. Docker 四种镜像制作方式

    2024-03-29 16:22:02       42 阅读
  8. qt.如何学习

    2024-03-29 16:22:02       32 阅读
  9. 软件速成书的神奇之处

    2024-03-29 16:22:02       34 阅读