Reputation: 97
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; // angle between the elements
$r = 1000; // radius
$angle = $alpha * $thisNum; // angle for N element
$x = $r * cos($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($angle); // Y coordinates
return $y;
}
But my code isn't work.. These functions produce strange coordinates.
image example: http://cl.ly/image/453E2w1Y0w0d
UPD:
echo positionX(4,1)."<br>";
echo positionY(4,1)."<br><br>";
echo positionX(4,2)."<br>";
echo positionY(4,2)."<br><br>";
echo positionX(4,3)."<br>";
echo positionY(4,3)."<br><br>";
echo positionX(4,4)."<br>";
echo positionY(4,4)."<br><br>";
4 - All elements; 1,2,3,4 - Number of element.
These code gives me result:
-448.073616129
893.996663601
-598.460069058
0
984.381950633
-176.045946471
-283.691091487
958.915723414
On the circle it does not work.
Upvotes: 1
Views: 1461
Reputation: 212412
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
Upvotes: 2
Reputation: 5410
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);
}
Upvotes: 2