Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implements brute force solution.

  • Loading branch information...
commit 2bcbff3e4c9a69989c4c352ca3d86c71d06b8ed9 1 parent 8460bb8
authored September 12, 2012

Showing 1 changed file with 51 additions and 0 deletions. Show diff stats Hide diff stats

  1. 51  HW3/Brute.java
51  HW3/Brute.java
... ...
@@ -0,0 +1,51 @@
  1
+import java.util.Arrays;
  2
+
  3
+public class Brute {
  4
+	
  5
+    public static void main(String[] args) {
  6
+    	
  7
+        // Rescales coordinates and turns on animation mode.
  8
+    	StdDraw.setXscale(0, 32768);
  9
+        StdDraw.setYscale(0, 32768);
  10
+        StdDraw.show(0);
  11
+
  12
+        // Reads in the input.        
  13
+    	String filename = args[0];
  14
+        In in = new In(filename);
  15
+        int N = in.readInt();   // Total # of points that are in input file.        
  16
+        
  17
+        // Adds all points from the input file to an array. 
  18
+        Point[] points = new Point[N];        
  19
+        for (int i=0; i < N; i++) {
  20
+        	int x = in.readInt(); 
  21
+        	int y = in.readInt(); 
  22
+        	Point p = new Point(x, y); 
  23
+        	points[i] = p; 
  24
+        }
  25
+       
  26
+        // Sorts the points array in descending order. 
  27
+        Arrays.sort(points); 
  28
+         
  29
+        // Brute forcing through to get all combinations of 4 points (N choose 4). 
  30
+        for (int i=0; i < N-3; i++) {
  31
+        	for (int j=i+1; j < N-2; j++) {
  32
+        		for (int k=j+1; k < N-1; k++) {
  33
+        			for (int l=k+1; l < N; l++) {     				
  34
+        				double slope1 = points[i].slopeTo(points[j]); 
  35
+        				double slope2 = points[j].slopeTo(points[k]); 
  36
+        				double slope3 = points[k].slopeTo(points[l]);
  37
+        				if (slope1 == slope2 && slope2 == slope3 && slope1 == slope3) {
  38
+        					// Prints out line segment order. 
  39
+        					StdOut.println(points[i] + " -> " + points[j] + " -> " + points[k] + " -> " + points[l]);
  40
+        					// Draws a line segment connecting the two ends. 
  41
+        					points[i].drawTo(points[l]);  
  42
+        				}
  43
+        			}
  44
+        		}
  45
+        	}
  46
+        }
  47
+       
  48
+        // Displays to screen all at once.
  49
+        StdDraw.show(0);
  50
+    }
  51
+}

0 notes on commit 2bcbff3

Please sign in to comment.
Something went wrong with that request. Please try again.