Results 1 to 16 of 16
Thread: coordinate converter
 03182014, 05:26 PM #1Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
coordinate converter
Hi, I made these classes, one for converting cart to polar then calculating the distance between 2 points and the other for polar to cart then calculating the distance between 2 points... I am using the test class to input a list of ten coords and add up the total distance between them. I am having trouble when I create the instance of polar or cartesian I dont know what to send to it (right now I just have null) and that is giving me issues. Any help is appreciated. Thanks!
Java Code:public class Test { public static void main(String[] args) { //TestConversion test = new TestConversion(); System.out.println("I made it to here test"); Test.TestConversion(); } public static double[] TestConversion() { System.out.println("X,Y Coordinates are:"); double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90}; double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710}; double cartesianDistance =0; double polarDistance = 0; Cartesian startPoint = new Cartesian(null); Cartesian endPoint = new Cartesian(null); Polar polar1 = new Polar(null); Polar polar2 = new Polar(null); for ( int row = 0; row < xCoords.length  1; row++ ) { // loop through columns of current row startPoint.setX(xCoords[ row ]); startPoint.setY(yCoords[ row ]); endPoint.setX(xCoords[ row + 1 ]); endPoint.setY(yCoords[ row + 1 ]); double totalDistance = 0; double distance = startPoint.getDistance(endPoint); totalDistance = totalDistance + distance; cartesianDistance = totalDistance; } for ( int row = 0; row < xCoords.length  1; row++ ) { // loop through columns of current row startPoint.setX(xCoords[ row ]); startPoint.setY(yCoords[ row ]); endPoint.setX(xCoords[ row + 1 ]); endPoint.setY(yCoords[ row + 1 ]); double totalDistance = 0; double distance = polar1.calculateDistancePolar(polar2); totalDistance = totalDistance + distance; polarDistance = totalDistance; } double[] bothDistances = {cartesianDistance, polarDistance}; System.out.println("Cartesian Distance test =" +cartesianDistance++); System.out.println("Polar Distance test =" +polarDistance++); return bothDistances; } // end Test }
Java Code:public class Cartesian { // Price two Arg Constructor public Cartesian(Polar polar) { this.x = polar.getRho() * Math.cos(polar.getTheta()); this.y = polar.getRho() * Math.sin(polar.getTheta()); }// End Constructor public double getDistance(Cartesian cartesian) { return Math.sqrt(Math.pow((getX()  cartesian.getX()), 2.0) + Math.pow((getY()  cartesian.getY()), 2.0)); } public double getX() { return this.x; } public void setX(double x) { this.x = x; } public double getY() { return this.y; } public void setY(double y) { this.y = y; } private double x; private double y; }
Java Code:public class Polar { // Price two Arg Constructor // public Polar(Cartesian cartesian) { this.rho = Math.hypot(cartesian.getX(), cartesian.getY()); this.theta = Math.atan2(cartesian.getY(),cartesian.getX()); }// End Constructor public double calculateDistancePolar(Polar polar) { return Math.sqrt(Math.pow(getRho(), 2.0) + Math.pow(polar.rho, 2.0)  2.0 * getRho() *polar.rho * Math.cos(polar.theta  getTheta())); }// End calculateDistancePolar Method public double getTheta() { return this.theta; } public void setTheta(double theta) { this.theta = theta; } public double getRho() { return this.rho; } public void setRho(double rho) { this.rho = rho; } private double theta; private double rho;
 03182014, 05:42 PM #2
Re: coordinate converter
when I create the instance of polar or cartesian I dont know what to send to it
How do you create an instance of either class without using an instance of the other class?If you don't understand my response, don't ignore it, ask a question.
 03182014, 06:33 PM #3Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
Re: coordinate converter
Ok I have this now. Im still a bit stuck
im trying to use this line to run the Polar distance calculator but it is looking for it within the cart class.
excerpt:Java Code:double distance = startPoint.calculateDistancePolar(endPoint);
Java Code:public class Test { public static void main(String[] args) { //TestConversion test = new TestConversion(); System.out.println("I made it to here test"); Test.TestConversion(); } public static double[] TestConversion() { System.out.println("X,Y Coordinates are:"); double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90}; double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710}; double cartesianDistance =0; double polarDistance = 0; System.out.println("I made it to here test"); Cartesian startPoint = new Cartesian(new Polar(startPoint)); Cartesian endPoint = new Cartesian(new Polar(endPoint)); for ( int row = 0; row < xCoords.length  1; row++ ) { // loop through columns of current row startPoint.setX(xCoords[ row ]); startPoint.setY(yCoords[ row ]); endPoint.setX(xCoords[ row + 1 ]); endPoint.setY(yCoords[ row + 1 ]); double totalDistance = 0; double distance = startPoint.getDistance(endPoint); totalDistance = totalDistance + distance; cartesianDistance = totalDistance; } for ( int row = 0; row < xCoords.length  1; row++ ) { // loop through columns of current row startPoint.setX(xCoords[ row ]); startPoint.setY(yCoords[ row ]); endPoint.setX(xCoords[ row + 1 ]); endPoint.setY(yCoords[ row + 1 ]); double totalDistance = 0; double distance = startPoint.calculateDistancePolar(endPoint); totalDistance = totalDistance + distance; polarDistance = totalDistance; } double[] bothDistances = {cartesianDistance, polarDistance}; System.out.println("Cartesian Distance test =" +cartesianDistance++); System.out.println("Polar Distance test =" +polarDistance++); return bothDistances; } // end Test }
 03182014, 06:52 PM #4
Re: coordinate converter
use this line to run the Polar distance calculator but it is looking for it within the cart class.
what class is startPoint?If you don't understand my response, don't ignore it, ask a question.
 03182014, 07:14 PM #5Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
Re: coordinate converter
I dont really know. startPoint is cartesian. which is why its being screwed up and that makes sense.
XML Code:Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method calculateDistancePolar(Cartesian) is undefined for the type Cartesian at Test.TestConversion(Test.java:60) at Test.main(Test.java:15) I made it to here test
And now it errors at line 24 and 25 with this error:
XML Code:I made it to here test Exception in thread "main" java.lang.Error: Unresolved compilation problems: The local variable startPoint may not have been initialized The local variable endPoint may not have been initialized at Test.TestConversion(Test.java:29) at Test.main(Test.java:15)
Java Code:public class Test { public static void main(String[] args) { //TestConversion test = new TestConversion(); System.out.println("I made it to here test"); Test.TestConversion(); } public static double[] TestConversion() { System.out.println("X,Y Coordinates are:"); double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90}; double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710}; double cartesianDistance =0; double polarDistance = 0; System.out.println("I made it to here test"); Cartesian startPoint = new Cartesian(new Polar(startPoint)); Cartesian endPoint = new Cartesian(new Polar(endPoint)); Polar polar1 = new Polar(startPoint); Polar polar2 = new Polar(endPoint); for ( int row = 0; row < xCoords.length  1; row++ ) { // loop through columns of current row startPoint.setX(xCoords[ row ]); startPoint.setY(yCoords[ row ]); endPoint.setX(xCoords[ row + 1 ]); endPoint.setY(yCoords[ row + 1 ]); double totalDistance = 0; double distance = startPoint.getDistance(endPoint); totalDistance = totalDistance + distance; cartesianDistance = totalDistance; } for ( int row = 0; row < xCoords.length  1; row++ ) { // loop through columns of current row startPoint.setX(xCoords[ row ]); startPoint.setY(yCoords[ row ]); endPoint.setX(xCoords[ row + 1 ]); endPoint.setY(yCoords[ row + 1 ]); double totalDistance = 0; double distance = polar1.calculateDistancePolar(polar2); totalDistance = totalDistance + distance; polarDistance = totalDistance; } double[] bothDistances = {cartesianDistance, polarDistance}; System.out.println("Cartesian Distance test =" +cartesianDistance++); System.out.println("Polar Distance test =" +polarDistance++); return bothDistances; } // end Test }
 03182014, 07:21 PM #6
Re: coordinate converter
That doesn't make sense. The code is using the value of the variables BEFORE they have been given any value. That is why the compiler says: startPoint may not have been initialized
The code needs to assign startPoint a value BEFORE trying to use that value.
Did you see this question I asked earlier:
How do you create an instance of either class without using an instance of the other class?If you don't understand my response, don't ignore it, ask a question.
 03182014, 09:09 PM #7Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
 03182014, 09:17 PM #8
Re: coordinate converter
Both classes need constructors that do not require an instance of the other class.
Your assignment or class material must describe how to create instances of the class from numbers for example.
Those constructors can be used to create startPoint and endPoint.If you don't understand my response, don't ignore it, ask a question.
 03182014, 09:48 PM #9Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
Re: coordinate converter
hmm, gotcha that is how I initially had created my classes but then this is what my instructor showed us this to use. What if I just have the cartesian class takes an input of x and y doubles in the constructor and leave the polar class constructor alone. That should work, right?
 03182014, 09:56 PM #10
Re: coordinate converter
What are the assignment's requirements?
In general I'd think both classes should have constructors that take numbers. Ask your instructor.If you don't understand my response, don't ignore it, ask a question.
 03192014, 08:23 PM #11Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
Re: coordinate converter
He just wants 2 classes, both to convert cart to polar and visversa in the constructor, then one method to calculate distance between two points. Then we got a matrix of ten cartesian points we have to use to test.
I think I have it figured out, Im having a little trouble figuring out how to write an if statement saying I if its at the 2nd to last value of the array do... line 39 of test class.
Let me know if you see anything real weird I might have done, I still have to go through and check everything but the values for the total distances for both polar and Cartesian are the same which is a good sign
Java Code:import java.util.Arrays; public class Test { public static void main(String[] args) { //TestConversion test = new TestConversion(); Test.TestConversion(); } public static double[] TestConversion() { System.out.println("X,Y Coordinates are:"); double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90}; double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710}; double[] rhoCoords = new double[10]; double[] thetaCoords = new double[10]; double cartesianDistance =0; double polarDistance = 0; /* Cartesian startPoint = new Cartesian(new Polar(startPoint)); Cartesian endPoint = new Cartesian(new Polar(endPoint)); Polar polar1 = new Polar(startPoint); Polar polar2 = new Polar(endPoint); */ for ( int row = 0; row < xCoords.length  1; row++ ) { Cartesian startPoint = new Cartesian(new Polar(xCoords[row], yCoords[row])); Cartesian endPoint = new Cartesian(new Polar(xCoords[row+1], yCoords[row+1])); rhoCoords [row] = new Polar(xCoords[row], yCoords[row]).getRho(); thetaCoords [row] = new Polar(xCoords[row], yCoords[row]).getTheta(); if (row.equals(xCoords.length  1)) { rhoCoords [row+1] = new Polar(xCoords[row+1], yCoords[row+1]).getRho(); thetaCoords [row+1] = new Polar(xCoords[row+1], yCoords[row+1]).getTheta(); } double cartTotalDistance = 0; double cartDistance = startPoint.getDistance(endPoint); cartTotalDistance = cartTotalDistance + cartDistance; cartesianDistance = cartTotalDistance; Polar polar1 = new Polar(xCoords[row], yCoords[row]); Polar polar2 = new Polar(xCoords[row+1], yCoords[row+1]); double polarTotalDistance = 0; double polarDistance1 = polar1.calculateDistancePolar(polar2); polarTotalDistance = polarTotalDistance +polarDistance1; polarDistance = polarTotalDistance; } double[] bothDistances = {cartesianDistance, polarDistance}; System.out.println(Arrays.toString(thetaCoords)); System.out.println(Arrays.toString(rhoCoords)); System.out.println("Cartesian Distance test =" +cartesianDistance++); System.out.println("Polar Distance test =" +polarDistance++); return bothDistances; } // end Test }
Java Code:public class Polar { // Price two Arg Constructor // public Polar(double x, double y) { this.rho = Math.hypot(x, y); this.theta = Math.atan2(y,x); }// End Constructor public double calculateDistancePolar(Polar polar) { return Math.sqrt(Math.pow(getRho(), 2.0) + Math.pow(polar.rho, 2.0)  2.0 * getRho() *polar.rho * Math.cos(polar.theta  getTheta())); }// End calculateDistancePolar Method public double getTheta() { return this.theta; } public void setTheta(double theta) { this.theta = theta; } public double getRho() { return this.rho; } public void setRho(double rho) { this.rho = rho; } private double theta; private double rho; }
Java Code:public class Cartesian { // Price two Arg Constructor public Cartesian(Polar polar) { this.x = polar.getRho() * Math.cos(polar.getTheta()); this.y = polar.getRho() * Math.sin(polar.getTheta()); }// End Constructor public double getDistance(Cartesian cartesian) { return Math.sqrt(Math.pow((getX()  cartesian.getX()), 2.0) + Math.pow((getY()  cartesian.getY()), 2.0)); } public double getX() { return this.x; } public void setX(double x) { this.x = x; } public double getY() { return this.y; } public void setY(double y) { this.y = y; } private double x; private double y; }
 03192014, 08:33 PM #12
Re: coordinate converter
It still seems that there should be a constructor for the Cartesian class that does not require a Polar object.
Also why do the methods that get the distance have different names?If you don't understand my response, don't ignore it, ask a question.
 03192014, 08:46 PM #13Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
Re: coordinate converter
method question is a good point. I can change that, would make more sense. And would I create a 2nd constructor (question just for my future knowledge can you have multiple constructors?)? or do you mean just make the constructor so it take the polar coords?
 03192014, 08:55 PM #14
Re: coordinate converter
can you have multiple constructors?
Have a second constructor that takes x and y.If you don't understand my response, don't ignore it, ask a question.
 03192014, 09:01 PM #15Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
 03202014, 08:03 PM #16Member
 Join Date
 Mar 2014
 Posts
 9
 Rep Power
 0
Re: coordinate converter
Awesome, thanks for your help. Its working beautifully now.
Java Code:public class Cartesian { //Cartesian two Arg Constructor public Cartesian(double rho, double theta) { this.x = rho * Math.cos(theta); this.y = rho * Math.sin(theta); }//End Constructor //Cartesian Polar Object Constructor public Cartesian(Polar polar) { this.x = polar.getRho() * Math.cos(polar.getTheta()); this.y = polar.getRho() * Math.sin(polar.getTheta()); }// End Constructor public double getDistance(Cartesian cartesian) { return Math.sqrt(Math.pow((getX()  cartesian.getX()), 2.0) + Math.pow((getY()  cartesian.getY()), 2.0)); } public double getX() { return this.x; } public void setX(double x) { this.x = x; } public double getY() { return this.y; } public void setY(double y) { this.y = y; } private double x; private double y; }
Java Code:public class Polar { // Polar two Arg Constructor public Polar(double x, double y) { this.rho = Math.hypot(x, y); this.theta = Math.atan2(y, x); }// End Constructor //Polar Cartesian Object Constructor public Polar(Cartesian cartesian) { this.rho = Math.hypot(cartesian.getX(), cartesian.getY()); this.theta = Math.atan2(cartesian.getY(), cartesian.getX()); }// End Constructor public double getDistance(Polar polar) { return Math.sqrt(Math.pow(getRho(), 2.0) + Math.pow(polar.rho, 2.0)  2.0 * getRho() * polar.rho * Math.cos(polar.theta  getTheta())); }// End calculateDistancePolar Method public double getTheta() { return this.theta; } public void setTheta(double theta) { this.theta = theta; } public double getRho() { return this.rho; } public void setRho(double rho) { this.rho = rho; } private double theta; private double rho; }
Java Code:import java.util.Arrays; public class Test { public static void main(String[] args) { //TestConversion test = new TestConversion(); Test.TestConversion(); } public static double[] TestConversion() { double[] xCoords = {86.92, 70.93, 97.74, 30.90, 51.66, 0.830, 55.91, 32.92, 65.26, 83.90}; double[] yCoords = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710}; double[] rhoCoords = new double[10]; double[] thetaCoords = new double[10]; double cartesianDistance =0; double polarDistance = 0; double cartTotalDistance = 0; double polarTotalDistance = 0; for ( int row = 0; row < xCoords.length  1; row++ ) { Cartesian startPoint = new Cartesian(new Polar(xCoords[row], yCoords[row])); Cartesian endPoint = new Cartesian(new Polar(xCoords[row+1], yCoords[row+1])); rhoCoords [row] = new Polar(xCoords[row], yCoords[row]).getRho(); thetaCoords [row] = new Polar(xCoords[row], yCoords[row]).getTheta(); if (row == xCoords.length  2) { rhoCoords [row+1] = new Polar(xCoords[row+1], yCoords[row+1]).getRho(); thetaCoords [row+1] = new Polar(xCoords[row+1], yCoords[row+1]).getTheta(); } double cartDistance = startPoint.getDistance(endPoint); cartTotalDistance = cartTotalDistance + cartDistance; cartesianDistance = cartTotalDistance; Polar polar1 = new Polar(xCoords[row], yCoords[row]); Polar polar2 = new Polar(xCoords[row+1], yCoords[row+1]); double polarDistance1 = polar1.getDistance(polar2); polarTotalDistance = polarTotalDistance +polarDistance1; polarDistance = polarTotalDistance; System.out.println("Coordinate Distances added up by step:"); System.out.println("Cartesian Distance test" +row+ " =" +cartesianDistance++); System.out.println("Polar Distance test " +row+ " =" +polarDistance++); } System.out.println(""); double[] bothDistances = {cartesianDistance, polarDistance}; System.out.println("theta coordinates are:"); System.out.println(Arrays.toString(thetaCoords)); System.out.println("rho coordinates are:"); System.out.println(Arrays.toString(rhoCoords)); System.out.println("Cartesian Distance test =" +cartesianDistance++); System.out.println("Polar Distance test =" +polarDistance++); return bothDistances; } // end Test }
Similar Threads

java x,y coordinate
By robc in forum Advanced JavaReplies: 7Last Post: 05292010, 06:11 AM 
Convert Cartesian coordinate system into java coordinate system?
By 123 in forum Java 2DReplies: 3Last Post: 02072010, 08:34 PM 
Ovals with the same X coordinate and different radiuses
By veseo in forum Java 2DReplies: 3Last Post: 12152009, 09:20 PM 
set coordinate to an image
By nuur in forum AWT / SwingReplies: 2Last Post: 04012008, 08:08 AM
Bookmarks