回答
查找,如果該點位於多邊形與否,將在更新完成內部
//this assumes that the orientation of your polygon is
//http://en.wikipedia.org/wiki/File:Pentagon.svg
$pass=1;
function filter($init, $final, $center)
{
if(($final['a']['x']-$init['x'])*($center['y']-$init['y'])-($final['a']['y'] - $init['y'])*($center['x']-$init['x']) > 0)
return $final['a'];
else
return $final['b'];
}
function getNextPoint($init, $center, $distance, $slope)
{
global $pass;
$final['a']['x'] = $init['x']+$distance/sqrt(1+tan($slope)*tan($slope));
$final['a']['y'] = $init['y']+(tan($slope)*$distance)/sqrt(1+tan($slope)*tan($slope));
$final['b']['x'] = $init['x']-$distance/sqrt(1+tan($slope)*tan($slope));
$final['b']['y'] = $init['y']-(tan($slope)*$distance)/sqrt(1+tan($slope)*tan($slope));
echo "<br/><br/>";
echo "Pass: $pass <br/>";
echo "Slope: ".$slope."<br/>";
if($pass == 1){
$point = $final['b'];
$distance = $distance*2*sin(pi()/5);
$slope = 0;
}
else{
$point = filter($init, $final, $center);
$slope = $slope+pi()/2.5;
}
echo "Position: ";
print_r($point);
echo "<br/>";
echo "Distance : ".distance($init['x'], $init['y'], $point['x'], $point['y']);
if($pass == 7){
return $point;
}
else{
//echo "x: ".($point['x'])." y: ".($point['y'])." <br/>";
$pass++;
getNextPoint($point, $center, $distance, $slope);
}
//echo "x: ".($point['x'])." y: ".($point['y'])." <br/>";
}
function polygon($vertices=5, $centerX=10, $centerY=10, $radius=5)
{
$internalangle = ($vertices-2)*pi()/$vertices;
$slope = pi()+($internalangle)/2;
$init['x'] = 10;
$init['y'] = 10;
getNextPoint($init, $init, 5, $slope);
}
polygon();
/*
function getx($slope, $x1, $y1, $y)
{
return (($y-$y1)/$slope+$x1);
}
function gety($slope, $x1, $y1, $x)
{
return ($slope*($x-$x1)+$y1);
}
*/
function distance($initx, $inity, $finalx, $finaly)
{
return sqrt(($initx-$finalx)*($initx-$finalx)+($inity-$finaly)*($inity-$finaly));
}
function getslope($final, $init)
{
return atan(($final['y']-$init['y'])/($final['x']-$init['x']))*180/pi();
}
這似乎適用於5頂點(五角形)的正多邊形。還沒有嘗試過4或6個頂點。 – 2011-03-09 05:48:45
分類點要求完全定義多邊形。通常情況下,您需要圍繞多邊形排列頂點,否則需要一些約束來完全定義多邊形(例如:規則,以原點爲中心,+ x軸上有一個頂點,以及給定數量的邊)。如果多邊形是自相交的,那麼在這種情況下,還需要定義「內部」和「外部」的含義(有幾個非等價的定義)。
編輯:如果谷歌「的PHP多邊形」你會發現很多的代碼點在多邊形測試(here,例如,雖然我不敢保證代碼的正確性)。
謝謝,但我想知道如何才能找到一個點在多邊形,如果我只知道多邊形的半徑。多邊形以原點爲中心。 – 2011-03-07 06:18:52
你不能 - 你需要有多邊形點和直線 – fazo 2011-03-07 06:24:35
即使多邊形以原點爲中心並且是正多邊形(所有邊和角都相等),這還不夠。考慮一個半徑爲1的正方形。如果對角線與X軸和Y軸對齊,則點(.75,0)位於正方形內;如果對角線在45度,那麼它不是。 – 2011-03-07 07:29:35
<?php
function point_in_reg_poly($sides, $radius, $p_x, $p_y) {
$centerAngle = 2*pi()/$sides;
$internalRadius = $radius * sin($centerAngle/2);
$angle = atan2($p_x, $p_y);
$length = sqrt($p_x*$p_x + $p_y*$p_y);
if($length > $radius)
return false;
//normalize angle to angle from center of nearest segment
$angle = fmod($angle + 2*pi(), $centerAngle) - $centerAngle/2;
return $internalRadius > $length * cos($angle);
} ?>
這一個工程。但是,真的,你難道沒有發現語法錯誤!
對不起,但我得到了一個空值,當我嘗試:**回聲point_in_reg_poly(4,5,10,10); ** – 2011-03-07 09:12:01
它已經有一段時間了我用PHP編碼。模數'%'運算符可能對浮點數無效。然而,很可能,PI不是一個定義的常量。將其視爲php-esque僞代碼 – Eric 2011-03-07 19:37:03
- 1. 從中心和半徑創建一個SqlGeography多邊形圈
- 2. 多邊形與半徑的折線?
- 3. CSS3 - 如何在矩形上創建圓形邊框半徑?
- 4. 在多個其他多邊形的某個半徑內查找多邊形
- 5. 爲div創建邊框半徑
- 6. 創建多邊形
- 7. 從獨立線創建的形狀創建一個多邊形
- 8. 如何創建一個隨機邊界半徑?
- 9. JQuery的css邊框半徑特定的角半徑?
- 10. 用不同半徑的圓覆蓋多邊形
- 11. 帶邊框半徑的多線圓角
- 12. CSS邊框RADIUS,這是使用正確的樣式?邊界半徑? -webkit-邊界半徑? -moz-邊界半徑?
- 13. IE8邊界半徑
- 14. CSS邊界半徑
- 15. 邊界半徑Nativescript
- 16. CSS3邊框半徑
- 17. CSS - 邊框半徑
- 18. ie9邊界半徑
- 19. 邊界半徑CSS
- 20. CSS邊界半徑
- 21. Java:使用創建的多邊形創建星形
- 22. 如何從多邊形表創建多邊形
- 23. 是否有可能僅使用邊框半徑創建此形狀?
- 24. 特定的邊框半徑CSS
- 25. CSS:創建具有邊界半徑的時鐘
- 26. R - 聚合縣地圖多邊形創建自定義邊框
- 27. 創建用點填充的多邊形
- 28. 創建封閉的空間多邊形
- 29. 六邊形+邊框半徑+背景圖像,我該怎麼做?
- 30. 創建箭頭:在插入邊框半徑後
這是什麼都用PHP或MySQL怎麼辦? – 2011-03-07 05:25:59
多邊形數據必須存儲在mysql數據庫中,並且應該使用php檢索相同的數據,以獲取頂點並查找點是否位於其中。我在我的問題中增加了更多細節。 – 2011-03-07 05:27:45
http://en.wikipedia.org/wiki/Point_in_polygon – Jacob 2011-03-07 05:31:00