简体   繁体   English

如何从 x、y、直线和角度构造一个点?

[英]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.

相关问题 如何在python中给定点(x,y)和线与y轴的夹角的情况下找到线的斜率(m)? - How do I find the slope (m) for a line given a point (x,y) on the line and the line's angle from the y axis in python? 如何使用x和y坐标进行点的透视变换 - How do I make perspective transform of point with x and y coordinate 如何从 ply 文件中获取 python 中给定点 ( x, y ) 的 z 轴坐标? - How do I get z-axis coordinate from ply file for a given point ( x, y ) in python? 如何在知道起始坐标、线长 x 角度和 y、角度的情况下画线 - how to draw a line knowing the starting coordinate, line length x angle and y, angle Python:使用到点 A 的距离(x0,y0)和角度找到给定点 B 的 ax,y 坐标 - Python: find a x,y coordinate for a given point B using the distance from the point A (x0, y0) and the angle 如何找到 X 和 Y 的角度,然后找到 plot 一条以可变角度横切每条网格线的线? - How to find the angle of X and Y and then plot a line transecting each grid lines at a variable angle? 如何通过此 seaborn plot 添加 x=y 线? - How do I add an x=y line through this seaborn plot? Python - 我怎样才能找到一条线在给定点的角度? - Python - how can i find the angle of a line at a given point? 如何将道路上某个点在其图像坐标中的 (x,y) 坐标转换为其在世界坐标中的 (x,y) 坐标? - How do I convert the (x,y) coordinates of a point on the road in its image coordinates, to its (x,y) coordinates in the world coordinates? 加快矩阵中每个x,y点的角度计算 - Speed up angle calculation for each x,y point in a matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM