Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting Polar Points to Cartesian Points  RSS feed

 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!