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()
复制代码
相关推荐
云空7 小时前
《PyQt6-3D应用开发技术文档》
3d·pyqt
前端小盆友8 小时前
从零实现一个GPT 【React + Express】--- 【5】实现网页生成能力
gpt·react.js·express
鹧鸪云光伏12 小时前
光伏无人机3D建模:毫秒级精度设计
3d·无人机
搞前端的小菜14 小时前
从零实现一个GPT 【React + Express】--- 【2】实现对话流和停止生成
gpt·react.js·express
杀生丸学AI14 小时前
【三维生成】FlashDreamer:基于扩散模型的单目图像到3D场景
人工智能·3d·大模型·aigc·蒸馏与迁移学习·扩散模型与生成模型
sky丶Mamba1 天前
Transformer、BERT、GPT以及Embedding之间的关系
gpt·bert·transformer
gis分享者1 天前
学习threejs,使用自定义GLSL 着色器,生成漂流的3D能量球
3d·threejs·着色器·glsl·shadermaterial·能量球
m0_743106461 天前
【论文笔记】BlockGaussian:巧妙解决大规模场景重建中的伪影问题
论文阅读·计算机视觉·3d·aigc·几何学
向宇it1 天前
【unity小技巧】在 Unity 中将 2D 精灵添加到 3D 游戏中,并实现阴影投射效果,实现类《八分旅人》《饥荒》等等的2.5D游戏效果
游戏·3d·unity·编辑器·游戏引擎·材质
陈敬雷-充电了么-CEO兼CTO2 天前
主流大模型Agent框架 AutoGPT详解
人工智能·python·gpt·ai·chatgpt·nlp·aigc