# 360degree rotation in Java2D

Discussion in 'Java' started by Will Clark, Aug 28, 2003.

1. ### Will ClarkGuest

I have a bit of a problem rotating Shapes using the Graphics2D function
rotate(theta).

My high-school trig lessons indeed tell me that sine and cosine functions go
between -90 degrees and +90 degrees (or 0 and 180, if you like), and I can
see why this means that when I call the rotate(...) function, it will not
rotate the object all the way round the whole 360 degrees.

Currently, I have a duplicate Shape object which is flipped so that for one
half of the rotation the original shape is drawn, and for the second half,
the other...

There must be a better way!

Any ideas?

Cheers )

Will

Will Clark, Aug 28, 2003

2. ### Will ClarkGuest

Yep, sorted now!

Cheers again, it was in the use of the Math.atan function... grrr, silly
mistakes!

"Will Clark" <> wrote in message
news:bilp7l\$5sn\$...
> Yeah, I was using radians, but the problem remains...
>
> What's even more annoying is that I just wrote a ten line example program

to
> show you the problem I'm having, and it isn't a problem in my test applet!
>
> So I must have made a mistake somewhere else in my code, and its only
> becoming visible through the rotation. I'll suss it out somehow!
>
> Back to the drawing board (no pun intended too much!)
>
> Cheers for your thoughts though )
>
> "Eric Sosman" <> wrote in message
> news:...
> > Will Clark wrote:
> > >
> > > I have a bit of a problem rotating Shapes using the Graphics2D

function
> > > rotate(theta).
> > >
> > > My high-school trig lessons indeed tell me that sine and cosine

> functions go
> > > between -90 degrees and +90 degrees (or 0 and 180, if you like), and I

> can
> > > see why this means that when I call the rotate(...) function, it will

> not
> > > rotate the object all the way round the whole 360 degrees.

> >
> > Say what? Sine and cosine are defined for all numbers, not
> > just for a half-circle's worth. Yes, they're periodic, but the
> > period is the full circle.
> >
> > Just a guess at possible sources of your trouble (I'm not
> > entirely sure what your trouble *is*):
> >
> > - The argument to awt.Graphics2D.rotate(double) is
> > an angle expressed in radians, not in degrees. Use
> > Math.toRadians(double) and Math.toDegrees(double) to
> > convert back and forth if needed.
> >
> > - If you rotate a shape by 2*pi radians (360 degrees),
> > you may have some difficulty determining whether
> > anything actually happened ...
> >
> > If neither of these helps clear up your problem, post your
> > code for study.
> >
> > --
> >

>
>

Will Clark, Aug 28, 2003

3. ### Roedy GreenGuest

On Thu, 28 Aug 2003 20:47:25 +0000 (UTC), "Will Clark"
<> wrote or quoted :

>Cheers again, it was in the use of the Math.atan function... grrr, silly
>mistakes!

Mathematically arctan has many possible solutions. Any atan
implementation has to pick a range for its canonical solutions to fall
in.

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green, Aug 28, 2003
4. ### Knute JohnsonGuest

Will Clark wrote:
> Yep, sorted now!
>
> Cheers again, it was in the use of the Math.atan function... grrr, silly
> mistakes!
>
> "Will Clark" <> wrote in message
> news:bilp7l\$5sn\$...
>
>>Yeah, I was using radians, but the problem remains...
>>
>>What's even more annoying is that I just wrote a ten line example program

>
> to
>
>>show you the problem I'm having, and it isn't a problem in my test applet!
>>
>>So I must have made a mistake somewhere else in my code, and its only
>>becoming visible through the rotation. I'll suss it out somehow!
>>
>>Back to the drawing board (no pun intended too much!)
>>
>>Cheers for your thoughts though )
>>
>>"Eric Sosman" <> wrote in message
>>news:...
>>
>>>Will Clark wrote:
>>>
>>>>I have a bit of a problem rotating Shapes using the Graphics2D

>
> function
>
>>>>rotate(theta).
>>>>
>>>>My high-school trig lessons indeed tell me that sine and cosine

>>
>>functions go
>>
>>>>between -90 degrees and +90 degrees (or 0 and 180, if you like), and I

>>
>>can
>>
>>>>see why this means that when I call the rotate(...) function, it will

>>
>>not
>>
>>>>rotate the object all the way round the whole 360 degrees.
>>>
>>> Say what? Sine and cosine are defined for all numbers, not
>>>just for a half-circle's worth. Yes, they're periodic, but the
>>>period is the full circle.
>>>
>>> Just a guess at possible sources of your trouble (I'm not
>>>entirely sure what your trouble *is*):
>>>
>>> - The argument to awt.Graphics2D.rotate(double) is
>>>an angle expressed in radians, not in degrees. Use
>>>convert back and forth if needed.
>>>
>>> - If you rotate a shape by 2*pi radians (360 degrees),
>>>you may have some difficulty determining whether
>>>anything actually happened ...
>>>
>>> If neither of these helps clear up your problem, post your
>>>code for study.
>>>
>>>--
>>>

>>
>>

>
>

Remember that Math.atan returns theta in -pi/2 to pi/2 and that
Math.atan2 returns theta in -pi to pi.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

Knute Johnson, Sep 4, 2003