Results 1 to 11 of 11
- 10-03-2012, 06:17 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Problem with comparator implementation
hi,
this one is driving me crazy.gif)
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=(a-a)/a;//horizontal lines double neg=(a-a)/-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(); } }
i really cannot understand why this does not work. Any ideas?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.length-1; 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.length-1; 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
- 10-03-2012, 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!
- 10-04-2012, 10:15 AM #3
Member
- 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:
see what i mean?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; 10-04-2012 at 10:17 AM.
- 10-04-2012, 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!
- 10-04-2012, 04:02 PM #5
Member
- 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:
while the correct order is the one i mentioned in my previous post.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
- 10-04-2012, 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!
- 10-04-2012, 05:54 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Problem with comparator implementation
The phrase "non-deterministic" comes to mind.
Please do not ask for code as refusal often offends.
- 10-04-2012, 07:16 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Re: Problem with comparator implementation
aaa ok..many thanks for making it clearer. Now i got itThat 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..gif)
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
and initialized a new arrayJava Code:public Point(Point p){ x=p.getX(); y=p.getY(); }
but still no luck.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)?
- 10-04-2012, 07:36 PM #9
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Re: Problem with comparator implementation
aaaa
- 10-04-2012, 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 x-value. But your data doesn't reflect that.
Try sorting them according to x-value, 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 "slope-at-point" 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!
- 10-04-2012, 08:29 PM #11
Member
- 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: 06-11-2012, 10:33 PM -
Implementation problem
By jcarosella10 in forum New To JavaReplies: 5Last Post: 03-11-2012, 03:57 PM -
java.util.Comparator Implementation
By Navatha in forum New To JavaReplies: 6Last Post: 11-05-2010, 01:48 AM -
Problem with the result of the implementation
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 5Last Post: 01-11-2010, 05:33 AM -
Problem in file implementation
By BHCluster in forum New To JavaReplies: 6Last Post: 04-21-2008, 03:21 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks