【问题标题】:Create polygon with a certain radius创建具有一定半径的多边形
【发布时间】:2011-03-07 05:22:16
【问题描述】:

我有一个具有一定半径的多边形。像this 这样的东西。现在我需要

  • 将其插入mysql数据库。
  • 找出一个点是位于多边形内部还是外部。 (是否包括获取它的顶点?)

如何做到这一点?

【问题讨论】:

  • 这跟php或mysql有什么关系?
  • 多边形数据必须存储在mysql数据库中,并且应该使用php检索相同的数据,以获取顶点并查找其中是否存在点。我为我的问题添加了更多细节。
  • 多边形是规则的还是不规则的,并且没有顶点(即常量或变量)?
  • 更多的顶点,更麻烦,而且你需要事先知道多边形的方向才能检查多边形的点

标签: php mysql


【解决方案1】:

分类点需要完全定义多边形。通常,您将需要围绕多边形按顺序排列的顶点,或者需要一些完全定义多边形的约束(例如:规则、以原点为中心、+x 轴上有一个顶点,以及给定数量的边)。如果多边形是自相交的,那么在这种情况下,您还需要定义“内部”和“外部”的含义(有几个不等价的定义)。

编辑:如果你用谷歌搜索“php 多边形”,你会发现很多用于多边形点测试的代码(例如,here,尽管我不保证正确性该代码)。

【讨论】:

  • 谢谢,但是如果我只知道多边形的半径,我想知道如何在多边形中找到一个点。多边形以原点为中心。
  • 即使多边形以原点为中心并且是正多边形(所有边和角度都相等),这还不够。考虑一个半径为 1 的正方形。如果对角线与 X 轴和 Y 轴对齐,则点 (.75, 0) 在正方形内;如果对角线是 45 度,则不是。
  • 好的,听起来我需要多边形点来确定一个点是否在多边形内。如果我有多边形的半径,我至少可以找到顶点吗?
  • @dskanth - 不!您需要知道多边形如何围绕原点旋转。再想想正方形的例子。
  • 多边形围绕原点逆时针旋转。例如,我有一个原点中心 (10,10),多边形的半径为 5,多边形有 5 个边(顶点)。
【解决方案2】:

查找,如果该点位于多边形内,则将在更新中完成

//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 个顶点。
【解决方案3】:
<?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);
} ?>

这个有效。但说真的,你难道没有发现语法错误吗!

【讨论】:

  • 抱歉,当我尝试使用时得到一个空值:echo point_in_reg_poly(4, 5, 10, 10);
  • 我已经有一段时间没有用 PHP 编码了。模 % 运算符可能对浮点数无效。不过,最有可能的是,PI 不是一个定义的常数。将其视为 php-esque 伪代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 2021-10-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多