Standard practice when you have a programming problem:
Try something simpler and see whether that works.Try it in little bits and get each bit working before you put the whole thing together. Suggest you ask one of the sheriffs or bartenders to transfer this
thread to beginners' because I think it is really a beginners' problem. Also suggest you ask them to close the other threads you have started because it gets very confusing when you have several discussions on different threads.
Have you carried out my first suggestion? Have you drawn it on paper? Draw a vertical line across the middle of the page with
y at its top and
-y at its bottom. Draw a horizontal line across the middle of the paper with
x at the right end and
-x on the left. Mark the place where the two lines cross (0, 0). Draw a circle centred on (0, 0) and call its radius
r. compare what you now have with the "polar coordinates" applet examples on the univie website I quoted yesterday. If you use that applet, you can move "P" around, and it shows
r and
phi at the top left, and
x and
y at the bottom right. Play with that applet, see what happens when you move "P" around. Try it with "degrees" and also with "radians." It is a nice little applet, which makes it easy to see what polar coordinates mean. If you click "coordinate lines" you can see the circles.
Start from scratch.
Set up a Point class (beware: there is another Point class in java.awt), with:
Private double x and y fields,A constructor to set x and y,the usual set and get methods,a toString() method like this: return "Point at x = " + x + ", y = " + y;Play with your Point objects until you have got them working nicely.
Find out whether there are any methods in the Math class which return sqrt(x2+y2). You can get
r from sqrt(x2+y2). Try it with new Point(1.0, 1.0) and see what you get as
r. Hint: It ought to start with 1.414.
Imagine you are rotating around (0, 0). You can get
r by using
myPoint.getX() and
myPoint.getY(). Try again with various values:
Try 1, 1. Should start with 1.414.Try 1, -1 or -1, -1. Should give exactly the same result as 1, 1.Try 3, 4. Should give 5.0.Try 12, 5. Should give 13. You already seem to know how to use atan2 to get the starting angle (called
phi on the univie applet, called
theta in the Math class API documentation). Try those examples, then try using
r * cos
theta for
x, and
r * sin
theta for
y. Set
x and
y in your Point to those values. Try using the same values as in the previous examples. Print out
theta which will probably be in radians, and see what it is. Print out the toString method (use System.out.println(myPoint);) and see what you have got for
x and
y. They should be the same figures you started with, would you believe.
Go through the Math class. Find out whether you need degrees or radians. Find out whether there are any methods which convert degrees to radians or
vice versa.
Now get a rotate method which simply adds something to
theta before you set the
x and
y. Now try the whole thing again, and see what happens. If you rotate (1, 1) through 90� you ought to get (-1, 1) and another 90� will give you (-1, -1). Rotating (3, 4) through 90� will give (4, -3) I think. Try a few examples, eg (2, 3) and then move a point on the univie applet and see whether you get the same results. You ought not to change
r if you are rotating in a circle.
Rotating around a point which is not (0, 0) use
otherPoint.getX() - myPoint.getX() for
x, and similar for
y. When you have altered
theta your new
x will be something like otherPoint.getX() +
r * sin(
theta) and similar for
y.
That lot should give you some sort of starting point!
[ December 22, 2007: Message edited by: Campbell Ritchie ]