我有一个存储2D点的arrayList。将这些点连接在一起时,它们代表一条路线。我想找到哪些点在同一条直线上(共线),以便我知道在哪些点上发生转弯/拐角。一个例子是:
ArrayList<Point> positions = new ArrayList<Point>();
positions.add(Point(0,0));
positions.add(Point(0,1));
positions.add(Point(0,2));
positions.add(Point(1,2));
positions.add(Point(2,2));
positions.add(Point(3,3));
示例图片:
我想知道在此示例中,点C
,D
,E
和G
是发生“转弯”的地方,而点{{ 1}},A,B,C
,C,D
,D,E
和E,F,G
是共线的。
我的想法是首先采样三个点并检查斜率。如果斜率不匹配,我知道第三个点与前两个点不共线。如果是这样,我知道它们是共线的,我会检查更多点,直到斜率不匹配为止。然后,我知道第一行结束,第二行开始以及转弯发生的位置。我重复这个过程。但是我还没有实现该想法的想法。我真的很感谢您的帮助。
编辑:我只有整数坐标,并且两个连续的点的Chebyshev距离始终为1。这要感谢@ Marco13。
答案 0 :(得分:1)
通过计算列表中每个相邻对的斜率变化
您可以获得发生“转弯”的点。
Java可以通过将斜率计算为“ Infinity ”,
来处理垂直线,因此不必担心。
public static void main(String[] args) {
ArrayList<Point> positions = new ArrayList<Point>();
positions.add(new Point(1,0));
positions.add(new Point(1,1));
positions.add(new Point(1,2));
positions.add(new Point(2,2));
positions.add(new Point(3,1));
positions.add(new Point(4,1));
positions.add(new Point(5,1));
positions.add(new Point(5,2));
ArrayList<Point> turns = new ArrayList<Point>();
for (int i = 0; i < positions.size(); i++) {
turns.add(null);
}
int counter = 0;
if (positions.size() > 2) {
Point base = positions.get(0);
Point next = positions.get(1);
int x = (next.x - base.x);
double slope = 1.0 * (next.y - base.y) / (next.x - base.x);
for (int i = 2; i < positions.size(); i++) {
Point newpoint = positions.get(i);
double newslope = 1.0 * (newpoint.y - next.y) / (newpoint.x - next.x);
if (newslope != slope) {
counter++;
turns.set(i - 1, positions.get(i - 1));
slope = newslope;
}
next = newpoint;
}
}
System.out.println("Collinear points:");
for (int i = 0; i < positions.size(); i++) {
System.out.print("(" + positions.get(i).x + ", " + positions.get(i).y + ") ");
if (turns.get(i) != null) {
System.out.println();
System.out.print("(" + positions.get(i).x + ", " + positions.get(i).y + ") ");
}
}
System.out.println();
System.out.println();
if (counter > 0) {
System.out.println("Turns at these points: ");
for (Point p : turns) {
if (p != null)
System.out.print("(" + p.x + ", " + p.y + ") ");
}
} else {
System.out.println("There are no turns!");
}
}
将打印:
Collinear points:
(1, 0) (1, 1) (1, 2)
(1, 2) (2, 2)
(2, 2) (3, 1)
(3, 1) (4, 1) (5, 1)
(5, 1) (5, 2)
Turns at these points:
(1, 2) (2, 2) (3, 1) (5, 1)
答案 1 :(得分:1)
当涉及垂直线时,您应该不计算斜率。
根据图像和描述,我假设您只有整数坐标,并且两个连续点的Chebyshev distance始终为1。(如果不是这种情况,则应编辑答案,然后包括更多信息)。
然后,当
时,点(x i ,y i )是转折点我个人建议计算列表中这些点的 indices ,因为这样做有几个优点:
这是在此处实现的示例:
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TurningPoints
{
public static void main(String[] args)
{
List<Point> points = new ArrayList<Point>();
points.add(createPoint("A", 1, 0));
points.add(createPoint("B", 1, 1));
points.add(createPoint("C", 1, 2));
points.add(createPoint("D", 2, 2));
points.add(createPoint("E", 3, 1));
points.add(createPoint("F", 4, 1));
points.add(createPoint("G", 5, 1));
points.add(createPoint("H", 5, 2));
List<Integer> indices = computeTurningPointIndices(points);
System.out.println("Turning points are at " + indices);
List<Point> turningPoints = indices.stream().map(i -> points.get(i))
.collect(Collectors.toList());
System.out.println("They are " + turningPoints);
System.out.println("Collinear:");
indices.add(0, 0);
indices.add(points.size() - 1);
for (int i = 0; i < indices.size() - 1; i++)
{
int i0 = indices.get(i);
int i1 = indices.get(i + 1);
List<Point> collinear = points.subList(i0, i1 + 1);
System.out.println(" " + collinear);
}
}
private static List<Integer> computeTurningPointIndices(List<Point> points)
{
List<Integer> indices = new ArrayList<Integer>();
for (int i = 1; i < points.size() - 1; i++)
{
Point prev = points.get(i - 1);
Point curr = points.get(i);
Point next = points.get(i + 1);
int dxPrev = prev.x - curr.x;
int dyPrev = prev.y - curr.y;
int dxNext = next.x - curr.x;
int dyNext = next.y - curr.y;
if (dxPrev != dxNext && dyPrev != dyNext)
{
indices.add(i);
}
}
return indices;
}
private static Point createPoint(String name, int x, int y)
{
// Only for this example. You should usually not do this!
return new Point(x, y)
{
@Override
public String toString()
{
return name + "(" + x + "," + y + ")";
}
};
}
}
输出为
Turning points are at [2, 3, 4, 6]
They are [C(1,2), D(2,2), E(3,1), G(5,1)]
Collinear:
[A(1,0), B(1,1), C(1,2)]
[C(1,2), D(2,2)]
[D(2,2), E(3,1)]
[E(3,1), F(4,1), G(5,1)]
[G(5,1), H(5,2)]