查找Android中路径中包含的点

Sta*_*ovy 9 java android contains path

他们是否有理由决定不在Android中添加contains方法(针对Path)?

我想知道我在Path中有什么观点,并希望它比在这里看到的更容易:

如何判断闭合路径是否包含给定点?

我是否更好地创建一个ArrayList并将整数添加到数组中?(我只在控制声明中检查一次点数)即.if(myPath.contains(x,y)

到目前为止,我的选择是:

  • 使用区域
  • 使用ArrayList
  • 扩展课程
  • 你的建议

我只是在寻找最有效的方法

the*_*enp 14

我不久前遇到了同样的问题,经过一番搜索,我发现这是最好的解决方案.

Java有一个Polygon类,其contains()方法可以使事情变得非常简单.不幸的java.awt.Polygon是,Android不支持该类.但是,我找到了一个写同等课程的人.

我认为你不能从Android Path类中得到构成路径的各个点,因此你必须以不同的方式存储数据.

该类使用交叉数算法来确定该点是否在给定的点列表内.

/**
 * Minimum Polygon class for Android.
 */
public class Polygon
{
    // Polygon coodinates.
    private int[] polyY, polyX;

    // Number of sides in the polygon.
    private int polySides;

    /**
     * Default constructor.
     * @param px Polygon y coods.
     * @param py Polygon x coods.
     * @param ps Polygon sides count.
     */
    public Polygon( int[] px, int[] py, int ps )
    {
        polyX = px;
        polyY = py;
        polySides = ps;
    }

    /**
     * Checks if the Polygon contains a point.
     * @see "http://alienryderflex.com/polygon/"
     * @param x Point horizontal pos.
     * @param y Point vertical pos.
     * @return Point is in Poly flag.
     */
    public boolean contains( int x, int y )
    {
        boolean oddTransitions = false;
        for( int i = 0, j = polySides -1; i < polySides; j = i++ )
        {
            if( ( polyY[ i ] < y && polyY[ j ] >= y ) || ( polyY[ j ] < y && polyY[ i ] >= y ) )
            {
                if( polyX[ i ] + ( y - polyY[ i ] ) / ( polyY[ j ] - polyY[ i ] ) * ( polyX[ j ] - polyX[ i ] ) < x )
                {
                    oddTransitions = !oddTransitions;          
                }
            }
        }
        return oddTransitions;
    }  
}
Run Code Online (Sandbox Code Playgroud)


Han*_*ans 7

尝试了另一个答案,但它给我的案子带来了错误的结果.没有找到确切的原因,但我自己直接从算法翻译:http: //www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

现在代码为:

/**
 * Minimum Polygon class for Android.
 */
public class Polygon
{
    // Polygon coodinates.
    private int[] polyY, polyX;

    // Number of sides in the polygon.
    private int polySides;

    /**
     * Default constructor.
     * @param px Polygon y coods.
     * @param py Polygon x coods.
     * @param ps Polygon sides count.
     */
    public Polygon( int[] px, int[] py, int ps )
    {
        polyX = px;
        polyY = py;
        polySides = ps;
    }

    /**
     * Checks if the Polygon contains a point.
     * @see "http://alienryderflex.com/polygon/"
     * @param x Point horizontal pos.
     * @param y Point vertical pos.
     * @return Point is in Poly flag.
     */
    public boolean contains( int x, int y )
    {
        boolean c = false;
        int i, j = 0;
        for (i = 0, j = polySides - 1; i < polySides; j = i++) {
            if (((polyY[i] > y) != (polyY[j] > y))
                && (x < (polyX[j] - polyX[i]) * (y - polyY[i]) / (polyY[j] - polyY[i]) + polyX[i]))
            c = !c;
        }
        return c;
    }  
}
Run Code Online (Sandbox Code Playgroud)


Seb*_*ian 7

我想评论一下@theisenp的答案:代码有整数数组,如果你看一下算法描述网页,它会警告不要使用整数而不是浮点数.

我复制了你上面的代码,它似乎工作得很好,除了一些角落的情况,当我制作的线条并没有很好地连接到自己.

通过将所有内容更改为浮点数,我摆脱了这个错误.