currently working on with Graham's Scan in conjunction with Convex HUll. I am a student, so I am trying to get it done myself, however I've been sifting through multiple sites to find an answer. In short I have my constructors, one from a file and one randomly generated, working so I am able to create an array of points. The next step is to implement quicksort, sorting by Polar angles. This is done via a comparator class. The comparator class is where I am stuck, we are told to use dot compare and cross compare to do the comparisons of the angles but I am pretty lost.
/**
* Use cross product and dot product to implement this method. Do not take square roots
* or use trigonometric functions. See the PowerPoint notes on how to carry out cross and
* dot products.
*
* Call comparePolarAngle() and compareDistance().
*
* @param p1
* @param p2
* @return -1 if one of the following three conditions holds:
* a) p1 and referencePoint are the same point but p2 is a different point;
* b) neither p1 nor p2 equals referencePoint, and the polar angle of
* p1 with respect to referencePoint is less than that of p2;
* c) neither p1 nor p2 equals referencePoint, p1 and p2 have the same polar
* angle w.r.t. referencePoint, and p1 is closer to referencePoint than p2.
* 0 if p1 and p2 are the same point
* 1 if one of the following three conditions holds:
* a) p2 and referencePoint are the same point but p1 is a different point;
* b) neither p1 nor p2 equals referencePoint, and the polar angle of
* p1 with respect to referencePoint is greater than that of p2;
* c) neither p1 nor p2 equals referencePoint, p1 and p2 have the same polar
* angle w.r.t. referencePoint, and p1 is further to referencePoint than p2.
*
*/
public int compare(Point p1, Point p2){
if(p1 == referencePoint && p2 != referencePoint){
return -1;
} else if(p1 == p2){
return 0;
} else {
}
return 0;
}
/**
* Compare the polar angles of two points p1 and p2 with respect to referencePoint. Use
* cross products. Do not use trigonometric functions.
*
* Precondition: p1 and p2 are distinct points.
*
* @param p1
* @param p2
* @return -1 if p1 equals referencePoint or its polar angle with respect to referencePoint
* is less than that of p2.
* 0 if p1 and p2 have the same polar angle.
* 1 if p2 equals referencePoint or its polar angle with respect to referencePoint
* is less than that of p1.
*/
public int comparePolarAngle(Point p1, Point p2){
// TODO
return 0;
}
/**
* Compare the distances of two points p1 and p2 to referencePoint. Use dot products.
* Do not take square roots.
*
* @param p1
* @param p2
* @return -1 if p1 is closer to referencePoint
* 0 if p1 and p2 are equidistant to referencePoint
* 1 if p2 is closer to referencePoint
*/
public int compareDistance(Point p1, Point p2){
int distance = 0;
return distance;
}
That's the whole of it, I just kind of went through little things on the compare method before just getting stuck.
The quickSort and partition methods are pretty standard, but I will add them so you guys can get a broad spectrum look at everything:
/**
* Sort the array points[] in the increasing order of polar angle with respect to lowestPoint.
* Use quickSort. Construct an object of the pointComparator class with lowestPoint as the
* argument for point comparison.
*
* Ought to be private, but is made public for testing convenience.
*/
public void quickSort(){
// TODO
}
/**
* Operates on the subarray of points[] with indices between first and last.
*
* @param first starting index of the subarray
* @param last ending index of the subarray
*/
private void quickSortRec(int first, int last){
// TODO
}
/**
* Operates on the subarray of points[] with indices between first and last.
*
* @param first
* @param last
* @return
*/
private int partition(int first, int last){
// TODO
return 0;
}
I know I essentially need to get the Compare class up and running before I can get the quicksort method going, but I feel like I don't know how to even use the dot / cross compare at all so am feeling really lost.
If anyone would be willing to help, I would be very grateful! Thank you very much for looking, have a great evening.