我正在尝试使用android地图做一些高级功能,为此我需要对矢量进行一些操作。现在 - 我从this读了答案,它给了我一些提示和提示。但是,有一部分我不明白。请允许我引用这个:
现在我们有了射线的开始和结束坐标,问题从“多边形内的点”变为“多边形射线与光线交叉的频率”。因此,我们不能像以前一样使用多边形点(对于边界框),现在我们需要实际的边。一方总是由两点来定义。
方1:(X1 / Y1) - (X2 / Y2)方2: (X2 / Y2) - (X3 / Y3)方3: (X3 / Y3) - (X4 / Y4)
所以我的理解是三角形的每一边实际上都是一个向量。但如何减去2分呢?假设我有一个带有3个顶点的三角形:A(1,1),B(2,2),C(1,3)。因此,根据这一点,我必须做,例如,(1,1) - (2,2),以计算其中一方。问题是如何在java / android中以编程方式执行此操作?下面我附上我已经开发的代码:
/** Creating the containers for screen
* coordinates taken from geoPoints
*/
Point point1_screen = new Point();
Point point2_screen = new Point();
Point point3_screen = new Point();
/* Project them from the map to screen */
mapView.getProjection().toPixels(point1, point1_screen);
mapView.getProjection().toPixels(point2, point2_screen);
mapView.getProjection().toPixels(point3, point3_screen);
int xA = point1_screen.x;
int yA = point1_screen.y;
int xB = point2_screen.x;
int yB = point2_screen.y;
int xC = point3_screen.x;
int yC = point3_screen.y;
int[] xPointsArray = new int[3];
int[] yPointsArray = new int[3];
xPointsArray[0] = xA;
xPointsArray[1] = xB;
xPointsArray[2] = xC;
yPointsArray[0] = yA;
yPointsArray[1] = yB;
yPointsArray[2] = yC;
Arrays.sort(xPointsArray);
int xMin = xPointsArray[0];
int yMin = yPointsArray[0];
int xMax = xPointsArray[xPointsArray.length-1];
int yMax = xPointsArray[xPointsArray.length-1];
int e = (xMax - xMin) / 100; // for ray calcultions
int width = mapView.getWidth();
int height = mapView.getHeight();
if(pPoint.x < xMin || pPoint.x > xMax || pPoint.y > yMin || pPoint.y < yMax)
{
DisplayInfoMessage(pPoint.x + " < " + xMin + " AND " + pPoint.x + " > " + xMax + " || " + pPoint.y + " < " + yMin + " AND " + pPoint.y + " > " + yMax );
// DisplayInfoMessage("Minimum is: "+ yPointsArray[0] + " and the maximum is: "+ yPointsArray[xPointsArray.length-1]);
}
else
{
GeoPoint start_point = new GeoPoint(xMin - e, pPoint.y);
Point start_point_container = new Point();
mapView.getProjection().toPixels(start_point, start_point_container);
int a, b, c, tx, ty;
int d1, d2, hd;
int ix, iy;
float r;
// calculating vector for 1st line
tx = xB - xA;
ty = yB - yA;
// equation for 1st line
a = ty;
b = tx;
c = xA*a - yA*b;
// get distances from line for line 2
d1 = a*xB + b*yB + c;
d2 = a*pPoint.x + b*pPoint.y + c;
DisplayInfoMessage("You clicked inside the triangle!" + "TRIANGLE POINTS: A("+xA+","+yA+") B("+xB+","+yB+") C("+xC+","+yC+")");
}
pPoint保存用户点击的点的坐标。我希望我能很好地解释我的问题。有人可以给我一些帮助吗?理解!
答案 0 :(得分:1)
我不是Android开发人员,但我发现android.graphics.drawable.shapes.Shape
缺少contains()
中找到的java.awt.Shape
方法。根据您引用的article的建议,您似乎必须开发自己的测试。此外,您可能希望查看crossing/winding number算法。
但如何减去2分呢?
向量的减法很好defined,在Java中很容易implemented。给定两个点作为向量,差异的分量表示连接点的线的切线(斜率)。 article中的示例通过以下行实现此目的:
//get tangent vector for line 1
tx = v1x2 - v1x1;
ty = v1y2 - v1y1;
所示方法的基础将在Line and Segment Intersections进一步讨论。