In a Java program, I have an array of type Point[]. I also have a variable calle
ID: 3890508 • Letter: I
Question
In a Java program, I have an array of type Point[]. I also have a variable called referencePoint, which is also of type Point. I want to Compare the polar angles of two points (p1 and p2) with respect to referencePoint using cross product. I am not allowed to use trignometric functions.
The method should return:
0 if p1 and p2 have the same polar angle.
-1 if p1 equals referencePoint or its polar angle with respect to referencePoint is less than that of p2.
1 otherwise.
In addition, There is another method which uses this method. The method is called compare. it takes two paramteres (Point p1, Point p2), the referencePoint is provided in the constructor.
it's description is as follows:
0 if p1 and p2 are the same point
-1 otherwise, if one of the following three conditions holds:
a) p1 and referencePoint are the same point (hence 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.
1 otherwise.
Implement these two methods in Java, and post the code here.
Explanation / Answer
class Point
{
int r,thetha;
public Point(int r,int thetha)
{
this.r=r;
this.thetha=thetha;
}
public int compare(Point p1,Point p2 )
{
if(p1.thetha==p2.thetha && p1.r==p2.r)
return 0;
else if(p1.thetha==this.thetha && p1.r==this.r)
return -1;
else if(p2.thetha==this.thetha && p2.r==this.r)
return -1;
else if(p1.r<p2.r && p1.thetha==p2.thetha)
return -1;
else
return 1;
}
public static void main(String [] args)
{
Point[] point=new Point[2];
Point refPoint=new Point(2,30);
point[0]=new Point(2,30);
point[1]=new Point(12,50);
if(refPoint.compare(point[0],point[1])==0)
System.out.println("P1 and P2 are same");
else if(refPoint.compare(point[0],point[1])==-1)
System.out.println("either p1 or p2 equals ref or p1 and p2 same angle but p1<p2 ");
else
System.out.println("otherwise condition");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.