在二维中计算类似 LookAt 的函数
Calculate LookAt like function in 2 dimensions
我正在尝试使用 Python 在 2 维中创建 lookAt 函数,所以现在这是我的代码。
from math import *
def lookAt(segment, originPoint):
segmentCenterPoint = getSegmentCenter(segment)
for i in range(2):
vtx = getVertex(segment, i)
x, y = getVertexCoord(vtx)
# Calculate the rotation angle already applied on the polygon
offsetAngle = atan2(y - segmentCenterPoint.y, x - segmentCenterPoint.x)
# Calculate the rotation angle to orient the polygon to an origin point
orientAngle = atan2(segmentCenterPoint.y - originPoint.y, segmentCenterPoint.x - originPoint.x)
# Get the final angle
finalAngle = orientAngle - (pi / 2)
if offsetAngle >= pi:
offsetAngle -= pi
elif offsetAngle < 0:
offsetAngle += pi
finalAngle += offsetAngle
# Temporary move the point to have its rotation pivot to (0,0)
tempX = x - segmentCenterPoint.x
tempY = y - segmentCenterPoint.y
# Calculate coords of the point with the rotation applied
s = sin(finalAngle)
c = cos(finalAngle)
newX = tempX * c - tempY * s
newY = tempX * s + tempY * c
# Move the point to the initial pivot
x = newX + segmentCenterPoint.x
y = newY + segmentCenterPoint.y
# Apply new coords to the vertex
setVertexCoord(vtx, x, y)
我手动尝试了一些示例并且效果很好,但是当我尝试将函数应用于数千个段时,似乎有些段的方向不正确。
我可能错过了什么,但我不知道是什么。另外,也许有更快的计算方法?
感谢您的帮助。
编辑
这是一个可视化图,可以更好地理解 lookAt 的目标。
目标是找到 A' 和 B' 坐标,假设我们已经知道 O、A 和 B。 ([AB] 是我们需要垂直于点 O 定向的线段)
要找到 A' 和 B' 的位置,您不需要旋转点(并且根本不需要处理角度)。
Find vector OC = segmentCenterPoint - originPoint
Make normalized (unit) vector oc = OC / Length(OC)
Make perpendicular vector P = (-oc.Y, oc.X)
Find CB length lCB
Find A' = C + lCB * P
B' = C - lCB * P
我正在尝试使用 Python 在 2 维中创建 lookAt 函数,所以现在这是我的代码。
from math import *
def lookAt(segment, originPoint):
segmentCenterPoint = getSegmentCenter(segment)
for i in range(2):
vtx = getVertex(segment, i)
x, y = getVertexCoord(vtx)
# Calculate the rotation angle already applied on the polygon
offsetAngle = atan2(y - segmentCenterPoint.y, x - segmentCenterPoint.x)
# Calculate the rotation angle to orient the polygon to an origin point
orientAngle = atan2(segmentCenterPoint.y - originPoint.y, segmentCenterPoint.x - originPoint.x)
# Get the final angle
finalAngle = orientAngle - (pi / 2)
if offsetAngle >= pi:
offsetAngle -= pi
elif offsetAngle < 0:
offsetAngle += pi
finalAngle += offsetAngle
# Temporary move the point to have its rotation pivot to (0,0)
tempX = x - segmentCenterPoint.x
tempY = y - segmentCenterPoint.y
# Calculate coords of the point with the rotation applied
s = sin(finalAngle)
c = cos(finalAngle)
newX = tempX * c - tempY * s
newY = tempX * s + tempY * c
# Move the point to the initial pivot
x = newX + segmentCenterPoint.x
y = newY + segmentCenterPoint.y
# Apply new coords to the vertex
setVertexCoord(vtx, x, y)
我手动尝试了一些示例并且效果很好,但是当我尝试将函数应用于数千个段时,似乎有些段的方向不正确。
我可能错过了什么,但我不知道是什么。另外,也许有更快的计算方法?
感谢您的帮助。
编辑
这是一个可视化图,可以更好地理解 lookAt 的目标。 目标是找到 A' 和 B' 坐标,假设我们已经知道 O、A 和 B。 ([AB] 是我们需要垂直于点 O 定向的线段)
要找到 A' 和 B' 的位置,您不需要旋转点(并且根本不需要处理角度)。
Find vector OC = segmentCenterPoint - originPoint
Make normalized (unit) vector oc = OC / Length(OC)
Make perpendicular vector P = (-oc.Y, oc.X)
Find CB length lCB
Find A' = C + lCB * P
B' = C - lCB * P