3D输出失真

2024-10-02 08:22:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用旋转矩阵、弱透视方程和自定义海龟包装器的组合来制作一个3D海龟包装器(这是一个玩具项目,看看我是否可以在没有numpy的情况下也使用海龟的情况下完成3D) 为了澄清这一点,我不必复制和粘贴代码,pix(point, update)在屏幕上标记一个小方形海龟来显示顶点。setup()使其tracer(0,0),然后shapesize(1 / 20, 1 / 20)ht,最后penup()refresh()只使用update() 在这里:

def vertdata(crt): #CRT stands for Coord-Rotation-Transformation. I took 
                   #transformation out for now, because those values are 0
                   #for this example.
  vert = [crt[0][0],crt[0][1],crt[0][2]]
  rotation = [crt[1][0],crt[1][1],crt[1][2]]
  return rotate(vert, rotation)


def vertex(point, upd):
  data = vertdata(point)
  pix([((data[0] * 2) / ((data[2] / 15) - 15) * 10), ((data[1] * 2) / ((data[2] / 15) - 15) * 10)], upd)

setup(100, 1000, 1000)
for x in range(3600):
    clear()
    vertex(
          [[10,-10,10],
           [45,45,x]],
          False
          )

    vertex(
          [[-10,-10,10],
           [45,45,x]],
          False
          )

    vertex(
          [[10,10,10],
           [45,45,x]],
          False
          )

    vertex(
          [[-10,10,10],
           [45,45,x]],
          False
          )

    vertex(
          [[10,-10,-10],
           [45,45,x]],
          False
          )

    vertex(
          [[-10,-10,-10],
           [45,45,x]],
          False
          )

    vertex(
          [[10,10,-10],
           [45,45,x]],
          False
          )

    vertex(
          [[-10,10,-10],
           [45,45,x]],
          False
          )
    refresh()

#Sorry about the long code. I don't really think I can simplify this any more.

有什么问题吗?Z轴上的旋转扭曲。立方体会拉伸和变形。很久以前,我在scratch中也做了同样的事情(很久以前),我把这些公式输入python,因为我知道它们是有效的。(所以是的,他们工作)我不能发现问题,也许你们中的一个可以?这是我最后的选择。这里有一张图片可以帮助你。对不起,我现在用手机,因为我不能再使用电脑了,所以我不能给出失真的图片。你知道吗

img

我猜错了,但我找不到。你知道吗


Tags: falsefordatadefsetup情况updatethis
1条回答
网友
1楼 · 发布于 2024-10-02 08:22:24

我把它修好了,这是我方程式中的一个输入错误。 另外,我忘了在文章中包含旋转方程函数。你知道吗

def tcos(angle):
  r = radians(angle)
  return cos(r)

def tsin(angle):
  r = radians(angle)
  return sin(r)

def rotate(vert, rot):
  cA,cB,cC = [tcos(rot[0]),tcos(rot[1]),tcos(rot[2])]
  sA,sB,sC = [tsin(rot[0]),tsin(rot[1]),tsin(rot[2])]
  x,y,z = [vert[0],vert[1],vert[2]]


  return [((x * cB * cC) + (y * sA * sB * cC) + (y * cA * sC) + (z * -cA * sB * cC) + (z * sA * sC)),
          ((x * -cB * sC) + (y * -sA * sB * sC) + (y * cA * cC) + (z * cA * sB * sC) + (z * sA * cC)),
          ((x * sB) + (y * (-sA * cB)) + (z * (cA * cB)))]

#In the very first set of parenthesis, it was x * cB * cA before. That was the problem.

相关问题 更多 >

    热门问题