Skip to content

Instantly share code, notes, and snippets.

@Chayemor
Created July 17, 2020 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chayemor/8d2deb4029d5170fdf8d9127c1bdb52a to your computer and use it in GitHub Desktop.
Save Chayemor/8d2deb4029d5170fdf8d9127c1bdb52a to your computer and use it in GitHub Desktop.
Collinear - Coursera Algorithms Part 1
/******************************************************************************
*
* Name: Johanna Mesa Ramos
* Coursera User ID: c02b4bb527298de170192d644483baeb
* Last modified: 17/07/2020
* Course: Algorithms, Part I https://www.coursera.org/learn/algorithms-part1
*
******************************************************************************/
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
public class BruteCollinearPoints {
private final LineSegment[] segments;
public BruteCollinearPoints(Point[] points) {
if (points == null)
throw new IllegalArgumentException(
"Argument can't be null, or have any null or repeated points");
Point[] mutablePoints = Arrays.stream(points).toArray(Point[]::new);
if (containsNullPoint(mutablePoints) || repeatedPoint(mutablePoints))
throw new IllegalArgumentException(
"Argument can't be null, or have any null or repeated points");
List<LineSegment> tmpSegments = new LinkedList<>();
for (int p = 0; p < mutablePoints.length; ++p) {
for (int q = p + 1; q < mutablePoints.length; ++q) {
for (int r = q + 1; r < mutablePoints.length; ++r) {
for (int s = r + 1; s < mutablePoints.length; ++s) {
if (mutablePoints[p].slopeTo(mutablePoints[q]) == mutablePoints[p]
.slopeTo(mutablePoints[r]) &&
mutablePoints[p].slopeTo(mutablePoints[r]) == mutablePoints[p]
.slopeTo(mutablePoints[s])
) {
Point[] segment = {
mutablePoints[p], mutablePoints[q], mutablePoints[r],
mutablePoints[s]
};
Arrays.sort(segment);
if (mutablePoints[p].compareTo(segment[0]) <= 0)
tmpSegments
.add(new LineSegment(mutablePoints[p], mutablePoints[s]));
}
}
}
}
}
segments = tmpSegments.toArray(new LineSegment[0]);
}
private boolean containsNullPoint(Point[] points) {
return Arrays.stream(points).anyMatch(Objects::isNull);
}
private boolean repeatedPoint(Point[] points) {
Arrays.sort(points);
for (int i = 0; i < points.length - 1; ++i) {
if (points[i].compareTo(points[i + 1]) == 0)
return true;
}
return false;
}
public int numberOfSegments() {
return segments.length;
}
public LineSegment[] segments() {
return Arrays.stream(segments).toArray(LineSegment[]::new);
}
public static void main(String[] args) {
// read the n points from a file
In in = new In(args[0]);
int n = in.readInt();
Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
int x = in.readInt();
int y = in.readInt();
points[i] = new Point(x, y);
}
// set color and size
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.setPenRadius(0.0075);
StdDraw.enableDoubleBuffering();
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
// print and draw the line segments
BruteCollinearPoints collinear = new BruteCollinearPoints(points);
for (LineSegment segment : collinear.segments()) {
StdOut.println(segment);
segment.draw();
}
StdDraw.show();
}
}
/******************************************************************************
*
* Name: Johanna Mesa Ramos
* Coursera User ID: c02b4bb527298de170192d644483baeb
* Last modified: 17/07/2020
* Course: Algorithms, Part I https://www.coursera.org/learn/algorithms-part1
*
******************************************************************************/
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
public class FastCollinearPoints {
private final LineSegment[] segments;
public FastCollinearPoints(Point[] points) {
if (points == null)
throw new IllegalArgumentException(
"Argument can't be null, or have any null or repeated points");
Point[] mutablePoints = Arrays.stream(points).toArray(Point[]::new);
if (containsNullPoint(mutablePoints) || repeatedPoint(mutablePoints))
throw new IllegalArgumentException(
"Argument can't be null, or have any null or repeated points");
List<LineSegment> tmpSegments = new LinkedList<>();
for (int i = 0; i < points.length; ++i) {
Point p = points[i];
Arrays.sort(mutablePoints, 0, points.length, p.slopeOrder());
int segmentStart = 1; // Skip index 0 always because it's slope of own point 'p'
int segmentSize = 1; // point 'p' counts as part of the segment
for (int q = segmentStart; q < points.length - 1; ++q) {
if (p.slopeTo(mutablePoints[q]) == p.slopeTo(mutablePoints[q + 1])) {
if (segmentSize == 1)
segmentStart = q;
++segmentSize;
}
else {
if (segmentSize >= 3)
constructSegment(segmentSize, segmentStart, mutablePoints, p, tmpSegments);
segmentSize = 1;
}
}
if (segmentSize >= 3)
constructSegment(segmentSize, segmentStart, mutablePoints, p, tmpSegments);
}
segments = tmpSegments.toArray(new LineSegment[0]);
}
private void constructSegment(int segmentSize, int segmentStart, Point[] mutablePoints, Point p,
List<LineSegment> tmpSegments) {
Point[] segmentPoints = new Point[segmentSize + 1]; // Plus one for that point 'p'
segmentPoints[0] = p;
for (int pos = 0; pos < segmentSize; ++pos) {
segmentPoints[pos + 1] = mutablePoints[segmentStart + pos];
}
Arrays.sort(segmentPoints, 0, segmentPoints.length);
if (p.compareTo(segmentPoints[0]) <= 0)
tmpSegments
.add(new LineSegment(segmentPoints[0], segmentPoints[segmentSize]));
}
private boolean containsNullPoint(Point[] points) {
return Arrays.stream(points).anyMatch(Objects::isNull);
}
private boolean repeatedPoint(Point[] points) {
Arrays.sort(points);
for (int i = 0; i < points.length - 1; ++i) {
if (points[i].compareTo(points[i + 1]) == 0)
return true;
}
return false;
}
public int numberOfSegments() {
return segments.length;
}
public LineSegment[] segments() {
return Arrays.stream(segments).toArray(LineSegment[]::new);
}
public static void main(String[] args) {
// read the n points from a file
In in = new In(args[0]);
int n = in.readInt();
Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
int x = in.readInt();
int y = in.readInt();
points[i] = new Point(x, y);
}
// set color and size
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.setPenRadius(0.0075);
StdDraw.enableDoubleBuffering();
StdDraw.setXscale(0, 32768);
StdDraw.setYscale(0, 32768);
// print and draw the line segments
FastCollinearPoints collinear = new FastCollinearPoints(points);
for (LineSegment segment : collinear.segments()) {
StdOut.println(segment);
segment.draw();
}
StdOut.println(collinear.numberOfSegments());
StdDraw.show();
}
}
/*************************************************************************
* Compilation: javac LineSegment.java
* Execution: none
* Dependencies: Point.java
*
* An immutable data type for Line segments in the plane.
* For use on Coursera, Algorithms Part I programming assignment.
*
* DO NOT MODIFY THIS CODE.
*
*************************************************************************/
public class LineSegment {
private final Point p; // one endpoint of this line segment
private final Point q; // the other endpoint of this line segment
/**
* Initializes a new line segment.
*
* @param p one endpoint
* @param q the other endpoint
* @throws NullPointerException if either <tt>p</tt> or <tt>q</tt>
* is <tt>null</tt>
*/
public LineSegment(Point p, Point q) {
if (p == null || q == null) {
throw new NullPointerException("argument is null");
}
this.p = p;
this.q = q;
}
/**
* Draws this line segment to standard draw.
*/
public void draw() {
p.drawTo(q);
}
/**
* Returns a string representation of this line segment
* This method is provide for debugging;
* your program should not rely on the format of the string representation.
*
* @return a string representation of this line segment
*/
public String toString() {
return p + " -> " + q;
}
/**
* Throws an exception if called. The hashCode() method is not supported because
* hashing has not yet been introduced in this course. Moreover, hashing does not
* typically lead to good *worst-case* performance guarantees, as required on this
* assignment.
*
* @throws UnsupportedOperationException if called
*/
public int hashCode() {
throw new UnsupportedOperationException();
}
}
/******************************************************************************
* Execution: java Point
* Dependencies: none
*
* An immutable data type for points in the plane.
* For use on Coursera, Algorithms Part I programming assignment.
* Provided base class implements the constructor, the draw(), drawTo(), and toString() methods
*
* Name: Johanna Mesa Ramos
* Coursera User ID: c02b4bb527298de170192d644483baeb
* Last modified: 17/07/2020
* Course: Algorithms, Part I https://www.coursera.org/learn/algorithms-part1
*
******************************************************************************/
import edu.princeton.cs.algs4.StdDraw;
import java.util.Comparator;
public class Point implements Comparable<Point> {
private final int x; // x-coordinate of this point
private final int y; // y-coordinate of this point
/**
* Initializes a new point.
*
* @param x the <em>x</em>-coordinate of the point
* @param y the <em>y</em>-coordinate of the point
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Draws this point to standard draw.
*/
public void draw() {
StdDraw.point(x, y);
}
/**
* Draws the line segment between this point and the specified point
* to standard draw.
*
* @param that the other point
*/
public void drawTo(Point that) {
StdDraw.line(this.x, this.y, that.x, that.y);
}
/**
* Returns the slope between this point and the specified point.
* Formally, if the two points are (x0, y0) and (x1, y1), then the slope
* is (y1 - y0) / (x1 - x0). For completeness, the slope is defined to be
* +0.0 if the line segment connecting the two points is horizontal;
* Double.POSITIVE_INFINITY if the line segment is vertical;
* and Double.NEGATIVE_INFINITY if (x0, y0) and (x1, y1) are equal.
*
* @param that the other point
* @return the slope between this point and the specified point
*/
public double slopeTo(Point that) {
if (compareTo(that) == 0) return Double.NEGATIVE_INFINITY;
if (this.y == that.y) return +0.0;
if (this.x == that.x) return Double.POSITIVE_INFINITY;
return (double) (that.y - this.y) / (that.x - this.x);
}
/**
* Compares two points by y-coordinate, breaking ties by x-coordinate.
* Formally, the invoking point (x0, y0) is less than the argument point
* (x1, y1) if and only if either y0 < y1 or if y0 = y1 and x0 < x1.
*
* @param that the other point
* @return the value <tt>0</tt> if this point is equal to the argument
* point (x0 = x1 and y0 = y1);
* a negative integer if this point is less than the argument
* point; and a positive integer if this point is greater than the
* argument point
*/
public int compareTo(Point that) {
if (this.y == that.y && this.x == that.x)
return 0;
if (this.y < that.y || (this.y == that.y && this.x < that.x))
return -1;
return 1;
}
/**
* Compares two points by the slope they make with this point.
* The slope is defined as in the slopeTo() method.
*
* @return the Comparator that defines this ordering on points
*/
public Comparator<Point> slopeOrder() {
return (p1, p2) -> {
double slopeP1 = slopeTo(p1);
double slopeP2 = slopeTo(p2);
return Double.compare(slopeP1, slopeP2);
};
}
/**
* Returns a string representation of this point.
* This method is provided for debugging
*
* @return a string representation of this point
*/
public String toString() {
return "(" + x + ", " + y + ")";
}
/**
* Unit tests the Point data type.
*/
public static void main(String[] args) {
Point a = new Point(5, 0);
Point b = new Point(6, 0);
System.out.format("a < b %s %n", a.compareTo(b) < 0);
a = new Point(6, 0);
b = new Point(-1, 0);
System.out.format("a > b %s %n", a.compareTo(b) > 0);
a = new Point(1, 0);
b = new Point(1, 0);
System.out.format("a == b %s %n", a.compareTo(b) == 0);
a = new Point(2, 10);
b = new Point(10, 10);
System.out.format("No slope %s %n", a.slopeTo(b) == +0.0);
a = new Point(2, 2);
b = new Point(2, 10);
System.out.format("Vertical slope %s %n", a.slopeTo(b) == Double.POSITIVE_INFINITY);
b = new Point(2, 2);
System.out.format("Point within itself %s %n", a.slopeTo(b) == Double.NEGATIVE_INFINITY);
a = new Point(2, 2);
b = new Point(3, 5);
System.out
.format("Regular slope %s %n", a.slopeTo(b) == (double) (a.y - b.y) / (a.x - b.x));
}
}
See the Assessment Guide for information on how to interpret this report.
ASSESSMENT SUMMARY
Compilation: PASSED
API: PASSED
Spotbugs: PASSED
PMD: PASSED
Checkstyle: PASSED
Correctness: 41/41 tests passed
Memory: 1/1 tests passed
Timing: 39/41 tests passed
Aggregate score: 99.02%
[Compilation: 5%, API: 5%, Spotbugs: 0%, PMD: 0%, Checkstyle: 0%, Correctness: 60%, Memory: 10%, Timing: 20%]
ASSESSMENT DETAILS
The following files were submitted:
----------------------------------
3.5K Jul 17 16:20 BruteCollinearPoints.java
4.0K Jul 17 16:20 FastCollinearPoints.java
4.7K Jul 17 16:20 Point.java
********************************************************************************
* COMPILING
********************************************************************************
% javac Point.java
*-----------------------------------------------------------
% javac LineSegment.java
*-----------------------------------------------------------
% javac BruteCollinearPoints.java
*-----------------------------------------------------------
% javac FastCollinearPoints.java
*-----------------------------------------------------------
================================================================
Checking the APIs of your programs.
*-----------------------------------------------------------
Point:
BruteCollinearPoints:
FastCollinearPoints:
================================================================
********************************************************************************
* CHECKING STYLE AND COMMON BUG PATTERNS
********************************************************************************
% spotbugs *.class
*-----------------------------------------------------------
================================================================
% pmd .
*-----------------------------------------------------------
================================================================
% checkstyle *.java
*-----------------------------------------------------------
% custom checkstyle checks for Point.java
*-----------------------------------------------------------
% custom checkstyle checks for BruteCollinearPoints.java
*-----------------------------------------------------------
% custom checkstyle checks for FastCollinearPoints.java
*-----------------------------------------------------------
================================================================
********************************************************************************
* TESTING CORRECTNESS
********************************************************************************
Testing correctness of Point
*-----------------------------------------------------------
Running 3 total tests.
Test 1: p.slopeTo(q)
* positive infinite slope, where p and q have coordinates in [0, 500)
* positive infinite slope, where p and q have coordinates in [0, 32768)
* negative infinite slope, where p and q have coordinates in [0, 500)
* negative infinite slope, where p and q have coordinates in [0, 32768)
* positive zero slope, where p and q have coordinates in [0, 500)
* positive zero slope, where p and q have coordinates in [0, 32768)
* symmetric for random points p and q with coordinates in [0, 500)
* symmetric for random points p and q with coordinates in [0, 32768)
* transitive for random points p, q, and r with coordinates in [0, 500)
* transitive for random points p, q, and r with coordinates in [0, 32768)
* slopeTo(), where p and q have coordinates in [0, 500)
* slopeTo(), where p and q have coordinates in [0, 32768)
* slopeTo(), where p and q have coordinates in [0, 10)
* throw a java.lang.NullPointerException if argument is null
==> passed
Test 2: p.compareTo(q)
* reflexive, where p and q have coordinates in [0, 500)
* reflexive, where p and q have coordinates in [0, 32768)
* antisymmetric, where p and q have coordinates in [0, 500)
* antisymmetric, where p and q have coordinates in [0, 32768)
* transitive, where p, q, and r have coordinates in [0, 500)
* transitive, where p, q, and r have coordinates in [0, 32768)
* sign of compareTo(), where p and q have coordinates in [0, 500)
* sign of compareTo(), where p and q have coordinates in [0, 32768)
* sign of compareTo(), where p and q have coordinates in [0, 10)
* throw java.lang.NullPointerException exception if argument is null
==> passed
Test 3: p.slopeOrder().compare(q, r)
* reflexive, where p and q have coordinates in [0, 500)
* reflexive, where p and q have coordinates in [0, 32768)
* antisymmetric, where p, q, and r have coordinates in [0, 500)
* antisymmetric, where p, q, and r have coordinates in [0, 32768)
* transitive, where p, q, r, and s have coordinates in [0, 500)
* transitive, where p, q, r, and s have coordinates in [0, 32768)
* sign of compare(), where p, q, and r have coordinates in [0, 500)
* sign of compare(), where p, q, and r have coordinates in [0, 32768)
* sign of compare(), where p, q, and r have coordinates in [0, 10)
* throw java.lang.NullPointerException if either argument is null
==> passed
Total: 3/3 tests passed!
================================================================
********************************************************************************
* TESTING CORRECTNESS (substituting reference Point and LineSegment)
********************************************************************************
Testing correctness of BruteCollinearPoints
*-----------------------------------------------------------
Running 17 total tests.
The inputs satisfy the following conditions:
- no duplicate points
- no 5 (or more) points are collinear
- all x- and y-coordinates between 0 and 32,767
Test 1: points from a file
* filename = input8.txt
* filename = equidistant.txt
* filename = input40.txt
* filename = input48.txt
==> passed
Test 2a: points from a file with horizontal line segments
* filename = horizontal5.txt
* filename = horizontal25.txt
==> passed
Test 2b: random horizontal line segments
* 1 random horizontal line segment
* 5 random horizontal line segments
* 10 random horizontal line segments
* 15 random horizontal line segments
==> passed
Test 3a: points from a file with vertical line segments
* filename = vertical5.txt
* filename = vertical25.txt
==> passed
Test 3b: random vertical line segments
* 1 random vertical line segment
* 5 random vertical line segments
* 10 random vertical line segments
* 15 random vertical line segments
==> passed
Test 4a: points from a file with no line segments
* filename = random23.txt
* filename = random38.txt
==> passed
Test 4b: random points with no line segments
* 5 random points
* 10 random points
* 20 random points
* 50 random points
==> passed
Test 5: points from a file with fewer than 4 points
* filename = input1.txt
* filename = input2.txt
* filename = input3.txt
==> passed
Test 6: check for dependence on either compareTo() or compare()
returning { -1, +1, 0 } instead of { negative integer,
positive integer, zero }
* filename = equidistant.txt
* filename = input40.txt
* filename = input48.txt
==> passed
Test 7: check for fragile dependence on return value of toString()
* filename = equidistant.txt
* filename = input40.txt
* filename = input48.txt
==> passed
Test 8: random line segments, none vertical or horizontal
* 1 random line segment
* 5 random line segments
* 10 random line segments
* 15 random line segments
==> passed
Test 9: random line segments
* 1 random line segment
* 5 random line segments
* 10 random line segments
* 15 random line segments
==> passed
Test 10: check that data type is immutable by testing whether each method
returns the same value, regardless of any intervening operations
* input8.txt
* equidistant.txt
==> passed
Test 11: check that data type does not mutate the constructor argument
* input8.txt
* equidistant.txt
==> passed
Test 12: numberOfSegments() is consistent with segments()
* filename = input8.txt
* filename = equidistant.txt
* filename = input40.txt
* filename = input48.txt
* filename = horizontal5.txt
* filename = vertical5.txt
* filename = random23.txt
==> passed
Test 13: throws an exception if either the constructor argument is null
or any entry in array is null
* argument is null
* Point[] of length 10, number of null entries = 1
* Point[] of length 10, number of null entries = 10
* Point[] of length 4, number of null entries = 1
* Point[] of length 3, number of null entries = 1
* Point[] of length 2, number of null entries = 1
* Point[] of length 1, number of null entries = 1
==> passed
Test 14: check that the constructor throws an exception if duplicate points
* 50 points
* 25 points
* 5 points
* 4 points
* 3 points
* 2 points
==> passed
Total: 17/17 tests passed!
================================================================
Testing correctness of FastCollinearPoints
*-----------------------------------------------------------
Running 21 total tests.
The inputs satisfy the following conditions:
- no duplicate points
- all x- and y-coordinates between 0 and 32,767
Test 1: points from a file
* filename = input8.txt
* filename = equidistant.txt
* filename = input40.txt
* filename = input48.txt
* filename = input299.txt
==> passed
Test 2a: points from a file with horizontal line segments
* filename = horizontal5.txt
* filename = horizontal25.txt
* filename = horizontal50.txt
* filename = horizontal75.txt
* filename = horizontal100.txt
==> passed
Test 2b: random horizontal line segments
* 1 random horizontal line segment
* 5 random horizontal line segments
* 10 random horizontal line segments
* 15 random horizontal line segments
==> passed
Test 3a: points from a file with vertical line segments
* filename = vertical5.txt
* filename = vertical25.txt
* filename = vertical50.txt
* filename = vertical75.txt
* filename = vertical100.txt
==> passed
Test 3b: random vertical line segments
* 1 random vertical line segment
* 5 random vertical line segments
* 10 random vertical line segments
* 15 random vertical line segments
==> passed
Test 4a: points from a file with no line segments
* filename = random23.txt
* filename = random38.txt
* filename = random91.txt
* filename = random152.txt
==> passed
Test 4b: random points with no line segments
* 5 random points
* 10 random points
* 20 random points
* 50 random points
==> passed
Test 5a: points from a file with 5 or more on some line segments
* filename = input9.txt
* filename = input10.txt
* filename = input20.txt
* filename = input50.txt
* filename = input80.txt
* filename = input300.txt
* filename = inarow.txt
==> passed
Test 5b: points from a file with 5 or more on some line segments
* filename = kw1260.txt
* filename = rs1423.txt
==> passed
Test 6: points from a file with fewer than 4 points
* filename = input1.txt
* filename = input2.txt
* filename = input3.txt
==> passed
Test 7: check for dependence on either compareTo() or compare()
returning { -1, +1, 0 } instead of { negative integer,
positive integer, zero }
* filename = equidistant.txt
* filename = input40.txt
* filename = input48.txt
* filename = input299.txt
==> passed
Test 8: check for fragile dependence on return value of toString()
* filename = equidistant.txt
* filename = input40.txt
* filename = input48.txt
==> passed
Test 9: random line segments, none vertical or horizontal
* 1 random line segment
* 5 random line segments
* 25 random line segments
* 50 random line segments
* 100 random line segments
==> passed
Test 10: random line segments
* 1 random line segment
* 5 random line segments
* 25 random line segments
* 50 random line segments
* 100 random line segments
==> passed
Test 11: random distinct points in a given range
* 5 random points in a 10-by-10 grid
* 10 random points in a 10-by-10 grid
* 50 random points in a 10-by-10 grid
* 90 random points in a 10-by-10 grid
* 200 random points in a 50-by-50 grid
==> passed
Test 12: m*n points on an m-by-n grid
* 3-by-3 grid
* 4-by-4 grid
* 5-by-5 grid
* 10-by-10 grid
* 20-by-20 grid
* 5-by-4 grid
* 6-by-4 grid
* 10-by-4 grid
* 15-by-4 grid
* 25-by-4 grid
==> passed
Test 13: check that data type is immutable by testing whether each method
returns the same value, regardless of any intervening operations
* input8.txt
* equidistant.txt
==> passed
Test 14: check that data type does not mutate the constructor argument
* input8.txt
* equidistant.txt
==> passed
Test 15: numberOfSegments() is consistent with segments()
* filename = input8.txt
* filename = equidistant.txt
* filename = input40.txt
* filename = input48.txt
* filename = horizontal5.txt
* filename = vertical5.txt
* filename = random23.txt
==> passed
Test 16: throws an exception if either constructor argument is null
or any entry in array is null
* argument is null
* Point[] of length 10, number of null entries = 1
* Point[] of length 10, number of null entries = 10
* Point[] of length 4, number of null entries = 1
* Point[] of length 3, number of null entries = 1
* Point[] of length 2, number of null entries = 1
* Point[] of length 1, number of null entries = 1
==> passed
Test 17: check that the constructor throws an exception if duplicate points
* 50 points
* 25 points
* 5 points
* 4 points
* 3 points
* 2 points
==> passed
Total: 21/21 tests passed!
================================================================
********************************************************************************
* MEMORY
********************************************************************************
Analyzing memory of Point
*-----------------------------------------------------------
Running 1 total tests.
The maximum amount of memory per Point object is 32 bytes.
Student memory = 24 bytes (passed)
Total: 1/1 tests passed!
================================================================
********************************************************************************
* TIMING
********************************************************************************
Timing BruteCollinearPoints
*-----------------------------------------------------------
Running 10 total tests.
Test 1a-1e: Find collinear points among n random distinct points
slopeTo()
n time slopeTo() compare() + 2*compare() compareTo()
-----------------------------------------------------------------------------------------------
=> passed 16 0.00 3640 0 3640 62
=> passed 32 0.00 71920 0 71920 148
=> passed 64 0.02 1270752 0 1270752 368
=> passed 128 0.10 21336000 0 21336000 857
=> passed 256 1.04 349585280 0 349585280 1974
==> 5/5 tests passed
Test 2a-2e: Find collinear points among n/4 arbitrary line segments
slopeTo()
n time slopeTo() compare() + 2*compare() compareTo()
-----------------------------------------------------------------------------------------------
=> passed 16 0.00 3824 0 3824 78
=> passed 32 0.00 72710 0 72710 183
=> passed 64 0.01 1274168 0 1274168 434
=> passed 128 0.11 21349258 0 21349258 1000
=> passed 256 1.28 349637958 0 349637958 2247
==> 5/5 tests passed
Total: 10/10 tests passed!
================================================================
Timing FastCollinearPoints
*-----------------------------------------------------------
Running 31 total tests.
Test 1a-1g: Find collinear points among n random distinct points
slopeTo()
n time slopeTo() compare() + 2*compare() compareTo()
-----------------------------------------------------------------------------------------------
=> passed 64 0.01 7936 18565 45066 367
=> passed 128 0.00 32256 88044 208344 873
=> passed 256 0.02 130048 412280 954608 2001
=> passed 512 0.12 522240 1884696 4291632 4487
=> passed 1024 0.32 2093056 8486403 19065862 9988
=> passed 2048 0.76 8380416 37905856 84192128 21983
==> 6/6 tests passed
lg ratio(slopeTo() + 2*compare()) = lg (84192128 / 19065862) = 2.14
=> passed
==> 7/7 tests passed
Test 2a-2g: Find collinear points among the n points on an n-by-1 grid
slopeTo()
n time slopeTo() compare() + 2*compare() compareTo()
-----------------------------------------------------------------------------------------------
=> passed 64 0.00 7936 4480 16896 15414
=> passed 128 0.00 32256 17399 67054 66912
=> passed 256 0.01 130048 68000 266048 293082
=> passed 512 0.07 522240 267961 1058162 1290527
=> passed 1024 0.23 2093056 1061917 4216890 5670135
=> FAILED 2048 0.41 8380416 4225214 16830844 24725353 (1.1x)
=> FAILED 4096 1.60 33538048 16847038 67232124 107118468 (1.3x)
==> 5/7 tests passed
lg ratio(slopeTo() + 2*compare()) = lg (67232124 / 16830844) = 2.00
=> passed
==> 6/8 tests passed
Test 3a-3g: Find collinear points among the n points on an n/4-by-4 grid
slopeTo()
n time slopeTo() compare() + 2*compare() compareTo()
-----------------------------------------------------------------------------------------------
=> passed 64 0.00 7936 17644 43224 5217
=> passed 128 0.00 32256 72642 177540 20087
=> passed 256 0.01 130048 282235 694518 66095
=> passed 512 0.03 522240 1108446 2739132 233187
=> passed 1024 0.08 2093056 4393141 10879338 872692
=> passed 2048 0.32 8380416 17510260 43400936 3358238
=> passed 4096 1.18 33538048 69834490 173207028 13177766
==> 7/7 tests passed
lg ratio(slopeTo() + 2*compare()) = lg (173207028 / 43400936) = 2.00
=> passed
==> 8/8 tests passed
Test 4a-4g: Find collinear points among the n points on an n/8-by-8 grid
slopeTo()
n time slopeTo() compare() + 2*compare() compareTo()
-----------------------------------------------------------------------------------------------
=> passed 64 0.00 7936 18605 45146 4691
=> passed 128 0.00 32256 86099 204454 18911
=> passed 256 0.01 130048 383961 897970 72415
=> passed 512 0.04 522240 1612530 3747300 263563
=> passed 1024 0.14 2093056 6745389 15583834 997752
=> passed 2048 0.54 8380416 27713551 63807518 3860992
=> passed 4096 2.06 33538048 112849370 259236788 15189912
==> 7/7 tests passed
lg ratio(slopeTo() + 2*compare()) = lg (259236788 / 63807518) = 2.02
=> passed
==> 8/8 tests passed
Total: 29/31 tests passed!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment