1

我有几点。我需要将这些点放在一个圆圈上并获取它们的坐标。

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;
}

但是我的代码不起作用..这些函数会产生奇怪的坐标。

图片示例:http ://cl.ly/image/453E2w1Y0w0d

升级版:

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 - 所有元素;1,2,3,4 - 元素数。

这些代码给了我结果:

-448.073616129
893.996663601

-598.460069058
0

984.381950633
-176.045946471

-283.691091487
958.915723414

在圈子上它不起作用。

4

2 回答 2

2

那是因为您没有在 sin() 和 cos() 函数中使用辐射。你需要把天使变成光芒四射。看看 sin() 的函数描述,你会发现 arg 是在 radiants 中。

提醒

1° = 2 PI / 360;

编辑

我似乎在你的代码中找不到错误,试试这个

function($radius, $points, $pointToFind) {

 $angle = 360 / $points * 2 * pi(); //angle in radiants

 $x = $radius * cos($angle * $pointToFind);
 $y = $radius * sin($angle * $pointToFind);

}
于 2012-11-23T14:05:38.253 回答
2

cos() 和 sin() 函数需要以弧度而不是度数为单位的参数。

使用deg2rad()函数进行转换

编辑

代码:

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>";

结果:

0
1000

-1000
0

-0
-1000

1000
-0
于 2012-11-23T14:05:41.593 回答