i have an object with Vectors describing an polygon. It looks something like that:
Now i´m looking for an effective algorithm to remove the unnecessary vectors to get something like that:
Does anyone know an good way to do this?
i have an object with Vectors describing an polygon. It looks something like that:
Now i´m looking for an effective algorithm to remove the unnecessary vectors to get something like that:
Does anyone know an good way to do this?
After some further research I wrote the following code as solution:
public void optimize() {
if (vectors.size() < 3)
return;
for (int i = 2; i < vectors.size(); i++) {
if (sameLine(vectors.get(i - 2),vectors.get(i - 1), vectors.get(i)))
vectors.remove(--i);
}
}
boolean sameLine(Point a, Point b , Point c) {
if ((b.x < a.x && b.x < c.x)|| (b.x > c.x && b.x > a.x) || (b.y < a.y && b.y < c.y)|| (b.y > c.y && b.y > a.y))
return false;
return (a.y - b.y) * (a.x - c.x) == (a.y - c.y) * (a.x - b.x);
}
vector a ArrayList of point objects.