Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

BafflinTransform

51 views
Skip to first unread message

bob smith

unread,
Oct 3, 2012, 10:58:04 AM10/3/12
to
Am I the only one who gets incredibly confused by AffineTransform?

I never know when to preConcatenate or concatenate. Then I try both possibilities, and a lot of times neither one produces my desired result.

Any tips?

markspace

unread,
Oct 3, 2012, 11:45:55 AM10/3/12
to
I suspect some math is involved. :-)


Seriously, it's matrix algebra, and the formulas are given in each
method description. You need to work out what it is you are actually
doing first. Which you haven't told us, so I'm assuming that's the problem.


Lew

unread,
Oct 3, 2012, 1:41:15 PM10/3/12
to
markspace wrote:
> bob smith wrote:
>> Am I the only one who gets incredibly confused by AffineTransform?

No, but that isn't necessarily a reflection on the class.

>> I never know when to preConcatenate or concatenate. Then I try both
>> possibilities, and a lot of times neither one produces my desired
>> result.

http://sscce.org/

"Hey, doctor, I keep trying to get healthy. I've tried medicine and I've tried
exercise, and neither one produces my desired result."

>> Any tips?

Ask smarter questions.

> I suspect some math is involved. :-)
>
> Seriously, it's matrix algebra, and the formulas are given in each
> method description. You need to work out what it is you are actually
> doing first. Which you haven't told us, so I'm assuming that's the problem.

So show us what you're trying to accomplish, what you've written as an SSCCE, and
what precisely, with copied and pasted output, what differs from your precise
expectations.

--
Lew

Gene Wirchenko

unread,
Oct 3, 2012, 1:51:14 PM10/3/12
to
On Wed, 3 Oct 2012 10:41:15 -0700 (PDT), Lew <lewb...@gmail.com>
wrote:

>markspace wrote:
>> bob smith wrote:
>>> Am I the only one who gets incredibly confused by AffineTransform?
>
>No, but that isn't necessarily a reflection on the class.
>
>>> I never know when to preConcatenate or concatenate. Then I try both
>>> possibilities, and a lot of times neither one produces my desired
>>> result.
>
>http://sscce.org/
>
>"Hey, doctor, I keep trying to get healthy. I've tried medicine and I've tried
>exercise, and neither one produces my desired result."
>
>>> Any tips?
>
>Ask smarter questions.

Besides SSCCE, there is
http://www.catb.org/~esr/faqs/smart-questions.html
How To Ask Questions The Smart Way
by Eric Steven Raymond
It discusses at length how to properly ask technical questions.

The main benefit of following it is that people will have more,
relevant details and stand a better chance of knowing what you are
discussing and what might be done about.

A quieter, but more powerful benefit, is that to ask a technical
question properly, you have to think about your problem in a
structured manner, and often, this can lead to you being able to solve
the problem yourself.

[snip]

Sincerely,

Gene Wirchenko

Jeff Higgins

unread,
Oct 3, 2012, 3:15:38 PM10/3/12
to
On 10/03/2012 10:58 AM, bob smith wrote:
> Am I the only one who gets incredibly confused by AffineTransform?

According to the docs:
concatenate [this] = [this] x [Tx]
preconcatenate [this] = [Tx] x [this]

Lew

unread,
Oct 3, 2012, 3:49:43 PM10/3/12
to
Jeff Higgins wrote:
> bob smith wrote:
>> Am I the only one who gets incredibly confused by AffineTransform?
>
> According to the docs:
> concatenate [this] = [this] x [Tx]

<http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html#concatenate(java.awt.geom.AffineTransform)>

> preconcatenate [this] = [Tx] x [this]

<http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html#preConcatenate(java.awt.geom.AffineTransform)>

>> I never know when to preConcatenate or concatenate. Then I try both possibilities,
>> and a lot of times neither one produces my desired result.

If neither produces the desired result, then it isn't the choice of method that is the problem.

>> Any tips?

--
Lew
Build a man a fire and you warm him for an hour.
Set a man on fire and you warm him for the rest of his life.

Roedy Green

unread,
Oct 3, 2012, 7:01:35 PM10/3/12
to
On Wed, 3 Oct 2012 07:58:04 -0700 (PDT), bob smith
<b...@coolfone.comze.com> wrote, quoted or indirectly quoted someone
who said :

>Am I the only one who gets incredibly confused by AffineTransform?
>
>I never know when to preConcatenate or concatenate. Then I try both possibilities, and a lot of times neither one produces my desired result.
>
>Any tips?

see http://mindprod.com/jgloss/affinetransform.html

It has been a while since I wrote it, but it might help.
--
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex.


Arne Vajhøj

unread,
Oct 3, 2012, 9:36:38 PM10/3/12
to
Hm.

If the original poster has a limited number of specific
problems, then producing a SSCCE is a very good thing.

But I don't see any indications of that.

To me it seems as if the original poster in general find
it difficult to understand how to get it right (besides trial
and error).

It is not a SSCCE'able problem, because it is not about
specific code.

Instead it is about concepts and principles.

Arne



Gene Wirchenko

unread,
Oct 4, 2012, 12:49:38 AM10/4/12
to
On Wed, 03 Oct 2012 21:36:38 -0400, Arne Vajhøj <ar...@vajhoej.dk>
wrote:

[snip]

>Hm.
>
>If the original poster has a limited number of specific
>problems, then producing a SSCCE is a very good thing.
>
>But I don't see any indications of that.
>
>To me it seems as if the original poster in general find
>it difficult to understand how to get it right (besides trial
>and error).
>
>It is not a SSCCE'able problem, because it is not about
>specific code.
>
>Instead it is about concepts and principles.

It is still SSCCE, just not in code.

Sincerely,

Gene Wirchenko

Jeff Higgins

unread,
Oct 4, 2012, 3:26:41 AM10/4/12
to
On 10/03/2012 09:36 PM, Arne Vajh�j wrote:
>>> bob smith wrote:


> Hm.
>
> If the original poster has a limited number of specific
> problems, then producing a SSCCE is a very good thing.
>
> But I don't see any indications of that.
>
> To me it seems as if the original poster in general find
> it difficult to understand how to get it right (besides trial
> and error).
>

To me it seems rather the bob is conducting a poll or troll.
>>>> Am I the only one who gets incredibly confused by AffineTransform?

bob smith

unread,
Oct 4, 2012, 10:22:30 AM10/4/12
to
It seems like when I want to "tack on" an operation, it is actually a preConcatenate.

This is counterintuitive to me.

Also, when you are just doing commutative operations, like just translation, it doesn't matter whether you preConcatenate or concatenate.

What happened was I started with just translations and was concatenating.

Then, I added rotation, and it did not work right whether I preConcatenated or concatenated. The issue was that I should have been preConcatenating the translations that I had already programmed in.

Gene Wirchenko

unread,
Oct 4, 2012, 12:30:24 PM10/4/12
to
On Thu, 04 Oct 2012 03:26:41 -0400, Jeff Higgins
<je...@invalid.invalid> wrote:

>On 10/03/2012 09:36 PM, Arne Vajh�j wrote:
>>>> bob smith wrote:

[snip]

>To me it seems rather the bob is conducting a poll or troll.
> >>>> Am I the only one who gets incredibly confused by AffineTransform?

Oh, come off it. Taking the literal meaning when it is rather
obviously not intended is silly.

[snip]

Sincerely,

Gene Wirchenko

Gene Wirchenko

unread,
Oct 4, 2012, 12:35:47 PM10/4/12
to
On Thu, 4 Oct 2012 07:22:30 -0700 (PDT), bob smith
<b...@coolfone.comze.com> wrote:

[snip]

>It seems like when I want to "tack on" an operation, it is actually a preConcatenate.
>
>This is counterintuitive to me.
>
>Also, when you are just doing commutative operations, like just translation, it doesn't matter whether you preConcatenate or concatenate.

Matrix multiplication is NOT commutative.

[snip]

Sincerely,

Gene Wirchenko

John B. Matthews

unread,
Oct 4, 2012, 1:58:13 PM10/4/12
to
In article <fder68l1c0etqevoa...@4ax.com>,
Absolutely correct, as well as pithy. Conceptually, it may help
to think of a series of (post-) concatenated transformations as
having been applied in a last-specified-first-applied order. In
the example cited below, a small, U-shaped Polygon is centered
about the origin. The following transform rotates, scales and
translates the Polygon in the (apparent) reverse of program
order:

AffineTransform at = new AffineTransform();
at.translate(SIZE/2, SIZE/2);
at.scale(60, 60);
at.rotate(Math.PI/4);

<https://sites.google.com/site/drjohnbmatthews/point-in-polygon>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

markspace

unread,
Oct 4, 2012, 2:44:05 PM10/4/12
to
On 10/4/2012 10:58 AM, John B. Matthews wrote:

> In article <fder68l1c0etqevoa...@4ax.com>,
>>
>> Matrix multiplication is NOT commutative.
>>
>>

> Absolutely correct, as well as pithy. Conceptually, it may help
> to think of a series of (post-) concatenated transformations as
> having been applied in a last-specified-first-applied order.


Another rule of thumb is that scaling and rotating tend to occur around
the origin, not the translation point. So if you scale or rotate an
already-translated AffinceTransform, you might get unintended results.

The method I'm used to from 3D graphics is to translate any objects back
to the origin, scale and rotate, then translate them back to their
original/intended point in space. It's a literal three step process;
you can't put all that together in one matrix.

Given that AffineTransforms are 2D transforms, some results might be
different than the 3D equivalents. Like I said, some math might be
involved here.


Lew

unread,
Oct 4, 2012, 3:05:00 PM10/4/12
to
Arne Vajhøj wrote:
> Lew wrote:
>>> bob smith wrote:
>>>> Am I the only one who gets incredibly confused by AffineTransform?
>
>> No, but that isn't necessarily a reflection on the class.
>
>>>> I never know when to preConcatenate or concatenate. Then I try both
>>>> possibilities, and a lot of times neither one produces my desired
>>>> result.
>
>> http://sscce.org/
>
. . .
>> So show us what you're trying to accomplish, what you've written as an SSCCE, and
>> what precisely, with copied and pasted output, what differs from your precise
>> expectations.
>
> Hm.
>
> If the original poster has a limited number of specific
> problems, then producing a SSCCE is a very good thing.
> But I don't see any indications of that.

Then you missed what you quoted:

>>>> Then I try both possibilities, and a lot of times neither one produces my desired
>>>> result.

> To me it seems as if the original poster in general find
> it difficult to understand how to get it right (besides trial
> and error).
>
> It is not a SSCCE'able problem, because it is not about
> specific code.
>
> Instead it is about concepts and principles.

He mentioned specific code, but he didn't show it.

I asked him to show what he tried, as he claimed he'd tried it.

That's pretty damned specific.

--
Lew

Gene Wirchenko

unread,
Oct 4, 2012, 4:51:32 PM10/4/12
to
On Thu, 04 Oct 2012 13:58:13 -0400, "John B. Matthews"
<nos...@nospam.invalid> wrote:

>In article <fder68l1c0etqevoa...@4ax.com>,
> Gene Wirchenko <ge...@ocis.net> wrote:
>
>> On Thu, 4 Oct 2012 07:22:30 -0700 (PDT), bob smith
>> <b...@coolfone.comze.com> wrote:
>>
>> [snip]
>>
>> > It seems like when I want to "tack on" an operation, it is
>> > actually a preConcatenate.
>> >
>> > This is counterintuitive to me.
>> >
>> > Also, when you are just doing commutative operations,
>> > like just translation, it doesn't matter whether you
>> > preConcatenate or concatenate.
>>
>> Matrix multiplication is NOT commutative.
>>
>> [snip]
>
>Absolutely correct, as well as pithy. Conceptually, it may help

Yes to the first, because I did not say much. I did not intend
the pithy. I took one course that dealt with graphics
transformations. The material was not covered as well as it could
have been. Preferring not to misstate, I kept my statement short.

Maybe, OP is going through what I did. I think he might benefit
from some study of matrix algebra.

[snip]

Sincerely,

Gene Wirchenko

Arne Vajhøj

unread,
Oct 4, 2012, 5:38:30 PM10/4/12
to
On 10/4/2012 3:26 AM, Jeff Higgins wrote:
> To me it seems rather the bob is conducting a poll or troll.

Why?

Problems understanding that stuff is pretty common.

Arne



John B. Matthews

unread,
Oct 4, 2012, 9:23:06 PM10/4/12
to
In article <k4kldp$g1q$1...@dont-email.me>, markspace <-@.> wrote:

> On 10/4/2012 10:58 AM, John B. Matthews wrote:
>
> > In article <fder68l1c0etqevoa...@4ax.com>,
> >>
> >> Matrix multiplication is NOT commutative.
> >>
> > Absolutely correct, as well as pithy. Conceptually, it may help
> > to think of a series of (post-) concatenated transformations as
> > having been applied in a last-specified-first-applied order.
>

> Another rule of thumb is that scaling and rotating tend to occur
> around the origin, not the translation point. So if you scale or
> rotate an already-translated AffinceTransform, you might get
> unintended results.

Agree.

> The method I'm used to from 3D graphics is to translate any objects
> back to the origin, scale and rotate, then translate them back to
> their original/intended point in space. It's a literal three step
> process; you can't put all that together in one matrix.

Why not? Isn't that what a method like the anchored rotate() below does?
When it returns, the enclosing transform contains a single matrix that
does all three things when applied to a graphics context or used to
create a transformed Shape.

public void rotate(double theta, double anchorx, double anchory) {
translate(anchorx, anchory);
rotate(theta);
translate(-anchorx, -anchory);
}

> Given that AffineTransforms are 2D transforms, some results might be
> different than the 3D equivalents. Like I said, some math might be
> involved here.

markspace

unread,
Oct 4, 2012, 10:27:32 PM10/4/12
to
On 10/4/2012 6:23 PM, John B. Matthews wrote:

> In article <k4kldp$g1q$1...@dont-email.me>, markspace <-@.> wrote:
>> you can't put all that together in one matrix.


> Why not? Isn't that what a method like the anchored rotate() below does?


What I was trying to say, and did say in fact, was that you cannot
multiply out the three steps into a single matrix. Which is exactly
what I quoted above. You apparently read "matrix" as "method" and
thought I was talking about something else.

Yes, you can easily make a single method. You still need a minimum of
three matrix operations to accomplish the goal, however.


Peter Duniho

unread,
Oct 4, 2012, 10:55:34 PM10/4/12
to
Can you clarify what you mean?

Matrices can be multiplied together (or in the Java lingo, "concatenated"),
resulting in a single matrix. Yes, you need to (barring some hairy math
that's not worth the trouble) start with a separate matrix for each
transform. But they can be easily combined into a single matrix that can
then be used to perform the sequence of transformations as a single
transformation.

Your post seems to be saying that's not possible.

Pete

Eric Sosman

unread,
Oct 4, 2012, 11:33:22 PM10/4/12
to
Has somebody repealed the associative law?

--
Eric Sosman
eso...@ieee-dot-org.invalid

Jeff Higgins

unread,
Oct 5, 2012, 3:00:36 AM10/5/12
to
On 10/04/2012 05:38 PM, Arne Vajhøj wrote:
> On 10/4/2012 3:26 AM, Jeff Higgins wrote:
>> To me it seems rather the bob is conducting a poll or troll.
>
> Why?
I don't know. It is just the way it read to me.
>
> Problems understanding that stuff is pretty common.
Yep. The original post didn't seem a sincere request for
enlightenment despite the final, "Any tips?".

Gene Wirchenko

unread,
Oct 5, 2012, 12:04:13 PM10/5/12
to
On Thu, 04 Oct 2012 11:44:05 -0700, markspace <-@.> wrote:

[snip]

>The method I'm used to from 3D graphics is to translate any objects back
>to the origin, scale and rotate, then translate them back to their
>original/intended point in space. It's a literal three step process;
>you can't put all that together in one matrix.

Yes, you can. Start with an identity matrix and apply each of
the three transforms in succession. You now have one matrix which
does all three transforms in one step. Matrix multiplication is
associative so this does work.

[snip]

Sincerely,

Gene Wirchenko

markspace

unread,
Oct 5, 2012, 12:34:33 PM10/5/12
to
No, really it doesn't work this way. Note I'm not talking about
starting with the identity matrix. I'm talking about starting with an
object that's already been translated at least once. You have to add an
extra step to remove the translations back to the origin ("identity") or
successive multiplications aren't going to work the way you'd expect.

And I don't think you can "pre-multiply" that out; you'll end up doing
four, or more, operations instead of three. In intensive graphics
applications, that's a no-no.




Gene Wirchenko

unread,
Oct 5, 2012, 3:58:37 PM10/5/12
to
On Fri, 05 Oct 2012 09:34:33 -0700, markspace <-@.> wrote:

[snip]

>No, really it doesn't work this way. Note I'm not talking about
>starting with the identity matrix. I'm talking about starting with an
>object that's already been translated at least once. You have to add an

You do not have to.

>extra step to remove the translations back to the origin ("identity") or
>successive multiplications aren't going to work the way you'd expect.
>
>And I don't think you can "pre-multiply" that out; you'll end up doing
>four, or more, operations instead of three. In intensive graphics
>applications, that's a no-no.

Well, you can. Since matrix multiplication is associative, you
can do so. Example with right-side transforms:

Let M be whatever matrix you wish to transform.
Let M' be the transformed matrix.
Let T1 be the transform for translation to origin.
Let T2 be the main transform.
Let T3 be the transform for translation from origin.

M' = M X T1 X T2 X T3
is equivalent to
M' = M X (T1 X T2 X T3)
Now, let Tall = T1 X T2 X T3. Obviously,
M' = M X Tall
You can apply Tall to however many matrices as you want, but you need
calculate it only once.

Sincerely,

Gene Wirchenko

markspace

unread,
Oct 5, 2012, 8:15:20 PM10/5/12
to
On 10/5/2012 12:58 PM, Gene Wirchenko wrote:
>
> Well, you can. Since matrix multiplication is associative, you
> can do so. Example with right-side transforms:

Since the translation part has to happen first and last, I don't think
this helps. Association isn't the issue, it's commutation.

F = T1 x S x A x T2

A is constant, S (start) is your input, T1 translates back to the
origin, and T2 translates back. F is the final output. T1 and T2 are
"variable" in that they are related to S and will be different for each
pass. I think T2 = the total translation on the S object, and T1 =
1/T2, but I haven't double checked/proved that mathematically.

If you try to combine A x T2, you don't save any work, because it still
has to be done once per S rotated.

I've seen this formula a lot in many books, and if there were a way to
simplify it, I'm sure someone would have by now. Plus, math.

Let me try to give a quick example. We have a shape which looks like
the letter X, which we want to rotate 45 degrees counter clockwise about
it's own center, so we are looking at an X that is slightly heeled over.
The X is currently at position (1,2), so if we just applied a
rotation, we'd actually rotate the (1,2) about the origin we don't want
that.

^
|
|. . .X
| .
|------------->

To do this we must translate back to the origin, apply the transform,
and then translation back to (1,2). Now imagine we have several hundred
other shapes to rotate/scale the same way. I think that makes clear the
operation I'm talking about. Many shapes all with a *local* transform
which need to be applied, most not at the origin. I don't think you can
reduce that formula in the general case.

Arne Vajhøj

unread,
Oct 5, 2012, 10:02:30 PM10/5/12
to
You may be right.

But I am willing to give the guy the benefit of
doubt.

Arne

0 new messages