在Android中查找路径中包含的点 [英] Finding Points contained in a Path in Android

查看:33
本文介绍了在Android中查找路径中包含的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们决定不在 Android 中添加 contains 方法(用于 Path)是否有原因?

Is there a reason that they decided not to add the contains method (for Path) in Android?

我想知道我在路径中有哪些点,并希望它比这里看到的更容易:

I'm wanting to know what points I have in a Path and hoped it was easier than seen here:

我怎么知道闭合路径包含给定点?

创建一个 ArrayList 并将整数添加到数组中对我来说会更好吗?(我只在控制语句中检查一次点)即.if(myPath.contains(x,y)

Would it be better for me to create an ArrayList and add the integers into the array? (I only check the points once in a control statement) Ie. if(myPath.contains(x,y)

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

So far my options are:

  • 使用区域
  • 使用 ArrayList
  • 拓展课程
  • 您的建议

我只是在寻找最有效的方法来解决这个问题

I'm just looking for the most efficient way I should go about this

推荐答案

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

I came up against this same problem a little while ago, and after some searching, I found this to be the best solution.

Java 有一个 Polygon 类和一个 contains() 方法,这将使事情变得非常简单.不幸的是,Android 不支持 java.awt.Polygon 类.但是,我找到了写 等效类.

Java has a Polygon class with a contains() method that would make things really simple. Unfortunately, the java.awt.Polygonclass is not supported in Android. However, I was able to find someone who wrote an equivalent class.

我认为您无法从 Android Path 类中获取构成路径的各个点,因此您必须以不同的方式存储数据.

I don't think you can get the individual points that make up the path from the Android Path class, so you will have to store the data in a different way.

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

The class uses a Crossing Number algorithm to determine whether or not the point is inside of the given list of points.

/**
 * 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;
    }  
}

这篇关于在Android中查找路径中包含的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆