2011-03-07 122 views
2

我有一個半徑多邊形。類似於this。現在我需要創建一定半徑的多邊形

  • 插入到mysql數據庫。
  • 找到一個點是位於多邊形的內部還是外部。 (它包括獲取它的頂點?)

這怎麼能實現?

+0

這是什麼都用PHP或MySQL怎麼辦? – 2011-03-07 05:25:59

+0

多邊形數據必須存儲在mysql數據庫中,並且應該使用php檢索相同的數據,以獲取頂點並查找點是否位於其中。我在我的問題中增加了更多細節。 – 2011-03-07 05:27:45

+1

http://en.wikipedia.org/wiki/Point_in_polygon – Jacob 2011-03-07 05:31:00

回答

1

查找,如果該點位於多邊形與否,將在更新完成內部

//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(); 
} 
+0

這似乎適用於5頂點(五角形)的正多邊形。還沒有嘗試過4或6個頂點。 – 2011-03-09 05:48:45

2

分類點要求完全定義多邊形。通常情況下,您需要圍繞多邊形排列頂點,否則需要一些約束來完全定義多邊形(例如:規則,以原點爲中心,+ x軸上有一個頂點,以及給定數量的邊)。如果多邊形是自相交的,那麼在這種情況下,還需要定義「內部」和「外部」的含義(有幾個非等價的定義)。

編輯:如果谷歌「的PHP多邊形」你會發現很多的代碼點在多邊形測試(here,例如,雖然我不敢保證代碼的正確性)。

+0

謝謝,但我想知道如何才能找到一個點在多邊形,如果我只知道多邊形的半徑。多邊形以原點爲中心。 – 2011-03-07 06:18:52

+1

你不能 - 你需要有多邊形點和直線 – fazo 2011-03-07 06:24:35

+1

即使多邊形以原點爲中心並且是正多邊形(所有邊和角都相等),這還不夠。考慮一個半徑爲1的正方形。如果對角線與X軸和Y軸對齊,則點(.75,0)位於正方形內;如果對角線在45度,那麼它不是。 – 2011-03-07 07:29:35

1
<?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); 
} ?> 

這一個工程。但是,真的,你難道沒有發現語法錯誤!

+0

對不起,但我得到了一個空值,當我嘗試:**回聲point_in_reg_poly(4,5,10,10); ** – 2011-03-07 09:12:01

+0

它已經有一段時間了我用PHP編碼。模數'%'運算符可能對浮點數無效。然而,很可能,PI不是一個定義的常量。將其視爲php-esque僞代碼 – Eric 2011-03-07 19:37:03