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

后端 未结 1 749
温柔的废话
温柔的废话 2021-02-05 20:43

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 spi

1条回答
  •  故里飘歌
    2021-02-05 21:31

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

    0 讨论(0)
提交回复
热议问题