gpt 3d三角形 重心坐标填充 沿x轴炫赵师傅

clike 复制代码
```go
import pygame
from pygame.locals import *
import sys
import math

# 初始化Pygame
pygame.init()

# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('3D Triangle Fill with Barycentric Coordinates')

# 定义三角形的三个顶点坐标(包含z坐标)
v0 = (200, 100, 0)
v1 = (400, 500, 0)
v2 = (600, 200, 0)
img=pygame.image.load("11.jpg").convert_alpha()
# 定义旋转角度
angle_x = 0
angle_y = 0
angle_z = 0
rotation_speed = 0.01  # 每帧旋转的速度

def rotate_x(vertex, angle):
    x, y, z = vertex
    new_y = y * math.cos(angle) - z * math.sin(angle)
    new_z = y * math.sin(angle) + z * math.cos(angle)
    return x, new_y, new_z

def rotate_y(vertex, angle):
    x, y, z = vertex
    new_x = x * math.cos(angle) + z * math.sin(angle)
    new_z = -x * math.sin(angle) + z * math.cos(angle)
    return new_x, y, new_z

def rotate_z(vertex, angle):
    x, y, z = vertex
    new_x = x * math.cos(angle) - y * math.sin(angle)
    new_y = x * math.sin(angle) + y * math.cos(angle)
    return new_x, new_y, z

# 对三角形顶点进行旋转变换
def rotate_triangle():
    global v0, v1, v2, angle_x, angle_y, angle_z
    v0 = rotate_x(v0, angle_x)
    v1 = rotate_x(v1, angle_x)
    v2 = rotate_x(v2, angle_x)

    v0 = rotate_y(v0, angle_y)
    v1 = rotate_y(v1, angle_y)
    v2 = rotate_y(v2, angle_y)

    v0 = rotate_z(v0, angle_z)
    v1 = rotate_z(v1, angle_z)
    v2 = rotate_z(v2, angle_z)

# 渲染三角形
def render_triangle(v0, v1, v2):
    for x in range(int(min(v0[0], v1[0], v2[0])), int(max(v0[0], v1[0], v2[0]))):
        for y in range(int(min(v0[1], v1[1], v2[1])), int(max(v0[1], v1[1], v2[1]))):
            u, v, w = barycentric_coordinates((x, y), v0, v1, v2)
            if u >= 0 and v >= 0 and w >= 0:
                z = u * v0[2] + v * v1[2] + w * v2[2]
                screen.set_at((x, y), (img.get_at((int(u*img.get_width()%img.get_width()),int((v*img.get_height())%img.get_height())))))

def barycentric_coordinates(p, v0, v1, v2):
    u = ((v1[1] - v2[1]) * (p[0] - v2[0]) + (v2[0] - v1[0]) * (p[1] - v2[1])) / \
        ((v1[1] - v2[1]) * (v0[0] - v2[0]) + (v2[0] - v1[0]) * (v0[1] - v2[1]))
    v = ((v2[1] - v0[1]) * (p[0] - v2[0]) + (v0[0] - v2[0]) * (p[1] - v2[1])) / \
        ((v1[1] - v2[1]) * (v0[0] - v2[0]) + (v2[0] - v1[0]) * (v0[1] - v2[1]))
    w = 1 - u - v
    return u, v, w

# 主循环
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # 清空屏幕
    screen.fill((0, 0, 0))

    # 旋转三角形
    rotate_triangle()

    # 渲染并填充三角形
    render_triangle(v0, v1, v2)

    # 更新角度
    angle_x += rotation_speed
    angle_y += rotation_speed
    angle_z += rotation_speed

    pygame.display.flip()
复制代码
相关推荐
funfan05174 小时前
GPT-5博士级AI使用教程及国内平替方案
人工智能·gpt
郝学胜-神的一滴9 小时前
Three.js 材质系统深度解析
javascript·3d·游戏引擎·webgl·材质
z千鑫13 小时前
【OpenAI】 GPT-4o-realtime-preview 多模态、实时交互模型介绍+API的使用教程!
人工智能·gpt·语言模型·aigc
人工干智能13 小时前
pygame的帧处理中,涉及键盘的有`pg.event.get()`与`pg.key.get_pressed()` ,二者有什么区别与联系?
python·游戏·计算机外设·pygame
AndrewHZ1 天前
【3D重建技术】如何基于遥感图像和DEM等数据进行城市级高精度三维重建?
图像处理·人工智能·深度学习·3d·dem·遥感图像·3d重建
Code_流苏1 天前
AI热点周报(8.10~8.16):AI界“冰火两重天“,GPT-5陷入热议,DeepSeek R2模型训练受阻?
人工智能·gpt·gpt5·deepseek r2·ai热点·本周周报
oe10191 天前
读From GPT-2 to gpt-oss: Analyzing the Architectural Advances(续)
笔记·gpt·学习
小白杨树树1 天前
【LLM】文献阅读-ISOLATE GPT:基于大语言模型的执行隔离架构
人工智能·gpt·语言模型
*星星之火*1 天前
【GPT入门】第47课 大模型量化中 float32/float16/uint8/int4 的区别解析:从位数到应用场景
人工智能·gpt
赛博郎中1 天前
pygame小游戏飞机大战_8继承精灵玩家优化
python·pygame