答案 0 :(得分:2)
对点进行分类需要完全定义多边形。通常,您需要在多边形周围按顺序排列顶点,或者完全定义多边形的一些约束(例如:常规,以原点为中心,+ x轴上有一个顶点,以及给定数量的边)。如果多边形是自相交的,那么在这种情况下你还需要定义“内部”和“外部”的含义(有几个非等价的定义)。
编辑:如果你谷歌“php polygon”你会发现许多代码用于多边形点测试(here,例如,虽然我不保证该代码的正确性。)
答案 1 :(得分: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);
} ?>
这个有效。但实际上,你不能发现语法错误!
答案 2 :(得分: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();
}