我目前正在尝试汇总一种算法,该算法可以知道平面中两个定义的点之间是否存在障碍物。
从图像中可以看到,从原点可以访问点1、2、3和6。点4和5不是。您穿过多边形。
我正在使用的代码如下。 pStartPoint和pEndPoint是从原点到相关点的线。该功能检查所有边缘,以查看直线是否穿过边缘。
public double GetSlopeOfLine(Point a, Point b){
double x = b.y - a.y;
double y = b.x - a.x;
return (x / y);
}
public double GetOffsetOfLine(double x, double y, double slope){
return (y - (slope * x));
}
public boolean IsPointAccessable(Point pStartPoint, Point pEndPoint){
//Define the equation of the line for these points. Once we have slope and offset the equation is
//y = slope * x + offset;
double slopeOfLine = GetSlopeOfLine(pStartPoint, pEndPoint);
double offSet = GetOffsetOfLine(pStartPoint.x, pStartPoint.y, slopeOfLine);
//Collision detection for each side of each obstacle. Once we get the point of collision, does it lie on the
//line in between the two points? If so, collision, and I can't reach that point yet.
for (Iterator<Obstacles> ObstacleIt = AdjustedObstaclesList.iterator(); ObstacleIt.hasNext();) {
Obstacles pObstacle = ObstacleIt.next();
int NumberOfEdges = pObstacle.getPoints().size();
for(int i=0; i<NumberOfEdges; i++){
//Get Edge[i];
int index = i;
Point pFirstPoint = (Point)pObstacle.getPoints().get(index);
if(i >= NumberOfEdges - 1)
index = 0;
else
index = i+1;
Point pNextPoint = (Point)pObstacle.getPoints().get(index);
double slopeOfEdge = GetSlopeOfLine(pFirstPoint, pNextPoint);
double offsetEdge = GetOffsetOfLine(pNextPoint.x, pNextPoint.y, slopeOfEdge);
int x = Math.round((float) ((-offSet + offsetEdge) / (slopeOfLine - slopeOfEdge)));
int y = Math.round((float) ((slopeOfLine * x) + offSet));
//If it lies on either point I could be looking at two adjacent points. I can still reach that point.
if(x > pStartPoint.x && x < pEndPoint.x && y > pStartPoint.y && y < pEndPoint.y &&
x > pFirstPoint.x && x < pNextPoint.x && y > pFirstPoint.y && y < pNextPoint.y){
return false;
}
}
}
return true;
}
如果线通过并且在pStartPoint和pEndPoint之间找到了线的交点,则我认为无法到达pEndPoint。
此功能不起作用,我想知道它是否与原点不在左下角而是在左上角以及我的窗口(宽度,高度)位于底部的事实有关对。因此,坐标平面被弄乱了。
我的思想必须糊涂,因为我无法思考如何对此进行调整,如果那确实是我的错误,因为我似乎无法解决该错误。我以为将斜率和偏移量乘以-1可能是解决方案,但这似乎行不通。
我的解决方案正确吗?检查相交点时,我的代码看起来是否正确?是否有更好的解决方案来查看某个点是否可访问。
此后还将进行下一步,一旦我确定现在在多边形的某个点上,便确定可以访问哪些点。例如,从点1开始,哪些点可以进入而无需进入多边形?
答案 0 :(得分:2)
首先,我想说的是,将坡度用于此类任务是可行的,但由于坡度非常不稳定,因为坡度可以从负无穷大变为无穷大点的变化很小。这是一个略有不同的算法,它依赖于 angles 而不是斜率。使用此功能的另一个优点是坐标系在这里并不重要。就像这样(我尽可能地重用了您现有的代码):
public boolean IsPointAccessable(Point pStartPoint, Point pEndPoint) {
//Collision detection for each side of each obstacle. Once we get the point of collision, does it lie on the
//line in between the two points? If so, collision, and I can't reach that point yet.
for (Iterator<Obstacles> ObstacleIt = AdjustedObstaclesList.iterator(); ObstacleIt.hasNext();) {
Obstacles pObstacle = ObstacleIt.next();
int NumberOfEdges = pObstacle.getPoints().size();
for(int i=0; i<NumberOfEdges; i++){
//Get Edge[i];
int index = i;
Point pFirstPoint = (Point)pObstacle.getPoints().get(index);
if(i >= NumberOfEdges - 1)
index = 0;
else
index = i+1;
Point pNextPoint = (Point)pObstacle.getPoints().get(index);
// Here is where we get a bunch of angles that encode in them important info on
// the problem we are trying to solve.
double angleWithStart = getAngle(pNextPoint, pFirstPoint, pStartPoint);
double angleWithEnd = getAngle(pNextPoint, pFirstPoint, pEndPoint);
double angleWithFirst = getAngle(pStartPoint, pEndPoint, pFirstPoint);
double angleWithNext = getAngle(pStartPoint, pEndPoint, pNextPoint);
// We have accumulated all the necessary angles, now we must decide what they mean.
// If the 'start' and 'end' angles are different signs, then the first and next points
// between them. However, for a point to be inaccessible, it also must be the case that
// the 'first' and 'next' angles are opposite sides, as then the start and end points
// Are between them so a blocking occurs. We check for that here using a creative approach
// This is a creative way of checking if two numbers are different signs.
if (angleWithStart * angleWithEnd <= 0 && angleWithFirst * angleWithNext <= 0) {
return false;
}
}
}
return true;
}
现在,剩下要做的就是找到一种方法来计算由三个点形成的正负角。快速的Google搜索产生了这种方法(来自this的问题):
private double getAngle(Point previous, Point center, Point next) {
return Math.toDegrees(Math.atan2(center.x - next.x, center.y - next.y)-
Math.atan2(previous.x- center.x,previous.y- center.y));
}
现在,此方法应在理论上可行(我正在测试以确保可以发现任何角度迹象或类似问题的答案,我会对其进行编辑)。希望您能理解,我的评论可以很好地解释代码,但是如果您希望我进一步阐述,请留下评论/问题。如果您不了解算法本身,我建议您拿出一张纸,然后按照算法进行操作,以了解到底发生了什么。希望这会有所帮助!
编辑:为了希望有助于更好地理解使用角度的解决方案,我以start
,end
,{{1 }}和first
的方向,并将其附加到此问题上。对不起,我很快就画了出来,但是从理论上讲这应该使思路更清晰。
答案 1 :(得分:0)
如果您的段数较少(例如,您的示例仅显示了三个形状的12个段,我们知道可以忽略其中的两个形状(由于边界框检查),那么我建议您仅执行行/行交集检查。
Point s = your selected point;
ArrayList<Point> points = polygon.getPoints();
ArrayList<Edge> edges = polygon.getEdges();
for(Point p: points) {
Line l = new Line(s, p);
for(Edge e: edges) {
Point i = e.intersects(l);
if (i != null) {
System.out.println("collision", i.toString());
}
}
}
使用非常简单的intersects
方法:
Point intersects(Line l) {
// boring variable aliassing:
double x1 = this.p1.x,
y1 = this.p1.y,
x2 = this.p2.x,
y2 = this.p2.y,
x3 = l.p1.x,
y2 = l.p1.y,
x3 = l.p2.x,
y2 = l.p2.y,
// actual line intersection algebra:
nx = (x1 * y2 - y1 * x2) * (x3 - x4) -
(x1 - x2) * (x3 * y4 - y3 * x4),
ny = (x1 * y2 - y1 * x2) * (y3 - y4) -
(y1 - y2) * (x3 * y4 - y3 * x4),
d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (d == 0) return null;
return new Point(nx/d, ny/d);
}