Results 1 to 1 of 1
Thread: ConvexHull by brute force
- 10-03-2011, 04:49 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
ConvexHull by brute force
I'm working on a convex hull problem. I want to use brute force O(n^3).
I've got some code done and it works okay but instead of outputting the convex hull I get edges to every point. Can someone help me out here. I'm dying.
Java Code:import java.util.*; import java.awt.Point; import java.awt.Polygon; public class ConvexHull { // all the points in the collection private ArrayList<Point> points; // the polygon that makes up the convex hull private Polygon hull; // a boolean to check if the convex hull needs to be recalculated private boolean hullCalculated = false; // constructor public ConvexHull() { points = new ArrayList<Point>(); hull = new Polygon(); } /** * Adds a single point to the collection * @param point */ public void addPoint(Point point) { this.points.add(point); hullCalculated = false; } /** returns a specific point from the collection * * @param i: a number between 0 and the number of points * @return the Point indexed */ public Point getPoint(int i){ if (0 <= i && i < points.size()) return points.get(i); else throw new NoSuchElementException(); } /** * * @return an ArrayList with all the Points in the collection */ public ArrayList<Point> getPoints() { return points; } /** * removes all the points from the collection */ public void clear() { points.clear(); hull.reset(); hullCalculated = false; } /** returns the number of points in the current collection */ public int getNumber() { return points.size(); } /** returns the convex hull for the current set of points */ public Polygon getHull() { if (!hullCalculated) calculateConvexHull(); return hull; } /** returns all points' positions as a String */ public String toString() { String returnString = "Points:\n"; for (int i=0; i<hull.npoints; i++){ returnString = returnString + " Point " + i + ": (" + points.get(i).x + "," + points.get(i).y; } return returnString; } /** returns convex hull points */ public String convexHullToString() { String hullPoints = "Convex Hull:\n"; if (!hullCalculated) calculateConvexHull(); for (int i=0; i<hull.npoints; i++){ hullPoints = hullPoints + "\t(" + hull.xpoints[i] +"," + hull.ypoints[i] + ")\n"; } return hullPoints; } /** * Calculates the convex hull from the current set of points * * This method currently does not work! */ private void calculateConvexHull() { hull.reset(); hullCalculated = true; Point leftMost = null; int i = 0, j, k; if (points.get(i).x > points.get(i + 1).x){ leftMost = points.get(i + 1); Point nextLeft = points.get(i + 2); i++; hull.addPoint(leftMost.x, leftMost.y); for (j = 0; j < points.size(); j++){ Edge chkEdge = new Edge(leftMost, nextLeft); for (k = 0; k < points.size(); k++){ chkEdge.plugInPoint(points.get(k)); //do the sign check. // if all neg or all pos then this is an edge if (j != k){ chkEdge.plugInPoint(points.get(j)); hull.addPoint(points.get(k).x, points.get(k).y); } } } } } }
Similar Threads
-
Force Graphics Sync (?)
By Acidic in forum Java 2DReplies: 5Last Post: 04-18-2011, 03:07 AM -
How to force applet to use libraries?
By Arvutis6ber in forum Java AppletsReplies: 1Last Post: 09-20-2010, 02:24 PM -
How to use "Class convexHull.GrahamScan" in Java
By Mazharul in forum Java 2DReplies: 1Last Post: 04-18-2009, 05:14 AM -
Howto force to use an external jar
By riccardo_b in forum EclipseReplies: 0Last Post: 12-11-2008, 10:08 AM -
Force Caps in JTable...
By markw8500 in forum Advanced JavaReplies: 0Last Post: 07-09-2008, 02:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks