[英]How do I construct a point from x,y, line and angle?
I am making a geometry interface in python (currently using tkinter) but I have stumbled upon a major problem: I need a function that is able to return a point, that is at a certain angle with a certain line segment, is a certain length apart from the vertex of the angle.我正在 python 中制作几何接口(当前使用 tkinter),但我偶然发现了一个主要问题:我需要一个能够返回一个点的 function除了角的顶点。 We know the coordinates of the points of the line segment, and also the angle at which we want the point to be.
我们知道线段点的坐标,以及我们希望点的角度。 I have attached an image below for a more graphical view of my question.
我在下面附上了一张图片,以更直观地了解我的问题。 The problem: I can calculate it using trigonometry, where
问题:我可以使用三角函数计算它,其中
x, y = vertex.getCoords()
endx = x + length * cos(radians(angle))
endy = y + length * sin(radians(angle))
p = Point(endx, endy)
The angle I input is in degrees.我输入的角度以度为单位。 That calculation is true only when the line segment is parallel to the abscissa.
只有当线段平行于横坐标时,该计算才成立。 But the sizes of the angles I get back are very strange, to say the least.
但是至少可以说,我得到的角度的大小非常奇怪。 I want the function to work wherever the first two points are on the tkinter canvas, whatever the angle is.
我希望 function 能够在 tkinter canvas 上的前两点上工作,无论角度如何。 I am very lost as to what I should do to fix it.
我非常迷茫我应该做些什么来解决它。 What I found out: I get as output a point that when connected to the vertex, makes a line that is at the desired angle to the abscissa.
我发现了什么:我得到了 output 一个点,当它连接到顶点时,会形成一条与横坐标成所需角度的线。 So it works when the first arm(leg, shoulder) of the angle is parallel to the abscissa, then the function runs flawlessly (because of cross angles) - the Z formation.
因此,当角度的第一个臂(腿,肩)平行于横坐标时它起作用,然后 function 完美运行(因为交叉角) - Z 形。 As soon as I make it not parallel, it becomes weird.
一旦我让它不平行,它就会变得很奇怪。 This is because we are taking the y of the vertex, not where the foot of the perpendicular lands(C1 on the attached image).
这是因为我们取的是顶点的 y,而不是垂直脚的位置(附图中的 C1)。 I am pretty good at math, so feel free to post some more technical solutions, I will understand them
我数学还不错,所以请随意发布一些技术解决方案,我会理解的
EDIT: I just wanted to make a quick recap of my question: how should I construct a point that is at a certain angle from a line segment.编辑:我只是想快速回顾一下我的问题:我应该如何构造一个与线段成一定角度的点。 I have already made functions that create the angle in respect to the X and Y axes, but I have no idea how i can make it in respect to the line inputted.
我已经创建了相对于 X 轴和 Y 轴创建角度的函数,但我不知道如何相对于输入的线创建角度。 Some code for the two functions:
这两个功能的一些代码:
def inRespectToXAxis(vertex, angle, length):
x, y = vertex.getCoords()
newx = x + length * cos(radians(angle))
newy = y + length * sin(radians(angle))
p = Point(abs(newx), abs(newy))
return p
def inRespectToYAxis(vertex, length, angle):
x, y = vertex.getCoords()
theta_rad = pi / 2 - radians(angle)
newx = x + length * cos(radians(angle))
newy = y + length * sin(radians(angle))
p = Point(newx, newy)
return p
Seems you want to add line segment angle to get proper result.似乎您想添加线段角度以获得正确的结果。 You can calculate it using segment ends coordinates
(x1,y1)
and (x2,y2)
您可以使用线段末端坐标
(x1,y1)
和(x2,y2)
来计算它
lineAngle = math.atan2(y2 - y1, x2 - x1)
Result is in radians, so apply it as结果以弧度为单位,因此将其应用为
endx = x1 + length * cos(radians(angle) + lineAngle) etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.