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

前端 未结 2 416
有刺的猬
有刺的猬 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:51

    Thats because you are not using radiants in your sin() and cos() functions. You need to convert angels into radiants. Look at the function description of sin(), there you find that the arg is in radiants.

    Reminder

    1° = 2 PI / 360;
    

    Edit

    I can't seem to find the error in your code, try this one instead

    function($radius, $points, $pointToFind) {
    
     $angle = 360 / $points * 2 * pi(); //angle in radiants
    
     $x = $radius * cos($angle * $pointToFind);
     $y = $radius * sin($angle * $pointToFind);
    
    }
    
    0 讨论(0)
  • 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))."<br>";
    echo round(positionY(4,1))."<br><br>";
    
    echo round(positionX(4,2))."<br>";
    echo round(positionY(4,2))."<br><br>";
    
    echo round(positionX(4,3))."<br>";
    echo round(positionY(4,3))."<br><br>";
    
    echo round(positionX(4,4))."<br>";
    echo round(positionY(4,4))."<br><br>";
    

    RESULTS:

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