有没有办法,他们决定不添加含有方法(路径)在Android的理由吗?
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:
How我可以告诉大家,如果一个封闭的路径包含一个给定的点?
难道是我最好创建一个ArrayList,并添加到整数数组? (我只在控制语句检查点一次)IE浏览器。 如果(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有一个多边形
类与包含()
的方法,这将使事情真的很简单。不幸的是, java.awt.Polygon中
类没有在Android的支持。不过,我能找到谁写的equivalent类。
Java has a Polygon
class with a contains()
method that would make things really simple. Unfortunately, the java.awt.Polygon
class is not supported in Android. However, I was able to find someone who wrote an equivalent class.
我不认为你可以得到各个点构成的路径从Android 路径
类,所以你必须将数据存储在不同的方式
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;
}
}