rotate "this" Point around a given center point, with the given angle, in a clock wised offset

i tried to make this function but it only work in one or two cases i dont have any idea how to build it otherwise x -represents the x axes value of the point y -represents the y axes value of the point

public void rotate(Point center,double angle) { double rad;

Please use code tags round quoted code; it makes it easier to read.

Draw the thing on paper. You will probably end up with something which looks like a clock face.

You need a bit of maths.

Go and find the java.lang.Math class, and find out which method you can use to convert Cartesian (rectangular) coordinates to polar. (Hint: use ctrl-F and look for "theta").

Find out what that method returns, and find out which units it is in.

Find out whether there are any methods useful for converting units of angles.

There is no need for the reduction by 360�. The sin and cos methods can take any argument.

Work out how to get the other part of your polar coordinates. Ask Dr Pythagoras.

Remember angles are conventionally drawn anticlockwise with 0 = 3o'clock.

If you rotate clockwise, subtract the rotation from the angles you get.

You should now be able to work out a radius and an angle; I am assuming the radius won't change

Use sin and cos to convert your polar coordinates back to Cartesian (rectangular).

This is the definition of modulo arithmetic. Either of the following is equivalent:

I'm also guessing from a brief look at your code that you're assuming the initial angle between this and point is 0. You first need to determine the angle between this and point. You then need to sweep it through the angle requested. (By using the trig functions, it doesn't matter whether you "normalize" the angle to less than 360� - they'll do it for you.) [ December 21, 2007: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);

johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78

posted

0

i tried to make this fuction i just dont know

some cases are working when i am adding PI

some are working whithout adding PI

i have built to codes for each case i dont know if the cases are defined correctly i am realy clueless about solving this problem

[ December 21, 2007: Message edited by: johny doe ] [ December 21, 2007: Message edited by: johny doe ]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052

28

posted

0

Have you understood the maths behind the rotation method? Do you know about polar coordinates? I did a Google search and this link seemed promising. This Wikipedia page also seems good. You have posted some similar posts which appeared to show you had got the problem sorted, only you had E-17 in your answer.

It is a lot easier if you always rotate around (0, 0). Polar coordinates give two values. The first is r, which tells you how far your point is from the origin [(0, 0) point] in a straight line. In fact your point will lie on a circle radius r, centred on the origin. Draw a pair of x and y axes on a sheet of paper, and then any other point. Get a pair of compasses and draw a circle centred on where the two axes cross, so the circle passes through the other point. Now when you rotate the point it will move round that circle. I presume that is what you were looking for?

Remember that a complete turn round the circle is PI radians. Also that 0 represents 3 o'clock and we usually turn anticlockwise. What you need to work out is that andAs you will see in the Wikipedia article, you can get x from r cos(theta) and y from r sin(theta). You don't need to calculate the slope. All you need to do is put x - centre and y - centre into the atan2 method to get theta. If centre is 0, then you just use x and y. There is no need to use the % remainder operator; the sin and cos methods can take arguments of any value.

I have told you before to draw it on a sheet of paper. Have you? Work out rectangular and polar coordinates for a few examples, using the applets in the first website I quoted to help. Use radians rather than degrees if possible. You appear to have got the bit about sin and cos right, but rotating (0, 0) 90� clockwise around (2, 1) should give (1, 3) not (3, 1).

I hope I have made things better and not confused you even more. Tell us what you get, and whether it works. I have a horrible feeling you have reached the stage where you are trying too hard. Go and drink some beer and relax and then get the pencil and paper out.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052

28

posted

0

There appears to be a Math.hypot() method which makes it easier to calculate the radius. Look it up.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052

28

posted

0

Have you had any luck with it yet? I have just noticed I said a complete turn is PI radians, which is a mistake. It is actually 2PI. I am beginning to think you have been given a maths exercise rather than a computing exercise.

johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78

posted

0

actually i lack the math knowledge on this subject i read polar angles as i showed before i constructed a working model

but there was a certain case that didnt work so i solved it by tryle and error method the answer was (add +PI) but this fix up didnt let me solve the previos cases that worked

i dont know why ..

i tried to construct some cases that in a certain case go the normal , on the other case go to the +PI fixup

but it didnt work because i dont know how to sort this cases and how many cases are there and how to deal with them.

there is some big math theory that i am missing regarding this rotation stuff. [ December 22, 2007: Message edited by: johny doe ]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052

28

posted

0

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 * costheta for x, and r * sintheta 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 ]

johny doe
Ranch Hand

Joined: Dec 07, 2007
Posts: 78

posted

0

thanks for trying to help but my problem is not with java commands i know how to find every component of the problem(radius ,angles,sin cos) i know the syntax my big problem is math i dont know the mathematecal theory about it and i cant find it no where in google

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052

28

posted

0

I thought you had been given a problem where it was the maths that was the difficulty . . .

I shall try to help you, but if you can't understand me you need to find a friend locally who knows some maths.

Draw the two lines (axes) to make a + shaped cross right across your sheet of paper with the crossing in the middle. You start off with rectangular coordinates, where you have x and y. The x bit means how far you are to the right of the + (x = -3 means you are 3 to the left). The y bit is similar, but how far you are above the +.

Go to this website and click the red button called "Cartesian coordinates." You get the x and y numbers at the top left of the window; as you move your mouse you can see where they are; "cursor (3, -2)" means your mouse arrow is at the position where x = 3 and y = -2. Try it. Move around the applet. Click "coordinate lines" and you get squares on the screen which help you find your way around. Keep doing it and watching the numbers until you are familiar with the x and y values. Same with the "coordinate system" applet, which is very similar.

Now run the "polar coordinates" applet. [I don't think the "oblique coordinates" applet is any use to you at present.] Click coordinate lines so you get circles on the screen. Each circle is 1 larger than its inside neighbour, and the straight lines radiate at 15� (PI/12 radian) intervals. At the top left it gives you r and phi and the familiar xy values are at the bottom right. If you move the point around the circles you can see that r means how far from the centre point you are, and phi which direction you have to go in, to find your point. Move your pointer around so it follows the circles. You will see how phi changes, but r stays the same (if you have a very steady hand). You can also see from the curved arrow, which angle phi represents. You can't see from this applet that you can go round twice and get 720� (4PI radians) or you can go round thrice and get 1080� (6PI radians). You also can't see from the applet that sin(45) is the same as sin(45 + 360) and that is the same as sin(45 + 720). But that's true.

You can see that you can get the angle by using tan(phi) = y / x, and the distance r is equal to square root (xsquared + ysquared). If you have the angle phi you can get x from r * cos(phi) and you get y from r * sin(phi). If you rotate through a new angle rot, the new x is r * cos(phi + rot), and the new y is r * sin(phi + rot). The angle is called theta in the Math class API.

You don't need to use remainder operators; the sin and cos methods will give a correct answer even if you pass more than 2PI.

If you can't understand the maths, please find a friend locally who knows some maths and get them to help.

This class can generate rotation transformations for you. Look into the static methods of the class. It can also concatenate transformations - e.g. rotate by 67.2 degrees then translate by 2.465 in the X direction. Although it is part of AWT, you can use it just to do maths, rather than actually doing graphics.

I think you can do rotation about somewhere other than the origin, by translating, applying the rotation, then reversing the translation. [ December 24, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052

28

posted

0

I am not convinced that Affine Transform is what he wants. I think this is a non-GUI assignment where the idea is to rotate the point (4, 5) 90� around the point (1, 1) as a centre of rotation, and all it has to do is print its location (I think, just off the top of my head, (-4, 5)).

The problem is that he doesn't understand the maths; I think it is not fair to give somebody an assignment requiring more maths than they actually know, without explaining it in words of one syllable. I have tried to explain the maths, and seem to have failed.

I cannot understand the formulae because of the smilies, but they look too complicated to be right. Please edit the post, and tick the box at the lower left beyond the bounds of the message box saying "disable smilies in this post." I actually think the formulae should read something like,

rr = hypot(x1 - x0, y1 - y0)

theta = atan2(x1 - x0, y1 - y0)

y2 = y0 + rr * sin(theta + rot)

x2 = x0 + rr * sin(theta + rot)

where

The "centre" point has the location (x0, y0)

the original position of the point to be moved is (x1, y1)

the point is to be moved to the position (x2, y2)

rr and theta are the two values in the polar coordinates of the original point (rr, theta)

and rot is the angle the point is to be rotated through. The angles rot and theta must be in the same units.

The methods atan2 and hypot are to be found in the java.lang.Math class, and can be imported with a static import. The distance from the centre of rotation (rr) should not change during the proceedings.

[edit]Wrote rr instead of a single letter for one of the polar coordinate values because the Forum won't take the single letter.[/edit] [ December 24, 2007: Message edited by: Campbell Ritchie ]

[Campbell]: I cannot understand the formulae because of the smilies, but they look too complicated to be right.

I edited the posts to remove the smilies. I don't see what's too complicated there. Considering that the formulas give the final values of x and y in just two lines, they seem simpler than solutions requiring you to calculate intermediate results like rr and theta. Those are the formulas I would use here. You can view them as the composition of three steps:

1. Move origin to x0, y0:

x1 = x - x0 y1 = y - y0

2. Rotate about origin by an angle of β:

x2 = x1 cos β - y1 sin β y2 = x1 sin β + y1 cos β

3. Move origin back to its original position, from x0, y0

x3 = x2 + x0 y3 = y2 + x0

These are the same three steps Peter referred to: "I think you can do rotation about somewhere other than the origin, by translating, applying the rotation, then reversing the translation." By substituting the first two equations into the third, you get the resultant equations which Johnny found. [ December 24, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052

28

posted

0

Sorry for not replying earlier, but I can see the three steps in the formulae now the smilies are gone. Sorry, I think I was mistaken.

I hope people are having a good Christmas, and Johnny will be now he had got the application to work. I still think it is a bad thing to give people an assignment like that without explaining the maths.