Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   how to use recursive algorithm to determine all of the arrangements? (http://www.velocityreviews.com/forums/t237084-how-to-use-recursive-algorithm-to-determine-all-of-the-arrangements.html)

 index 05-03-2006 01:55 AM

how to use recursive algorithm to determine all of the arrangements?

suppose there are n elements,how can you use recursive algorithm to
determine all of the n elements' arrangements.
for example,if there is 1 element,say, A1,there is only one
arrangement, namely,{A1};
if there are 2 elements,say,A1 and A2,there are two
arrangements,namely, {A1,A2},{A2,A1};
3 elements, A1,A2 and A3, six arrangements
exist,{A1,A2,A3},{A2,A1,A3},{A3,A1,A2},{A3,A2,A1}, {A1,A3,A2},{A2,A3,A1};
.......
so if there are n elements, how can we use recursive algorithm to
determine all of these arrangements?

 index 05-03-2006 02:12 AM

Re: how to use recursive algorithm to determine all of the arrangements?

of course We know that there are n! permutations of n elements,but i
just want to make a program to print what does every permutation look
like,like
this,{A1,A2,A3},{A2,A1,A3},{A3,A1,A2},{A3,A2,A1},{ A1,A3,A2},{A2,A3,A1}.
can anyone give me some tips?

 James McGill 05-03-2006 03:54 AM

Graphics2D rotation

I'm stumped on this one. I draw a circular figure in my panel, with the
midpoint of the circle being (radius, radius). Then I want to draw
another circle over it in another layered pane, but rotated with respect
to the midpoint of the circles, not with respect to the panel.

I've tried lots and lots of combinations of translate and rotate, but I
just can't figure out what to do to make this happen. How do you rotate
a circle with respect to its midpoint?

 Rhino 05-03-2006 04:25 AM

Re: Graphics2D rotation

"James McGill" <jmcgill@cs.arizona.edu> wrote in message
news:1146628460.23965.2.camel@localhost.localdomai n...
> I'm stumped on this one. I draw a circular figure in my panel, with the
> midpoint of the circle being (radius, radius). Then I want to draw
> another circle over it in another layered pane, but rotated with respect
> to the midpoint of the circles, not with respect to the panel.
>
> I've tried lots and lots of combinations of translate and rotate, but I
> just can't figure out what to do to make this happen. How do you rotate
> a circle with respect to its midpoint?
>

Uhh, how do you tell if it worked? If I draw a circle of radius 5 units
centered on (0,0), then rotate the circle 90 degrees, how does the second
circle look any different that the first?

If this is a homework assignment, maybe you've read it incorrectly??

--
Rhino

 Simon 05-03-2006 07:32 AM

Re: how to use recursive algorithm to determine all of the arrangements?

index wrote:
> of course We know that there are n! permutations of n elements,but i
> just want to make a program to print what does every permutation look
> like,like
> this,{A1,A2,A3},{A2,A1,A3},{A3,A1,A2},{A3,A2,A1},{ A1,A3,A2},{A2,A3,A1}.
> can anyone give me some tips?

Remove the last element An and consider all arrangements for the reduced set.
Then insert An at every possible position.

Cheers,
Simon

 Simon 05-03-2006 07:59 AM

Re: Graphics2D rotation

James McGill wrote:
> I'm stumped on this one. I draw a circular figure in my panel, with the
> midpoint of the circle being (radius, radius). Then I want to draw
> another circle over it in another layered pane, but rotated with respect
> to the midpoint of the circles, not with respect to the panel.

I'm not sure about your question, so let me restate it as I understand it. I
assume that you do not want to rotate the circle itself (because that doesn't
make much sense) but rather the center of a second object (which is again a
circle?) with respect to the origin of the first.

You have a circle at point (r,r) with radius r, i.e., it looks like this:
(use a fixed width font)

| o
| ________/
| / / \
|/ / \
| / |
r| x |
| |
|\ /
|_\_________/____
r

Now you want to draw another figure that is positioned relative to the center of
the circle at position o given in terms of an angle and a distance d from the
center x (perhaps you want it on the circumference, i.e., d=r)? Then you can do
the following:

1. Translate the graphics context to the center of the circle, i.e. translate(r,r)
2. Perform your rotation (around the origin which now coincides with the center
of the circle)
3. Translate the origin again to move away from the origin by d arriving at "o"
in the above drawing. (E.g., call translate(d,0).)
4. Draw the second object.

Is that what you want to do?

> I've tried lots and lots of combinations of translate and rotate, but I
> just can't figure out what to do to make this happen. How do you rotate
> a circle with respect to its midpoint?

^^^^^^^^
Just to be sure: Maybe your problems are caused by a misunderstaning in the
usage of Graphics: calling translate() or rotate() or any other method of
Graphics does not move or rotate the things you have already drawn. You cannot
translate or rotate the circle but rather the graphics context itself. This has
an influence only on the objects you draw afterwards.

Hope this helps,
Simon

 James McGill 05-03-2006 05:01 PM

Re: Graphics2D rotation

On Wed, 2006-05-03 at 09:59 +0200, Simon wrote:
> James McGill wrote:
> > I'm stumped on this one. I draw a circular figure in my panel, with the
> > midpoint of the circle being (radius, radius). Then I want to draw
> > another circle over it in another layered pane, but rotated with respect
> > to the midpoint of the circles, not with respect to the panel.

>
> I'm not sure about your question, so let me restate it as I understand it. I
> assume that you do not want to rotate the circle itself (because that doesn't
> make much sense) but rather the center of a second object (which is again a
> circle?) with respect to the origin of the first.

I make a buffered image for the base layer, which is a circle.
Then I make another buffered image for the top layer, which is also a
circle, the same size.

I want to animate the second image on top of the first, I guess I'd call
it a "pinwheel" effect. So each timer tick, the top layer should be
redrawn in its next step of rotation.
This worked for my purpose:

...
// first translate
// then rotate
// then translate back
g2.drawImage(topLayerImage, 0, 0, this);
...

So I think I have to translate the origin back after rotating becuase
the image itself is oriented to that space, correct?

 James McGill 05-04-2006 12:08 AM

Re: Graphics2D rotation

On Wed, 2006-05-03 at 00:25 -0400, Rhino wrote:

> > I've tried lots and lots of combinations of translate and rotate, but I
> > just can't figure out what to do to make this happen. How do you rotate
> > a circle with respect to its midpoint?
> >

> Uhh, how do you tell if it worked? If I draw a circle of radius 5 units
> centered on (0,0), then rotate the circle 90 degrees, how does the second
> circle look any different that the first?

It's a circular image with a pattern, not just a circle. I was trying
to keep my question simple and focused on how to do rotates.

> If this is a homework assignment, maybe you've read it incorrectly??

I'm writing a game and I'm fairly new with the Java2D api. I'm just now
figuring out how to deal with JLayeredPanes. So one of the elements of
my game is an image on one layer with another moving image on its own
layer.

Thanks,

James

 Simon 05-04-2006 07:38 AM

Re: Graphics2D rotation

James McGill wrote:
> I make a buffered image for the base layer, which is a circle.
> Then I make another buffered image for the top layer, which is also a
> circle, the same size.
>
> I want to animate the second image on top of the first, I guess I'd call
> it a "pinwheel" effect. So each timer tick, the top layer should be
> redrawn in its next step of rotation.
> This worked for my purpose:
>
> ...
> // first translate
> // then rotate
> // then translate back
> g2.drawImage(topLayerImage, 0, 0, this);
> ...
>
>
> So I think I have to translate the origin back after rotating becuase
> the image itself is oriented to that space, correct?

Ok, now I see the problem. You are drawing an image and that does not have the
center at the "origin" but at the upper left corner. I thought you were drawing
a Shape which typically has the center at the origin. Therefore, you need the
second translation. I would assume that your code should actually solve the
problem, doesn't it?

One suggestion: Once you are sure about which transformations you need,
aggregate all of them into a single AffineTransform and apply it once when
rendering the image. There are also variants of the drawImage methods that
accept an AffineTransform. Depending on how quickly your wheel rotates and how
much memory you can affort, it might be even better to generate rotated copies
of the image in a preprocessing phase so you don't have to perform the time
consuming rotation for each frame.

Cheers,
Simon

 Thomas Weidenfeller 05-04-2006 08:03 AM

Re: Graphics2D rotation

James McGill wrote:
> I make a buffered image for the base layer, which is a circle.
> Then I make another buffered image for the top layer, which is also a
> circle, the same size.
>
> I want to animate the second image on top of the first,

I would pre-calculate a set of rotated images. The number of
pre-calculated images should depend on the desired animation speed, and
the fps rate with which your game works.

Once you have them pre-calcualated you just have to select the one
matching the the current desired wheel position and draw it with a fixed
x/y offset.

As a general hint, you are suggesting you are using JLayeredPane to
construct your animation. This doesn't sound too good. Java games
typically use multi-buffers, all manged by the game, and flip the
buffers manually. In your case one would probably compose each frame
from a fixed background buffer, plus the current animation and then flip
the buffer.

/Thomas
--
The comp.lang.java.gui FAQ: