Search code examples
pythongeometryangle

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. 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. 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. 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. 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). 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. 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

enter image description here


Solution

  • 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)

    lineAngle = math.atan2(y2 - y1, x2  - x1)
    

    Result is in radians, so apply it as

    endx = x1 + length * cos(radians(angle) + lineAngle) etc