如何用中心坐标和一个点的坐标创建一个三角形?

Rui Coito

我编写了一些代码来尝试实现一个使用屏幕中心坐标创建三角形的函数,在这种情况下,它也对应于三角形中心,因此将这些坐标标识为“ cx”和“ cy”,因为窗口是800 x 600 cx = 400和cy =300。为此,我创建了一个“第一个点”,该点具有与中心相同的x坐标,但其位于中心上方100个像素,现在我使用该中心,并且尝试了第一个点计算另一点应该在哪里。这是代码:

def triangle(cx,cy):
    angle = 2*math.pi/3
    first_point_x = cx
    first_point_y = cy + 100
    vertex = [first_point_x,first_point_y]
    for i in range(2):
        newx = (vertex[i*2]-cx)   * math.cos(angle) - (vertex[i*2+1]-cy) * math.sin(angle)
        newy = (vertex[i*2+1]-cy) * math.cos(angle) + (vertex[i*2]-cx) * math.sin(angle)
        vertex.append(newx)
        vertex.append(newy)
    return vertex

但是由于某种原因,数组给出的负值和数字总体上与我想要的不对应。任何帮助谁将不胜感激。

拉比德76

您实际要做的是计算从(cx,cy)到数组最后一个点的向量,并将向量旋转120°。但是在将点附加到列表之前,您错过了将向量添加到(cx,cy)的操作:

newx = (vertex[i*2]-cx) * math.cos(angle) - (vertex[i*2+1]-cy) * math.sin(angle)
newy = (vertex[i*2+1]-cy) * math.cos(angle) + (vertex[i*2]-cx) * math.sin(angle)

newx = cx + (vertex[i*2]-cx)   * math.cos(angle) - (vertex[i*2+1]-cy) * math.sin(angle)
newy = cy + (vertex[i*2+1]-cy) * math.cos(angle) + (vertex[i*2]-cx) * math.sin(angle)

计算从中心到最后一点的向量

vx = vertex[i*2] - cx
vy = vertex[i*2+1] - cy

旋转向量

rotated_vx = vx * math.cos(angle) - vy * math.sin(angle)
rotated_vy = vy * math.cos(angle) + vx * math.sin(angle)

计算新点

newx = cx + rotated_vx 
newy = cy + rotated_vy 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章