2011-03-07 83 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

相关问题