Hi all,
I have a class in which I convert a list of points to a polygon (and do some other things). Before adding the points to the polygon I would like to check if it is really necessary to add it. For instance, I know point B is precisely in the middle of point A and C. Therefore, if I draw a line from point A to point C, I do not have to add point B to the polygon. So, my question is how I can check for UNnecessary points. I have thought of some dirty algorithms but I need at least 50 lines of code. I guess that can be done smarter. If anybody knows a nice, quick to process way of doing, would you tell me, please?
In the method I have an iterating loop, already. Please note that I cannot use 'for(Point p : zone)' for this loop because of other calculations in the method. I can use (the other) two ways of iterating over it, as you can see in the demo. As I am looking for the nicest and easiest way to process, I would like to use my current iterating loop.
I created a workable demo.
import java.awt.Point; import java.awt.Polygon; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class PolyCheck { PolyCheck() { List<Point> zone = new ArrayList<Point>(); zone.add(new Point(0, 0)); // Point A zone.add(new Point(1, 0)); // Point B -> UNnecessary zone.add(new Point(2, 0)); // Point C zone.add(new Point(2, 2)); // Point D zone.add(new Point(0, 2)); // Point E Polygon poly = new Polygon(); // Add points to polygon int x = 0; for (Iterator<Point> i = zone.iterator(); i.hasNext();) { i.next(); // clean up the zone // If allowed, at point to polygon poly.addPoint(zone.get(x).x, zone.get(x).y); System.out.println("Point added: " + zone.get(x)); x++; } /* * Other way of iterating * for (int x = 0; x < zone.size(); x++) { * // clean up the zone * * // If allowed, at point to polygon * poly.addPoint(zone.get(x).x, zone.get(x).y); * * System.out.println("Point added: " + zone.get(x)); * } */ } public static void main(String[] args) { new PolyCheck(); } }