Results 1 to 11 of 11
 10032012, 06:17 PM #1Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
Problem with comparator implementation
hi,
this one is driving me crazy
i have made a Point class that holds x and y coordinates. I am calculating the slopes among points and then i want to sort the Points according to their slopes. I have made i private class that Implements comparator for this reason.
This is what i have written
XML Code:import java.util.Comparator; public class Point { Integer x,y; public final Comparator <Point>BY_SLOPE=new BySlope(); public Point(Integer x,Integer y){ this.x=x; this.y=y; } public void setX(Integer x){ this.x=x; } public int getX(){ return this.x; } public void setY(Integer y){ this.y=y; } public int getY(){ return this.y; } public double slopeTo(Point that){ double a=1; double pos=(aa)/a;//horizontal lines double neg=(aa)/a;//vertical lines double y=that.getY()this.getY(); double x=that.getX()this.getX(); double slope=y/x;//(that.getY()this.getY())/(that.getX()this.getX()); if (y==0) slope=pos; if (x==0) slope=Double.POSITIVE_INFINITY; if (y==0 && x==0) slope=Double.NEGATIVE_INFINITY; return slope; } private class BySlope implements Comparator<Point>{ private double epsilon=0.00001; @Override public int compare(Point o1, Point o2) { double s1=Point.this.slopeTo(o1); double s2=Point.this.slopeTo(o2); if (s1<s2) return 1; return 0; } } public String toString(){ return ""+this.getX()+","+this.getY(); } }
XML Code:import java.util.Arrays; public class PointTest { public static void main(String[] args) { int first=0; int second=1; int ff=0; int ss=1; Point[] arr=new Point [7]; Point a=new Point(23,43); Point b=new Point(33,3); Point c=new Point(65,31); Point d=new Point(14,56); Point e=new Point(90,0); Point f=new Point(4,6); Point g=new Point (35,5); arr[0]=a; arr[1]=b; arr[2]=c; arr[3]=d; arr[4]=e; arr[5]=f; arr[6]=g; for (int i=0; i<arr.length1; i++){ System.out.println(arr[second].getY()+""+arr[first].getY()+"/"+arr[second].getX()+""+arr[first].getX()+" slope>> "+arr[second].slopeTo(arr[first])); first++; second++; } Arrays.sort(arr,arr[0].BY_SLOPE); System.out.println("AFTER SORT"); for (int i=0; i<arr.length1; i++){ System.out.println(arr[ss].getY()+""+arr[ff].getY()+"/"+arr[ss].getX()+""+arr[ff].getX()+" slope>> "+arr[ss].slopeTo(arr[ff])); ff++; ss++; } } }
thanks
 10032012, 07:38 PM #2
Re: Problem with comparator implementation
What exactly do you mean when you say this does not work?
But it seems strange to me to order points by their slope points don't have a slope. You're introducing a dependence that I'm pretty sure isn't compatible with the notion of sorting.How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 10042012, 10:15 AM #3Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
Re: Problem with comparator implementation
hi and thanks for the reply..
what am i trying to do is, lets say your are in a spreadsheet with 3 columns. x,y and their slopes. Then you sort all the columns by the slopes column.
example in libreoffice
XML Code:x y slope 23 43 4 33 3 0.875 65 31 0.49 14 56 0.736 90 0 0.069 4 6 0.0322 35 5 0
turns to this:
XML Code:x y slope 23 43 4 14 56 0.736 65 31 0.49 4 6 0.0322 35 5 0 90 0 0.069 33 3 0.875
this ia actually part from a very interesting exercise i found here
i the above code i have made a mistake. I shouldn`t recalculate the slope after the array had been sorted but i think you get my pointLast edited by dimiro; 10042012 at 10:17 AM.
 10042012, 02:32 PM #4
Re: Problem with comparator implementation
Right but the thing is that a point's slope is completely dependent on the other point. So does comparing 2 points and then comparing 1 of those points to a 3rd point tell you about the total ordering of them? What if you had compared them in a different order?
You still haven't told us what doesn't work about your code though.How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 10042012, 04:02 PM #5Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
Re: Problem with comparator implementation
exactly they are dependent but i am only calculating the slopes once with their specific initial order . By having them is this *specific* order, calculate the slopes and sort them according to the *already* calculated slopes.
the mistake in the code is that i get this:
23,43
33,3
65,31
14,56
90,0
4,6
35,5
AFTER SORT
4,6
65,31
90,0
14,56
35,5
33,3
23,43
 10042012, 04:08 PM #6
Re: Problem with comparator implementation
That isn't necessarily true. You don't have any control over what order the slopes are compared, especially as they are moved around in the array by whatever sort algorithm Java's using under the hood (I think it's something similar to merge sort, but it doesn't really matter). The point is that you don't have any control over when the points are compared, so you're relying on a dependency that isn't a guarantee.
How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 10042012, 05:54 PM #7Moderator
 Join Date
 Apr 2009
 Posts
 12,091
 Rep Power
 20
Re: Problem with comparator implementation
The phrase "nondeterministic" comes to mind.
Please do not ask for code as refusal often offends.
** This space for rent **
 10042012, 07:16 PM #8Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
Re: Problem with comparator implementation
That isn't necessarily true. You don't have any control over what order the slopes are compared, especially as they are moved around in the array by whatever sort algorithm Java's using under the hood (I think it's something similar to merge sort, but it doesn't really matter). The point is that you don't have any control over when the points are compared, so you're relying on a dependency that isn't a guarantee.
so it makes sense to do something like this:
create a copy of my initial array. Lets say i call it sorted and then i do:
Java Code:Arrays.sort(arr,sorted[0].BY_SLOPE);
i only added a copy constructor in my Point class
Java Code:public Point(Point p){ x=p.getX(); y=p.getY(); }
Java Code:Point [] sorted=new Point[7]; for (int i=0; i<sorted.length; i++){ sorted[i]=new Point(arr[i]); }
Does this makes sense or am i just trying to do something that cannot be done (like this)?
 10042012, 07:36 PM #9Member
 Join Date
 Jul 2012
 Posts
 2
 Rep Power
 0
Re: Problem with comparator implementation
aaaa
 10042012, 07:42 PM #10
Re: Problem with comparator implementation
I still don't really understand what you're trying to do with sorting the points by slope.
What I guess you're trying to do is sort points according to the slope of the imagined function that they all represent at that xvalue. But your data doesn't reflect that.
Try sorting them according to xvalue, then you can find the "slope of the line to the next point" for each point. Store that in a data structure along with the point. Sort that data structure by the "slopeatpoint" value. I still don't know what that gets you, but I think it's what you're trying to do.How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 10042012, 08:29 PM #11Member
 Join Date
 Apr 2011
 Posts
 7
 Rep Power
 0
Similar Threads

problem in comparator interface implementing
By fractal in forum New To JavaReplies: 11Last Post: 06112012, 10:33 PM 
Implementation problem
By jcarosella10 in forum New To JavaReplies: 5Last Post: 03112012, 03:57 PM 
java.util.Comparator Implementation
By Navatha in forum New To JavaReplies: 6Last Post: 11052010, 01:48 AM 
Problem with the result of the implementation
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 5Last Post: 01112010, 05:33 AM 
Problem in file implementation
By BHCluster in forum New To JavaReplies: 6Last Post: 04212008, 03:21 AM
Bookmarks