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

半腔热情 提交于 2019-12-20 12:25:49

问题


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!


回答1:


// 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



来源:https://stackoverflow.com/questions/10874678/looping-through-a-formula-that-describes-a-spiral-to-generate-xy-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!