Search code examples
python-3.xmathcoordinatesangle

Calculate position after adding an angle


I am trying to make a calculator-like program that has a specific starting point and I want to take a point on a canvas (I get the x and y of this point). Now, I want to from the starting_point add 30 on the angle and calculate the point on the canvas (like a circle and just change it's position).

I don't have any code about it.

I know in maths that I have to think of a circle and calculate it's position on the circle but I am not sure about how to do this in programming. I'd like if you could show me in how to do it in preferably python, or in another language.


Solution

  • here's a sample code:

    import math
    
    x = int(input('x position: '))
    y = int(input('y position: '))
    a = - math.radians(int(input('angle to add: ')))
    
    new_x = math.cos(a)*x - math.sin(a)*y
    new_y = math.sin(a)*x + math.cos(a)*y
    
    print(f'new point: {new_x} {new_y}')