1

i want to get the coordinates of n points on a circle with the diameter of x

i tried this:

<?php
header("Content-Type: image/png");
$img = @imagecreate(900, 900)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
for($i=0;$i<=360;$i+=10)
{
$x = round(cos($i)*400)+450;
$y = round(sin($i)*400)+450;
imagefilledellipse($img, $x, $y, 3, 3, $red);
}
imagepng($img);
imagedestroy($img);
?>

but there seems to be an error:

https://i.stack.imgur.com/x8VYn.jpg

school and math and sin&cos is long ago ... so i would be happy if someone could give me a hint

roeb
  • 467
  • 8
  • 19

2 Answers2

2

Basically just use the deg2rad function. http://www.php.net/manual/en/function.deg2rad.php

So change your cos and sin calls to this:

$x = round(cos(deg2rad($i))*400)+450;
$y = round(sin(deg2rad($i))*400)+450;
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0

PHP trigonometric functions accept radian values, so you should multiply values of $i by Pi/180.

elias
  • 849
  • 13
  • 28
  • i don't know radian values ... sorry – roeb Nov 14 '13 at 12:50
  • Check out the wikipedia page for a better understanding. http://en.wikipedia.org/wiki/Radian But to cut it short, the radian value of an angle is Pi/180 times its degree value, that's why a simple multiplication solves your conversion problem. – elias Nov 14 '13 at 12:54