Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Matrix Wanted

Thread view: 
Martijn Mulder - 25 May 2005 13:53 GMT
Hello,

I want to transform the unit square (0,0),(0,1),(1,1),(1,0) to some
parallelogram in 2-d space using  java.awt.geom.AffineTransform . What
translate-, scale-, shear- and/or rotate-operations do I need to combine
and in what order?

A working example based on this code would help a lot :-)

//class Parallelogram
public class Parallelogram extends java.awt.Component
{

//data members point1, point2, point3, point4
java.awt.Point point1;
java.awt.Point point2;
java.awt.Point point3;
java.awt.Point point4;

//constructor
Parallelogram(int a,int b,int c,int d,int e,int f)
{
 point1=new java.awt.Point(a,b);
 point2=new java.awt.Point(c,d);
 point3=new java.awt.Point(e,f);
 point4=new java.awt.Point(e-c+a,f-d+b);
}

//paint
public void paint(java.awt.Graphics a)
{

 //get a 3x3 identity matrix
 java.awt.geom.AffineTransform at=new java.awt.geom.AffineTransform();

 //these operations on AffineTransform I need help with
 at.translate(point1.x,point1.y);
 at.scale(point3.x-point1.x,point3.y-point1.y);
 at.shear(Math.atan2(point4.y-point2.y,point4.x-point2.x),0);
 at.rotate(Math.atan2(point3.y-point1.y,point3.x-point1.x));

 //p1..p4 represent the unit square
 java.awt.Point p1=new java.awt.Point(0,0);
 java.awt.Point p2=new java.awt.Point(0,1);
 java.awt.Point p3=new java.awt.Point(1,1);
 java.awt.Point p4=new java.awt.Point(1,0);

 //transform the unit square using AffineTransform at
 at.transform(p1,p1);
 at.transform(p2,p2);
 at.transform(p3,p3);
 at.transform(p4,p4);

 //paint the original parallelogram
 a.setColor(java.awt.Color.red);
 a.drawLine(point1.x,point1.y,point2.x,point2.y);
 a.setColor(java.awt.Color.green);
 a.drawLine(point2.x,point2.y,point3.x,point3.y);
 a.setColor(java.awt.Color.black);
 a.drawLine(point3.x,point3.y,point4.x,point4.y);
 a.setColor(java.awt.Color.blue);
 a.drawLine(point4.x,point4.y,point1.x,point1.y);

 //paint the transformed unit square. Both parallelograms should be equal
 a.setColor(java.awt.Color.red);
 a.drawLine(p1.x,p1.y,p2.x,p2.y);
 a.setColor(java.awt.Color.green);
 a.drawLine(p2.x,p2.y,p3.x,p3.y);
 a.setColor(java.awt.Color.black);
 a.drawLine(p3.x,p3.y,p4.x,p4.y);
 a.setColor(java.awt.Color.blue);
 a.drawLine(p4.x,p4.y,p1.x,p1.y);
}

//main
static void main(String[]a)
{
 java.awt.Frame frame=new java.awt.Frame("Parallelogram");
 frame.add(new Parallelogram(13,21,55,34,144,89));
 frame.setSize(300,300);
 frame.show();
}
}
Bob - 25 May 2005 14:03 GMT
> Hello,
>
[quoted text clipped - 4 lines]
>
> A working example based on this code would help a lot :-)

This is definitely homework. Have you been sleeping in lectures?
Signature

Bob

Martijn Mulder - 25 May 2005 14:05 GMT
This is definitely homework. Have you been sleeping in lectures?

No, this is not homework. I did some sleeping during lectures, but that was a
long time ago in a completely different field.
Alex - 25 May 2005 14:21 GMT
http://www.kasamba.com
Martijn Mulder - 25 May 2005 15:24 GMT
> http://www.kasamba.com

That's a paid service, thank you. In fact, I am looking for a matrix that
transforms one parallelogram into another, not necessarily the unit square. But
the price might get to high...
Andrew Thompson - 25 May 2005 15:39 GMT
>> http://www.kasamba.com
>
> That's a paid service, thank you.

Yes.  It is perfect for people who want solutions without effort.

>..In fact, I am looking for a matrix that
> transforms one parallelogram into another, not necessarily the unit square. But
> the price might get to high...

So either ..
a) pay more, or
b) do (a lot) more yourself ..because without more effort on your
part, you are hardly likely to get a serious response here.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Martijn Mulder - 25 May 2005 15:42 GMT
"Andrew Thompson"  wrote in message

> So either ..
> a) pay more, or
> b) do (a lot) more yourself ..because without more effort on your
> part, you are hardly likely to get a serious response here.

There is a complete, commented, compilable class included that cannot be
mistaken for homework. I could not make it simpler. I tried numerous hours
figuring it out myself but turned to the newsgroups in want of someone who has
been through it before. Thanks for your time.
Andrew Thompson - 25 May 2005 16:34 GMT
> "Andrew Thompson"  wrote in message
>
[quoted text clipped - 5 lines]
> There is a complete, commented, compilable class included that cannot be
> mistaken for homework.

It is a complete, commented, compilable class that might have been
obtained off the net, or from a colleague, or as part of an assignment
for college, or out of a book, or that you wrote yourself.  

(shrugs)  It is not important.  I personally do not care if it is
homework, and would be no less (nor more) likely to help you if it were.

>..I could not make it simpler. I tried numerous hours
> figuring it out myself

You stated earlier..
"What
translate-, scale-, shear- and/or rotate-operations do I need to combine
and in what order?"

Without any clear indication of..
- what you have tried, or
- why it did not work, or
- your theories as to why it did not work,
..it gives the impression that you wish to be spoonfed your solutions.

>...but turned to the newsgroups in want of someone who has
> been through it before. Thanks for your time.

Your welcome.

I will leave you with this thought.

Which do you think is going to be more likely to produce a solution
to your problem?
1) Replying to the (stated or implied) criticisms of the people who reply,
in such a way as to bringing them around to helping you.
2) Becoming defensive and 'denying' their claims.

If you wish to progress this thread, I suggest you fill in some
of the blanks mentioned above.  That might get it back on track
for you..

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Chris Uppal - 25 May 2005 17:13 GMT
> a) pay more, or
> b) do (a lot) more yourself ..because without more effort on your
> part, you are hardly likely to get a serious response here.

Have you ever /tried/ working with affine transforms yourself ?  I find them
completely fustrating/baffling (for all I understand the /concepts/), and if I
needed help with setting one up to a particular specification, then I could do
one of three things:

1) I could try stuff more or less at random till it worked.
2) I could pay someone to do it for me.
3) I could ask here or in some other forum where there may be people who could
answer.

I'd probably use approach (1) myself, but if I went for (3) then certainly have
the test harness available in which I originally tried and failed at (1), and
so why not help anyone who wants to help me by cleaning up its code and adding
it to the post ?  I would consider that a courtesy.

(Incidentally, I have had a go at solving this particular problem, but gave
up... :-(

   -- chris

P.S.  Nice to see (or read) you're back again, Andrew.
Jacob - 25 May 2005 20:33 GMT
> P.S.  Nice to see (or read) you're back again, Andrew.

Really?

For a while I actually found this group useful and
friendly. With this individual back in action it
has again become noisy and hostile, and I for sure
go elsewhere with my issues and wisdom.
Andrew Thompson - 26 May 2005 04:15 GMT
>> P.S.  Nice to see (or read) you're back again, Andrew.
>
[quoted text clipped - 3 lines]
> friendly. With this individual back in action it
> has again become noisy and hostile,

Really?  

Odd that this "noisy and hostile" poster has elicited words
of thanks from no less than 4 posters across c.l.j.p/g/h
over the last couple of days.

Perhaps it is more that this poster "don't take no crap", and
apparently neither do a lot of the other posters.

>..and I for sure go elsewhere with my issues and wisdom.

Feel free to keep posting and questioning/advising/criticising,
but otherwise.  Bye!

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

yogo - 02 Jun 2005 20:33 GMT
>> P.S.  Nice to see (or read) you're back again, Andrew.
>
[quoted text clipped - 4 lines]
> has again become noisy and hostile, and I for sure
> go elsewhere with my issues and wisdom.

I totally agree with you. Too bad this guy is back...
Scott Ellsworth - 02 Jun 2005 21:47 GMT
> >> P.S.  Nice to see (or read) you're back again, Andrew.
> >
[quoted text clipped - 6 lines]
>
> I totally agree with you. Too bad this guy is back...

For a group to stay useful enough for skilled programmers to keep coming
back, it has to police itself.  Andrew provides a lot of useful
information, as well as encouraging people to think, read the FAQs, and
to use the help newsgroup for newbie questions.  Bug him enough, and he
may leave, but then, so will the help and resources he provides.

Welcome back, Andrew.

Scott
Andrew Thompson - 03 Jun 2005 02:26 GMT
>>>> P.S.  Nice to see (or read) you're back again, Andrew.
>>>
[quoted text clipped - 9 lines]
> For a group to stay useful enough for skilled programmers to keep coming
> back, it has to police itself.  

Thanks Scott!  When these 'discussions' crop up and I feel the
need to justify/explain my actions, that is one reason always
in the back of my mind, but I usually do not mention it.

I get the distinct impression that there are a variety of people who
appreciate the efforts (by a number of posters) to keep the group lean,
but do but have the time (or motivation) for discussing such matters.

Many of the posters who are most proficient Java fit this category.

> ...Bug him enough, and he may leave,

LOL.  They wish!    ;-)    But...

>..but then, so will the help and resources he provides.

It is more the other *quieter* folks who risk being lost to the group,
the people who would not have the time to trawl through a group
inundated with screaming noobs.  I'll still be here, haranguing 10/10,
in the hope of finding the 1 in 10 who can develop into a usenaut.

> Welcome back, Andrew.

Good to be back.     :-)

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Angus - 03 Jun 2005 07:02 GMT
> > >> P.S.  Nice to see (or read) you're back again, Andrew.
> > >
[quoted text clipped - 9 lines]
> Andrew provides a lot of useful
> information, as well as encouraging people to think and read FAQs

The irritating part is not what Andrew says, it's the way he
says it. Even a total idiot doesn't like someone screaming
that fact to his face.

Andrew, perhaps you should consider moderating the sarcastic,
condescending style of your postings to the newsgroups.

Good Luck,
Avi.
Andrew Thompson - 03 Jun 2005 08:12 GMT
> Andrew, perhaps you should consider moderating the sarcastic,
> condescending style of your postings to the newsgroups.

I consider the tone I use in each post carefully.
Perhaps if you have a problem with the tone I use in any post,
you should take it up at the time, and be specific.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Angus - 03 Jun 2005 21:43 GMT
> > Andrew, perhaps you should consider moderating the sarcastic,
> > condescending style of your postings to the newsgroups.
>
> I consider the tone I use in each post carefully.
> Perhaps if you have a problem with the tone I use in any post,
> you should take it up at the time, and be specific.

I knew you would be under the impression that there is
nothing wrong with your posts. I was hoping that you would
prove me wrong. Obviously you haven't reached enlightenment
yet. I realise these words will have absolutely no effect
on you, but I'm glad that I expressed them, anyway.

Good Luck,
Avi.
Scott Ellsworth - 04 Jun 2005 01:22 GMT
> > > Andrew, perhaps you should consider moderating the sarcastic,
> > > condescending style of your postings to the newsgroups.
[quoted text clipped - 8 lines]
> yet. I realise these words will have absolutely no effect
> on you, but I'm glad that I expressed them, anyway.

He did ask for specifics.  This reply seems fairly measured, for example.

You are under no obligation to supply specifics, of course.  I merely
point out that the above reply, at least, does not seem at all acerbic
to an outside party.

Scott
Andrew Thompson - 04 Jun 2005 05:00 GMT
>>> Andrew, perhaps you should consider moderating the sarcastic,
>>> condescending style of your postings to the newsgroups.
[quoted text clipped - 3 lines]
> I knew you would be under the impression that there is
> nothing wrong with your posts.

Where the heck did you get that idea?  Were we reading the same paragraph?

Just because I consider the tone of each post carefully,
does not mean that I never make mistakes.  There have been
occasions when I have publicly apologised for my words.

Which leads me back to..

>> Perhaps if you have a problem with the tone I use in any post,
>> you should take it up at the time, and be specific.

>..I was hoping that you would prove me wrong.

I'm hoping you'll find some evidence, or find peace.

>..Obviously you haven't reached enlightenment yet.

<slightly sarcastic>
Light the incense and pack me a bong.  I'm almost there.
</slightly sarcastic>

>..I realise these words will have absolutely no effect
> on you,

<dripping with..>
Don't worry, your words are (mildly) irritating.
</dripping with..>

>..but I'm glad that I expressed them, anyway.

Please feel free to express them at the time you have a
grievance.  As mentioned (twice, now).

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Matt Humphrey - 04 Jun 2005 17:02 GMT
> >>> Andrew, perhaps you should consider moderating the sarcastic,
> >>> condescending style of your postings to the newsgroups.
[quoted text clipped - 36 lines]
> Please feel free to express them at the time you have a
> grievance.  As mentioned (twice, now).

Andrew, you are a prolific responder who gives quick, helpful answers.
However, I have also noticed that a few replies are remarkably insulting and
unhelpful.  Since you asked for specifics, I refer you to your first post in
this thread.

Cheers,
Matt Humphrey  matth@ivizNOSPAM.com  http://www.iviz.com/
Andrew Thompson - 05 Jun 2005 01:48 GMT
>> Please feel free to express them at the time you have a
>> grievance.  ....
[quoted text clipped - 3 lines]
> unhelpful.  Since you asked for specifics, I refer you to your first post in
> this thread.

This one?
<http://groups.google.com.au/group/comp.lang.java.programmer/msg/d2809cb02324638a
?dmode=source&hl=en
>

[ My only defense of that one is linking to it.  I feel it
requires no further justification, and if a poster arrives at
the group with a similarly phrased problem, (with two other
posters already hinting that the OP was not about to be taken
seriously in this group - without the OP realising they may
have a point) they can expect similar treatment. ]

You must be kidding Matt!  I am a little disappointed that you
showed so little effort in hunting down my more questionable
posts.  Surely you can do better than that.

But.. this thread is now (and I feel has for a considerable while)
not been about this thread, or this poster.
[ FWIW Martin, my apologies for not trying to drag it off into
a separate thread earlier. ]

Might I suggest that anyone who has a further interest in pursuing
the level of niceness that should be displayed on this group, take
it up on a new and separate thread?

In fact.. see 'Courtesy and manners on c.l.j.programmer'.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Andrew Thompson - 05 Jun 2005 02:08 GMT
> [ FWIW Martin, my apologies for not trying to drag it off into
> a separate thread earlier. ]

(slaps forehead) ..and double, special apologies for stuffing up the
spelling of your name, Martijn.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Martijn Mulder - 05 Jun 2005 03:23 GMT
"Andrew Thompson"
> (slaps forehead) ..and double, special apologies for stuffing up the
> spelling of your name, Martijn.

Now that you come to mention it, it was kind:

<Andrew Thompson>
a) pay more, or
b) do (a lot) more yourself ..because without more effort on your
part, you are hardly likely to get a serious response here.
</Andrew Thompson>

it was helpful:

<Andrew Thompson>
> That's a paid service, thank you.
Yes.  It is perfect for people who want solutions without effort.
</Andrew Thompson>

it showed a deep psychological understanding:

<Andrew Thompson>
I will leave you with this thought.

Which do you think is going to be more likely to produce a solution
to your problem?
1) Replying to the (stated or implied) criticisms of the people who reply,
in such a way as to bringing them around to helping you.
2) Becoming defensive and 'denying' their claims.
</Andrew Thompson>

but best of all, it was an open attempt to further communication!

<Andrew Thompson>
If you wish to progress this thread, I suggest you fill in some
of the blanks mentioned above.  That might get it back on track
for you..
</Andrew Thompson>

Thank you Andrew, thank you so much!
Thomas Weidenfeller - 03 Jun 2005 08:45 GMT
> For a group to stay useful enough for skilled programmers to keep coming
> back, it has to police itself.

Not only this. There is no one else who can and will do it.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Martijn Mulder - 03 Jun 2005 09:16 GMT
Dear Thomas,

What, do you think, is easier to ridicule? Is it

http://redwing.hutman.net/~mreed/warriorshtm/xenophobe.htm

or is it

http://redwing.hutman.net/~mreed/warriorshtm/bigdogmetoo.htm
Thomas Weidenfeller - 03 Jun 2005 09:03 GMT
> For a while I actually found this group useful and
> friendly. With this individual back in action it
> has again become noisy and hostile, and I for sure
> go elsewhere with my issues and wisdom.

Please do so. I remember you flaiming me for an answer and I am just
surprised you you managed to get out of my killfile. I'll fix that in a
second.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Jacob - 03 Jun 2005 13:50 GMT
> Please do so. I remember you flaiming me for an answer and I am just
> surprised you you managed to get out of my killfile. I'll fix that in a
> second.

I don't usually flame anyone, so either you mistake me
for someone else, or there has been a misunderstanding.
And I if actually did, I must have had a really bad day,
so please accept my appology.

Signature

Jacob Dreyer
G - Free 2D Graphics Library for Java  - http://geosoft.no/graphics
Free Java Software - http://geosoft.no/software

Andrew Thompson - 26 May 2005 03:43 GMT
> P.S.  Nice to see (or read) you're back again, Andrew.

Good to be back!

A.
John C. Bollinger - 25 May 2005 23:16 GMT
> I want to transform the unit square (0,0),(0,1),(1,1),(1,0) to some
> parallelogram in 2-d space using  java.awt.geom.AffineTransform . What
> translate-, scale-, shear- and/or rotate-operations do I need to combine
> and in what order?

If you break up an AffineTransform into a series of elementary
transformations, as you desire to do, a key thing to watch is what each
elementary transformation does to unit vectors aligned along the two
axes of the original 2D Euclidean space.  The next thing you should keep
in mind is that the order in which you concatenate the elementary
transformations is the opposite of the logical order in which they are
applied -- you can determine this from the matrix-based definitions of
the class and its concatenate(AffineTransform) method.

Then look at the invariants of each type of elementary transform:
() translations preserve vector lengths and directions and inter-vector
angles
() all elementary transforms except translations leave the origin fixed
() scaling preserves vector directions and inter-vector angles, but not
vector lengths
() rotation preserves vector lengths and inter-vector angles, but not
vector directions
() shearing preserves vector lengths and directions for vectors
(anti-)parallel to a particular characteristic direction

Since affine transformations preserve the parallelism of lines, the
easiest thing to do in your particular case is find the the scale/shear
that produces the correct shape, the rotation that orients it correctly,
and the translation that moves it into place, *in that logical order*.

There is no single correct sequence, but the sequence you choose affects
which matrices you need to use.

> //class Parallelogram
> public class Parallelogram extends java.awt.Component
[quoted text clipped - 27 lines]
>   at.shear(Math.atan2(point4.y-point2.y,point4.x-point2.x),0);
>   at.rotate(Math.atan2(point3.y-point1.y,point3.x-point1.x));

You have not fully defined the transformation you want inasmuch as which
parallelogram vertices are to correspond to which vertices of the
square, but I'll suppose (0,0) -> point1, (0,1) -> point2, (1,1) ->
point3, (1,0) -> point4.

If we plan on shearing such that the preserved direction is along the x
axis, then in the scaling step we want to scale by distance(point1 ->
point4) in x, and by parallelogram height in y.  It is convenient to
find vectors describing the directions and lengths of the parallelogram
edges:

vx = (point4.x - point1.x, point4.y - point1.y) = (vxx, vxy)
vy = (point2.x - point1.x, point2.y - point1.y) = (vyx, vyy)

Their lengths are

lx = sqrt(vxx * vxx + vxy * vxy), ly = sqrt(vyx * vyx + vyy * vyy)

The distance from point1 to point 4 is simply lx, but the height is more
complicated.  As an intermediate step, it is useful to compute the
length of the projection of vy onto vx, which is related to the dot
product of the above vectors:

l = length(vxx * vyx, vxy * vyy) / lx

The parallelogram height is then:

h = sqrt(ly * ly - l * l)

Suppose that we (conceptually) scale first, then shear.  The scaling
will transform vector (0, 1) to (0,h), and the shear must further
transform this to (l, h) while leaving the (1, 0) direction invariant.
The corresponding shear parameters are just (l / h, 0).

Now the rotation.  The scaling and shearing did not alter the directions
of vectors originally along the x axis, so the angle between vx and the
x axis is the desired rotation angle.  The coordinates of vx are closely
related to the angle's cosine and sign, respectively:

cos(theta) = vxx / lx , sin(theta) = vxy / lx

Therefore,

theta = Math.atan2(vxy / lx, vxx / lx)

The translation is easy; all of the preceding transforms have left the
origin fixed, so the translation is the same as the coordinates of
point1.  To put it all together, then, remembering that the
concatenation order is the opposite of the conceptual order of
application of the operations and using the definitions above, we get:

    AffineTransform at = new AffineTransform();

    at.translate(point1.x, point1.y);
    at.rotate(theta);
    at.shear(l / h, 0);
    at.scale(l, h);

My, but wasn't that painful.  As an alternative, you might consider
skipping everything after computing the components of vx and vy, and
doing this instead:

    AffineTransform at = new AffineTransform(
            vxx, vxy, vyx, vyy, point1.x, point1.y);

That's a general, unique solution for the case where you know how the
Euclidean basis vectors and origin transform.

Signature

John Bollinger
jobollin@indiana.edu

Martijn Mulder - 26 May 2005 09:38 GMT
<snip>

Thank you for your elaborate answer to my question. I am working on the
implementation of both solutions you gave in Java and I intend to present it to
the group when finished, but there is one point that I do not understand:

> (vxx, vyx, lx, ly declared and defined)
> The distance from point1 to point 4 is simply lx, but the height is more
[quoted text clipped - 3 lines]
>
> l = length(vxx * vyx, vxy * vyy) / lx

How do I implement the length(int,int)-function? Is it a function to start with?

-Martijn
jonck - 26 May 2005 10:37 GMT
I assume that he did not specify the function since it is very trivial:

public int length(int x1, int x2) {
    return x2 - x1;
}
John C. Bollinger - 26 May 2005 15:44 GMT
> <snip>
>
[quoted text clipped - 11 lines]
>
> How do I implement the length(int,int)-function? Is it a function to start with?

Sorry, that was intended to represent the standard Cartesian vector
length formula, length(x, y) = sqrt(x*x + y*y), but the assertion is in
error in the first place.  It should have simply been

l = (vxx * vyx + vxy * vyy) / lx

The dot product part is the term inside the parentheses; some people
prefer to call this a scalar product.

I have looked over the rest of the math, and find no other errors.  In
particular, I worked out the algebra to show that the matrix produced by
the combination of elemental transforms (with the above correction) is
the same as the one produced directly via the shortcut.

Signature

John Bollinger
jobollin@indiana.edu

Martijn Mulder - 27 May 2005 09:53 GMT
I am looking for a way to transform the unit square to a given
parallelogram using java.awt.geom.AffineTransform. Two methods
where suggested to me by John Bollinger in this group. One of
them is very succinct, but the other, more complex one, I do not
get right. Here is a class Parallelogram in which I implement
both the succesfull and the failing method. The comments in the code
elaborate on the attemps. Can someone review the code and
hopefully fix it? Thanks in advance.

-Martijn

//class Parallelogram
//Find the matrix that transforms the unit square (0,0), (0,1),
//(1,1), (1,0) to a predefined parallelogram. Two methods are
//given, getAffineTransform_1() and getAffineTransform_2(). As of
//this attempt, only the first method yields the desired result.
public class Parallelogram extends java.awt.Component
{

//data members point1..point4, vertices of parallelogram
java.awt.Point point1;
java.awt.Point point2;
java.awt.Point point3;
java.awt.Point point4;

//data members u1..u4, vertices of the unit square
java.awt.Point u1=new java.awt.Point(0,0);
java.awt.Point u2=new java.awt.Point(0,1);
java.awt.Point u3=new java.awt.Point(1,1);
java.awt.Point u4=new java.awt.Point(1,0);

//constructor
Parallelogram(int a,int b,int c,int d,int e,int f)
{
 point1=new java.awt.Point(a,b);
 point2=new java.awt.Point(c,d);
 point3=new java.awt.Point(e,f);
 point4=new java.awt.Point(e-c+a,f-d+b);
}

//getAffineTransform_1
//Very cool and very sure way to find the matrix that
//transforms the unit square to a given parallelogram.
//Works like a breeze. See function getAffineTransform_2
//for a hairier beast.
java.awt.geom.AffineTransform getAffineTransform_1()
{
 return new java.awt.geom.AffineTransform
 (
  point3.x-point2.x,
  point3.y-point2.y,
  point2.x-point1.x,
  point2.y-point1.y,
  point1.x,
  point1.y
 );
}

//getAffineTransform_2
//Cookbook approach to find the matrix that transforms
//the unit square to a given parellelogram, based on the
//work of John Bollinger. See his post earlier in this
//thread for an explanation.
//
//PROBLEM: IT DOES NOT WORK! WHERE DO I GO WRONG?
//
//My mathematical skills don't go anywhere near this, so
//I relied on 'cut-and-paste' (sort of) to write this
//method. There must be a typo somewhere, but I cannot
//find it. Please help.
java.awt.geom.AffineTransform getAffineTransform_2()
{
 int vxx = point4.x - point1.x;
 int vxy = point4.y - point1.y;
 int vyx = point2.x - point1.x;
 int vyy = point2.y - point1.y;
 double lx = Math.sqrt(vxx * vxx + vxy * vxy);
 double ly = Math.sqrt(vyx * vyx + vyy * vyy);
 double l = (vxx * vyx + vxy * vyy) / lx;
 double h = Math.sqrt(ly * ly - l * l);
 double theta = Math.atan2(vxy / lx, vxx / lx);
 java.awt.geom.AffineTransform at=new java.awt.geom.AffineTransform();
 at.translate(point1.x, point1.y);
 at.rotate(theta);
 at.shear(l / h, 0);
 at.scale(l, h);
 return at;
}

//paint with AffineTransform
public void paint
(
 java.awt.Graphics a,
 java.awt.geom.AffineTransform b
)
{

 //data members p1..p4, will hold the transformed unit square
 java.awt.Point p1=new java.awt.Point();
 java.awt.Point p2=new java.awt.Point();
 java.awt.Point p3=new java.awt.Point();
 java.awt.Point p4=new java.awt.Point();

 //transform the unit square using AffineTransform
 b.transform(u1,p1);
 b.transform(u2,p2);
 b.transform(u3,p3);
 b.transform(u4,p4);

 //paint the transformed unit square
 a.setColor(java.awt.Color.red);
 a.drawLine(p1.x,p1.y,p2.x,p2.y);
 a.setColor(java.awt.Color.black);
 a.drawLine(p2.x,p2.y,p3.x,p3.y);
 a.setColor(java.awt.Color.green);
 a.drawLine(p3.x,p3.y,p4.x,p4.y);
 a.setColor(java.awt.Color.blue);
 a.drawLine(p4.x,p4.y,p1.x,p1.y);
}

//paint
public void paint(java.awt.Graphics a)
{

 //paint the original parallelogram
 a.setColor(java.awt.Color.red);
 a.drawLine(point1.x,point1.y,point2.x,point2.y);
 a.setColor(java.awt.Color.black);
 a.drawLine(point2.x,point2.y,point3.x,point3.y);
 a.setColor(java.awt.Color.green);
 a.drawLine(point3.x,point3.y,point4.x,point4.y);
 a.setColor(java.awt.Color.blue);
 a.drawLine(point4.x,point4.y,point1.x,point1.y);

 //paint with getAffineTransform_1(). This one works!
 paint(a,getAffineTransform_1());

 //paint with getAffineTransform_2(). Close....
 paint(a,getAffineTransform_2());
}

//main
static void main(String[]a)
{
 java.awt.Frame frame=new java.awt.Frame("Parallelogram");
 frame.add(new Parallelogram(13,21,55,34,144,89));
 frame.setSize(300,300);
 frame.show();
}
}
John C. Bollinger - 27 May 2005 15:47 GMT
>  //getAffineTransform_2
>  //Cookbook approach to find the matrix that transforms
[quoted text clipped - 8 lines]
>  //method. There must be a typo somewhere, but I cannot
>  //find it. Please help.

Or I made a mistake in drawing up the recipe, or the AffineTransform
class is buggy or has misleading documentation.  I rechecked my math
yesterday, and I don't see an error in your translation into Java code,
 but I could be overlooking something.  On the other hand,
AffineTransform is coded in a fairly obtuse manner, apparently in an
effort to make it perform as well as possible.  There is a great deal of
conditional logic and use and manipulation of state flags, all of which
could hide errors, and there is some degree of exact floating-point
comparison involved that could conceivably produce numerical
instability.  This is not to say that the problem is necessarily with
that class, only that the possibility should not be ruled out.

If the other technique works, then do you really need to worry about the
complicated version?

Signature

John Bollinger
jobollin@indiana.edu

Martijn Mulder - 27 May 2005 16:11 GMT
"John C. Bollinger"
<snip>
> If the other technique works, then do you really need to worry about the
> complicated version?

You are right. I was not aware it could be THAT simple. Venturing into the
complicated version was a mere academic endeveour. I think I can do the
things needed with the simpler version, so I stick with that. Simplicity is
a marker for quality. Thanks for your insights :-)
Glenn Reynolds - 01 Jun 2005 11:41 GMT
Martijn

There is a discussion of the AffineTransform in the Java Tutorial. The 3x3
matrix is explained there. I would probably create GeneralPath objects
instead of drawing lines. Then you can apply one transform to the unit
square as a whole.

It's hard to see what you are trying to do. I think you want a transformed
unit square to match a given parallelogram. If you can cast it a series of
equations (one for each vertex), then you should get more equations than
unknowns. Get a matrix package from COLT and do a generalised inversion.

I wouldn't extend Component for the parallelogram. I would extend Shape or
GeneralPath and just paint it after transformation. The transformation can
be determined independently of Graphics2D using a matrix package. Put the
result into the AffineTransform of the drawing surface and repaint the
parallelogram or unit square.

If you are trying to geo-register or orthorectify an image to coordinates
based on mouse clicks, then that approach will work, as I just wrote a
module myself to do the same thing.

Regards

Glenn

> Hello,
>
[quoted text clipped - 79 lines]
>  }
> }
Martijn Mulder - 01 Jun 2005 12:28 GMT
"Glenn Reynolds"
<snip>

> If you are trying to geo-register or orthorectify an image to coordinates
> based on mouse clicks, then that approach will work, as I just wrote a
> module myself to do the same thing.

Thank you for your reply, Glenn. This group has helped me
finding a way to perform the desired transform. How I will
implement it later I don't know yet, but for the sake of illustration
I extend JComponent.

The actual goal is to have TRIANGLES modified and mapped
on each other. In pseudo-code I do it like this:

1. Given are 2 triangles with known points. Triangle 1 is the source,
Traingle 2 the destination.

2. find the matrix that converts the 'unit-triangle' (0,0),(0,1),(1,1) to
the source-triangle.

3. Take the inverse of this matrix

4. concatenate it with the matrix that converts the 'unit-triangle' to
the destination-triangle.

In java-code it is even messier, but it works. Hack if you like (and
suggest a better way of doing it if you know one :-). 'Driehoek' is the
dutch word for Triangle.

/*
Driehoek.java
Driehoek, convert 1 'Triangle' to another.
*/

//class Driehoek
public class Driehoek extends javax.swing.JComponent
{

//data members tri_1, tri_2
Tri tri_1=new Tri(21,34,55,89,144,55);
Tri tri_2=new Tri(402,347,548,137,753,276);

//paint
public void paint(java.awt.Graphics a)
{
 tri_1.paint(a);
 tri_2.paint(a);
 tri_1.paint(a,tri_1.getAffineTransform(tri_2));
 tri_2.paint(a,tri_2.getAffineTransform(tri_1));
}

static void main(String[]a)
{
 javax.swing.JFrame frame=new javax.swing.JFrame("Driehoek");
 frame.getContentPane().add(new Driehoek());
 frame.setSize(1024,780);
 frame.show();
 System.out.println("Driehoek");
}

}

//class Tri
class Tri
{

//data members point1, point2, point3
java.awt.Point point1;
java.awt.Point point2;
java.awt.Point point3;

//constructor
Tri(int a,int b,int c,int d,int e,int f)
{
 point1=new java.awt.Point(a,b);
 point2=new java.awt.Point(c,d);
 point3=new java.awt.Point(e,f);
}

//getAffineTransform
java.awt.geom.AffineTransform getAffineTransform()
{
 return new java.awt.geom.AffineTransform
 (
  point2.x-point1.x,
  point2.y-point1.y,
  point3.x-point2.x,
  point3.y-point2.y,
  point1.x,
  point1.y
 );
}

//getAffineTransform
java.awt.geom.AffineTransform getAffineTransform(Tri a)
{
 java.awt.geom.AffineTransform b=a.getAffineTransform();
 b.concatenate(getInverse(getAffineTransform()));
 return b;
}

//getInverse
java.awt.geom.AffineTransform getInverse(java.awt.geom.AffineTransform a)
{
 java.awt.geom.AffineTransform b=new java.awt.geom.AffineTransform();
 try{b=a.createInverse();}
 catch(java.awt.geom.NoninvertibleTransformException exception)
 {
  System.out.println("NoninvertibleTransformException thrown");
  System.out.println(exception);
 }
 return b;
}

//paint
public void paint(java.awt.Graphics a)
{
 java.awt.Graphics2D b=(java.awt.Graphics2D)a;
 b.setColor(java.awt.Color.blue);
 b.drawLine(point1.x,point1.y,point2.x,point2.y);
 b.drawLine(point2.x,point2.y,point3.x,point3.y);
 b.drawLine(point3.x,point3.y,point1.x,point1.y);
}

//paint
public void paint(java.awt.Graphics a,java.awt.geom.AffineTransform b)
{
 java.awt.Graphics2D c=(java.awt.Graphics2D)a;
 java.awt.geom.Point2D.Double d=new java.awt.geom.Point2D.Double();
 java.awt.geom.Point2D.Double e=new java.awt.geom.Point2D.Double();
 java.awt.geom.Point2D.Double f=new java.awt.geom.Point2D.Double();
 b.transform(point1,d);
 b.transform(point2,e);
 b.transform(point3,f);
 c.setColor(java.awt.Color.red);
 c.drawLine((int)d.x,(int)d.y,(int)e.x,(int)e.y);
 c.setColor(java.awt.Color.green);
 c.drawLine((int)e.x,(int)e.y,(int)f.x,(int)f.y);
 c.setColor(java.awt.Color.black);
 c.drawLine((int)f.x,(int)f.y,(int)d.x,(int)d.y);
}

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.