Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through a formula that describes a spiral to generate XY coordinates

I'm trying to generate a spiral galaxy in the form of xy (2D) coordinates -- but math is not my strong suit.

I've gleaned the following from an excellent source on spirals:

The radius r(t) and the angle t are proportional for the simpliest spiral, the spiral of Archimedes. Therefore the equation is:

(3) Polar equation: r(t) = at [a is constant].
From this follows
(2) Parameter form: x(t) = at cos(t), y(t) = at sin(t),
(1) Central equation: x²+y² = a²[arc tan (y/x)]².

This question sort of touched upon galaxy generation, but the responses were scattered and still overly complex for what I need (aka, my math-dumb mind can't understand them).

Essentially, what I need to do is loop through a spiral formula in PHP ~5000 times to generate points on a 513x513 XY grid. The size of the grid and the number of points needed may change in the future. Even better would be to weigh those points towards the origin of the spirals both in frequency and how far they can stray from the exact mathematical formula, similarly to how a galaxy actually looks.

This mathematical paper talks about a formula that describes the structure of spiral galaxies.

What completely loses me is how to translate a mathematical formula to something I can loop through in PHP!

like image 744
Nick Avatar asked Jun 03 '12 23:06

Nick


People also ask

How do you find the coordinates of a spiral?

In particular, d(P,O)=r, and θ is the second coordinate. Therefore the equation for the spiral becomes r=kθ. Note that when θ=0 we also have r=0, so the spiral emanates from the origin.

What is the equation of a spiral?

The general equation of the logarithmic spiral is r = aeθcotb, in which r is the radius of each turn of the spiral, a and b are constants that depend on the particular spiral, θ is the angle of rotation as the curve spirals, and e is the base of the natural logarithm.

What is a spiral mathematically?

In mathematics, a spiral is a curve which emanates from a point, moving farther away as it revolves around the point.

Is logarithmic spiral the same as golden ratio?

In geometry, a golden spiral is a logarithmic spiral whose growth factor is φ, the golden ratio. That is, a golden spiral gets wider (or further from its origin) by a factor of φ for every quarter turn it makes.


1 Answers

// a is 5 here
function x($t){ return 5 * $t * cos($t); }
function y($t){ return 5 * $t * sin($t); }

for ($t = 0; $t < 50; $t += 0.01) {
    $xyPoint = array(x($t), y($t));
    // draw it
}

when you encounter parametric equations like this, its common for the parameter variable to be t, which means time. So you could think of plugging increasing values of t into the functions, and getting coordinates which gradually change as elapsed time increases.

you'll need to choose your own values for a, the range of t, and the increment step size of t. It just depends on your requirements. both cos() and sin() have a max value of 1, if that helps you figure out suitable values for a and t depending on your canvas size

like image 78
goat Avatar answered Sep 28 '22 14:09

goat