How to get the coordinates of several points on the circle with php

前端 未结 2 419
有刺的猬
有刺的猬 2021-01-16 06:00

I have a few points. I need to put those points on a circle and get their coordinates.

function positionX($numItems,$thisNum){ 
  $alpha = 360/$numItems; //          


        
2条回答
  •  伪装坚强ぢ
    2021-01-16 06:54

    The cos() and sin() functions expect the argument in radians, not in degrees.

    Use the deg2rad() function to convert

    EDIT

    CODE:

    function positionX($numItems,$thisNum){
      $alpha = 360/$numItems; // angle between the elements
      $r = 1000; // radius
      $angle = $alpha * $thisNum; // angle for N element
      $x = $r * cos(deg2rad($angle)); // X coordinates
      return $x;
    }
    
    function positionY($numItems,$thisNum){
      $alpha = 360/$numItems; // angle between the elements
      $r = 1000; // radius
      $angle = $alpha * $thisNum; // angle for N element
      $y = $r * sin(deg2rad($angle)); // Y coordinates
      return $y;
    }
    
    echo round(positionX(4,1))."
    "; echo round(positionY(4,1))."

    "; echo round(positionX(4,2))."
    "; echo round(positionY(4,2))."

    "; echo round(positionX(4,3))."
    "; echo round(positionY(4,3))."

    "; echo round(positionX(4,4))."
    "; echo round(positionY(4,4))."

    ";

    RESULTS:

    0
    1000
    
    -1000
    0
    
    -0
    -1000
    
    1000
    -0
    

提交回复
热议问题