Search code examples
pythonmathtrigonometrycurve

How to write a spirial function in python?


I'm trying to write a function in python that takes two arguments (x,y), and returns an angle in degrees in a spiraling direction.

Suppose the center of the spiral is at location (x0,y0). Then given (0,0), it returns 45. Given some other point say (0,90) which is the second intersection from the top on the y axis, the angle is around 170. For any point not touching the red line, it should return an angle of what you would expect the direction to be. The spiral is just a general thing to show the direction of the angles.

Does anyone know how to write such a function?

Thanks

enter image description here enter image description here


Solution

  • That's an Archimedean spiral curve. As the page says in polar coordinates the formula of the curve is r = aθ, usually the scalar a is 1 i.e. r = θ. Polar to cartesian conversion is

    x = r cos θ, y = r sin θ
    

    Hence

    x = θ cos θ, y = θ sin θ
    

    Varying θ from 0 to 6π would give the curve you've. When you vary the parameter θ and get x and y values what you get would be relative to the origin (0, 0). For your case, you've to translate i.e. move the points by the x and y offset to position it accordingly. If you want a bigger version of the same curve you've to scale (before translation) i.e. multiply both x and y values by a constant scalar.