programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

Converting Polar Points to Cartesian Points

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:
Hello! I was assigned to use a PolarPnt class that implements Point (interface). We have to complete methods like returning the x coordinate. I was wondering how to go about this? How do I convert from a polar point to cartesian point in java and then output just the x coordinate? I am assuming you call the angle and the find the cos of theta and then multiply by the hypotenuse? I just have no idea how to write this in java.

Sheriff
Posts: 27460
88
• Number of slices to send:
Optional 'thank-you' note:
To find the cosine of an angle in Java you do this:

Bear in mind that the angle has to be in radians, so if you have an angle in degrees then there's another method in the Math class to convert degrees to radians. You seem to have the required calculations under control (I think... haven't reviewed the trigonomety in detail) so that's probably what you're missing. If there's anything else, then we're here to answer questions.

Jackson Daniels
Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:
I believe I got it. However, I do have another question if you don't mind. It is regarding the method Point rot90() it is a query for a new Cartesian Point equivalent to this Cartesian point rotated by 90 degrees. Any idea how to implement this?

Paul Clapham
Sheriff
Posts: 27460
88
• Number of slices to send:
Optional 'thank-you' note:
There's two parts to that, right? The first part is that you have to create a new CartesianPoint object (is that the name of the class?) so you'd have to use one of the class's constructors. The second part is that you have to set the state of that object -- that's the internal variables -- so that it represents a point rotated 90 degrees from the other point. (Clockwise or anticlockwise? Hopefully your assignment specifies.)

The second part requires some trigonometry like what you already did. You might pass the results from that part into the CartesianPoint constructor, or you might have to create that object first and then call some method to set its state. All depends on what you've got so far.

Does that help?

 You've gotta fight it! Don't give in! Read this tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising