如何创建一个中心坐标和一点坐标的三角形?
How to create a triangle with the center coordinates and the coordinates of one point?
我写了一些代码来尝试实现一个使用屏幕中心坐标创建三角形的函数,在这种情况下它也对应于三角形的中心,这个坐标被标识为“cx”和“cy”,因为 window 是 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
但由于某种原因,数组给出的负值和数字总体上与我想要的不符。
任何帮助都将不胜感激。
你实际上做的是计算从 (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
我写了一些代码来尝试实现一个使用屏幕中心坐标创建三角形的函数,在这种情况下它也对应于三角形的中心,这个坐标被标识为“cx”和“cy”,因为 window 是 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
但由于某种原因,数组给出的负值和数字总体上与我想要的不符。 任何帮助都将不胜感激。
你实际上做的是计算从 (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