Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中形状的旋转不正确_Python_Shapes - Fatal编程技术网

Python中形状的旋转不正确

Python中形状的旋转不正确,python,shapes,Python,Shapes,我试图在pygame中创建一个旋转平面。我正在将3d(x,y,z)坐标转换为屏幕坐标(x,y),然后旋转屏幕坐标。当它在x轴和y轴上都旋转时,这似乎是可行的,但是当它只在一个轴上旋转时(我注释掉了y轴旋转),它是倾斜的。我似乎不明白为什么 import pygame import math red = (255, 0, 0) class Vector3: def __init__(self, _x, _y, _z): self.x = _x self.

我试图在pygame中创建一个旋转平面。我正在将3d(x,y,z)坐标转换为屏幕坐标(x,y),然后旋转屏幕坐标。当它在x轴和y轴上都旋转时,这似乎是可行的,但是当它只在一个轴上旋转时(我注释掉了y轴旋转),它是倾斜的。我似乎不明白为什么

import pygame
import math

red = (255, 0, 0)

class Vector3:
    def __init__(self, _x, _y, _z):
        self.x = _x
        self.y = _y
        self.z = _z

class Vector2:
    def __init__(self, _x, _y):
        self.x = _x
        self.y = _y

class Plane:
    def draw(self, screen, value):
        scale = 25

        points = []
        vertices = [Vector3(0, 1, 0),
                    Vector3(1, 1, 0),
                    Vector3(1, 0, 0),
                    Vector3(0, 0, 0)]

        for vert in vertices:
            x, y = vec3to2(vert)
            points.append(Vector2(x * scale + 40, y * scale + 100))
            print((x, y))

        centerx = (points[0].x + points[1].x + points[2].x + points[3].x) / 4
        centery = (points[0].y + points[1].y + points[2].y + points[3].y) / 4

        for point in points:
            rotx, roty = vec3rot(point, math.radians(value), centerx, centery)
            point.x = rotx
            #point.y = roty

        pygame.draw.line(screen, red, (points[0].x, points[0].y), (points[1].x, points[1].y))
        pygame.draw.line(screen, red, (points[1].x, points[1].y), (points[2].x, points[2].y))
        pygame.draw.line(screen, red, (points[0].x, points[0].y), (points[3].x, points[3].y))
        pygame.draw.line(screen, red, (points[3].x, points[3].y), (points[2].x, points[2].y))

        pygame.draw.circle(screen, red, (int(centerx), int(centery)), 1)

def vec3to2(vect3):
    try:
        _x = vect3.x / vect3.z
    except ZeroDivisionError:
        _x = vect3.x

    try:
        _y = vect3.y / vect3.z
    except ZeroDivisionError:
        _y = vect3.y

    return(_x, _y)

def vec3rot(vect3, theta, centerx, centery):
    _x = centerx + (vect3.x - centerx) * math.cos(theta) - (vect3.y - centery) * math.sin(theta)
    _y = centery + (vect3.x - centerx) * math.sin(theta) + (vect3.y - centery) * math.cos(theta)

    return(_x, _y)

def main():
    pygame.init()
    screen = pygame.display.set_mode((640, 480))

    v = 0

    plane = Plane()

    running = True

    while running:
        screen.fill((0, 0, 0))
        plane.draw(screen, v)
        pygame.display.flip()
        v += 0.1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

main()
pygame已经有向量-pygame已经有向量-