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()
复制代码
相关推荐
哈__9 小时前
CANN加速3D目标检测推理:点云处理与特征金字塔优化
目标检测·3d·目标跟踪
心疼你的一切15 小时前
三维创世:CANN加速的实时3D内容生成
数据仓库·深度学习·3d·aigc·cann
3DVisionary1 天前
掌控发动机“心脏”精度:蓝光3D扫描在凸轮轴全尺寸检测中的应用
3d·图形渲染·汽车发动机·精密测量·蓝光3d扫描·凸轮轴检测·形位公差
coder攻城狮1 天前
VTK系列1:在屏幕绘制多边形
c++·3d
绝缘体11 天前
如何使用外卖霸王餐api接口?
大数据·搜索引擎·微信·pygame
PHOSKEY1 天前
3D工业相机如何“读透”每一个字符?快速识别、高精度3D测量
数码相机·3d
acai_polo1 天前
如何在国内合规、稳定地使用GPT/Claude/Gemini API?中转服务全解析
人工智能·gpt·ai·语言模型·ai作画
XX風1 天前
7.2 harris 3d
3d
多恩Stone1 天前
【3D-AICG 系列-3】Trellis 2 的O-voxel (下) Material: Volumetric Surface Attributes
人工智能·3d·aigc
多恩Stone1 天前
【3D-AICG 系列-1】Trellis v1 和 Trellis v2 的区别和改进
人工智能·pytorch·python·算法·3d·aigc