Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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 I';我无法将3D坐标投影到2D屏幕坐标_Python_3d - Fatal编程技术网

Python I';我无法将3D坐标投影到2D屏幕坐标

Python I';我无法将3D坐标投影到2D屏幕坐标,python,3d,Python,3d,注意:因为我是一名新会员,我显然需要10个声誉来发布图片或2个以上的链接,所以我会参考这些图片。很抱歉给您带来不便 我正试图用python编写一个可视化3D对象的程序,但在将3D坐标投影到2D屏幕坐标上的函数中遇到了一个问题。它在某些情况下有效,但并非所有情况下都有效。当相机离点相对较远且大致在同一水平面上时,投影看起来非常漂亮,例如在这幅描绘十边形和矩形的图像中,其顶点由3D点定义(图2)。但是,当摄影机向下看这些点时,它们所描绘的对象会不成比例地变平(图3),当摄影机靠近必须投影的点时,形状

注意:因为我是一名新会员,我显然需要10个声誉来发布图片或2个以上的链接,所以我会参考这些图片。很抱歉给您带来不便

我正试图用python编写一个可视化3D对象的程序,但在将3D坐标投影到2D屏幕坐标上的函数中遇到了一个问题。它在某些情况下有效,但并非所有情况下都有效。当相机离点相对较远且大致在同一水平面上时,投影看起来非常漂亮,例如在这幅描绘十边形和矩形的图像中,其顶点由3D点定义(图2)。但是,当摄影机向下看这些点时,它们所描绘的对象会不成比例地变平(图3),当摄影机靠近必须投影的点时,形状会扭曲(图4)

所讨论的函数是Camera.projectPoint,我已将其在以下内容中使用的所有函数和类包括在内:

class Vector3:

    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
        self.mag = math.sqrt(x**2+y**2+z**2)
        self.yaw = math.atan2(self.y, self.x)
        self.pitch = math.atan2(self.z, math.sqrt(self.x**2+self.y**2))

class Point:

    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def vectorTo(self, p):
        return(Vector3(p.x-self.x, p.y-self.y, p.z-self.z))

class Camera:

    def __init__(self, pos, yaw, pitch, FOV=math.pi/2):
        self.pos = pos
        self.yaw = yaw
        self.pitch = pitch
        self.FOV = FOV

    def projectPoint(self, point):
        ## finding the vector from the camera position to the point
        v = self.pos.vectorTo(point)
        ## setting alpha to the difference in yaw between the camera and the vector to the point
        ## and beta to the difference in pitch
        alpha = v.yaw - self.yaw
        beta = v.pitch - self.pitch
        ## making sure that the smallest angle between the two is chosen
        ## (difference between 300 degrees and 10 degrees is 70, not 290)
        alpha = (alpha+math.pi)%(2*math.pi)-math.pi
        beta = (beta+math.pi)%(2*math.pi)-math.pi
        ## Doing the operation pictured in the diagram
        h = math.sin(self.FOV/2)/math.cos(self.FOV/2)
        x1 = math.sin(alpha)/math.cos(alpha)
        y1 = math.sin(beta)/math.cos(beta)
        ## adjusting for screen resolution
        return(x1*1000/h+500, y1*1000/h+325)

我四处寻找将3D坐标投影到屏幕上的算法,发现了很多东西,比如中描述的内容(这是我的函数的基础),但它似乎工作得不太好。这是算法的问题吗?我实现它的方式?它使用的功能之一?(我99%确信所有其他函数和类都非常好)。有什么问题以及如何解决的想法吗?谢谢。

您考虑过OpenGL吗?我有过,但我写这个程序更多的是为了挑战自己,而不是别的。OpenGL解决这个问题可能有点太好了,但您提供的链接似乎会有所帮助。我会试试的,我明白了。看看你是否发现自己摆脱了挑战。