Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.util.ArrayList;
- /**
- * Created by IntelliJ IDEA.
- * User: Alowan
- * Date: 16-dec-2009
- * Time: 18:15:30
- * This code is for educational purposes only!
- */
- public class Main {
- public static void main(String[] args) {
- ArrayList<Point> points = new ArrayList<Point>();
- points.add(new Point(1,1));
- points.add(new Point (5,5));
- points.add(new Point(1,2));
- points.add(new Point(5,4));
- points.add(new Point(2,2));
- points.add(new Point(4,4));
- ArrayList<ArrayList<Point>> connectedPointsList = new ArrayList<ArrayList<Point>>();
- while(points.size() > 0) {
- connectedPointsList.add(getConnected(points.get(0), points, new ArrayList<Point>()));
- }
- for(ArrayList<Point> ps : connectedPointsList) {
- for(Point p : ps) {
- System.out.println(p);
- }
- System.out.println("-----------------------");
- }
- }
- private static ArrayList<Point> getConnected(Point p, ArrayList<Point> allPoints, ArrayList<Point> connectedPoints) {
- connectedPoints.add(p);
- allPoints.remove(p);
- Point p1 = new Point(p.x - 1, p.y);
- Point p2 = new Point(p.x + 1, p.y);
- Point p3 = new Point(p.x, p.y - 1);
- Point p4 = new Point(p.x, p.y + 1);
- for(Point pX : new Point[] {p1, p2, p3, p4}) {
- if(allPoints.contains(pX) && !connectedPoints.contains(pX)) {
- getConnected(pX, allPoints, connectedPoints);
- }
- }
- return connectedPoints;
- }
- }
Add Comment
Please, Sign In to add comment