Problems rotating a polygon shape

1) I have a polygon shape that I want to rotate arount its own axis.
Everything works fine when the shape is located at point (0,0). But when I
place the shape somewhere else on the canvas (using SetX()/SetY()) the
centre point changes everytime I do a rotation (when I click on the shape).

I've tried using the CalculatePolygonCentre and UpdateOriginalPoints methods
(although I don't hava a clue why and when to use them). This doesn't work
either!

Am I doing something wrong or is this a bug in the Rotation method?

2) When I try to print out the polygon points using GetPoints, python
crashes!

Code is attached. Cheers.
Thomas

BTW: Tried to do this on a RectangleShape but as nothing happened, I assumed
this shape doesn't support rotation. Please tell me if I'm wrong!

begin 666 RotationTest.py
M9G)O;2!W>%!Y=&AO;BYW>"!I;7!O<G0@*@T*9G)O;2!W>%!Y=&AO;BYO9VP@
M:6UP;W)T("H-"@T*:6UP;W)T(&UA=&@-"@T*8VQA<W,@4F]T871I;VY497-T
M*'=X4VAA<&5#86YV87,I.@T*(" @(&1E9B!?7VEN:71?7RAS96QF+"!P87)E
M;G0I.@T*(" @(" @("!W>%-H87!E0V%N=F%S+E]?:6YI=%]?*'-E;&8L('!A
M<F5N="D-"@T*(" @(" @("!S96QF+F1I86=R86T@/2!W>$1I86=R86TH*0T*
M(" @(" @("!S96QF+E-E=$1I86=R86TH<V5L9BYD:6%G<F%M*0T*(" @(" @
M("!S96QF+F1I86=R86TN4V5T0V%N=F%S*'-E;&8I#0H-"B @(" @(" @<#$@
M/2!W>%!O;'EG;VY3:&%P92@I#0H@(" @(" @(' Q+D-R96%T92A;=WA296%L
M4&]I;G0H+34P+#$P*2P-"B @(" @(" @(" @(" @(" @("!W>%)E86Q0;VEN
M="@U,"P@,3 I+ T*(" @(" @(" @(" @(" @(" @('=X4F5A;%!O:6YT*#4P
M+"TQ,"DL#0H@(" @(" @(" @(" @(" @(" @=WA296%L4&]I;G0H+34P+"TQ
M,"E=*2 @( T*#0H@(" @(" @('-E;&8N9&EA9W)A;2Y!9&13:&%P92AP,2D-
M"B @(" @(" @<#$N4V5T6"@P*0T*(" @(" @("!P,2Y39719*# I#0H@(" @
M(" @(' Q+E-H;W<H5')U92D-"B @(" @(" @#0H@(" @(" @(&5V=$AA;F1L
M97(@/2!497-T179T2&%N9&QE<B@I#0H@(" @(" @(&5V=$AA;F1L97(N4V5T
M4VAA<&4H<#$I#0H@(" @(" @(&5V=$AA;F1L97(N4V5T4')E=FEO=7-(86YD
M;&5R*' Q+D=E=$5V96YT2&%N9&QE<B@I*0T*(" @(" @("!P,2Y3971%=F5N
M=$AA;F1L97(H979T2&%N9&QE<BD@(" @(" @( T*(" @(" @(" -"@T*8VQA
M<W,@5&5S=$5V=$AA;F1L97(H=WA3:&%P945V=$AA;F1L97(I.@T*(" @(&1E
M9B!?7VEN:71?7RAS96QF*3H-"B @(" @(" @=WA3:&%P945V=$AA;F1L97(N
M7U]I;FET7U\H<V5L9BD-"B @(" @(" @<V5L9BYD96=R964@/2 P#0H-"B @
M("!D968@3VY,969T0VQI8VLH<V5L9BP@>"P@>2P@:V5Y<STP+"!A='1A8VAM
M96YT(#T@,"DZ#0H@(" @(" @('-H87!E(#T@<V5L9BY'9713:&%P92@I#0H@
M(" @(" @(&-A;G9A<R ]('-H87!E+D=E=$-A;G9A<R@I#0H@(" @(" @(&1C
M(#T@=WA#;&EE;G1$0RAC86YV87,I#0H@(" @(" @(&-A;G9A<RY0<F5P87)E
M1$,H9&,I#0H-"B @(" @(" @(W!R:6YT('@L('D-"B @(" @(" @<')I;G0@
M<VAA<&4N1V5T6"@I+"!S:&%P92Y'9719*"D-"B @(" @(" @(W!R:6YT('-H
M87!E+D=E=$)O=6YD:6YG0F]X36%X*"D-"B @(" @(" @(W!R:6YT('-H87!E
M+D=E=%!O:6YT<R@I#0H@(" @(" @( T*(" @(" @("!S96QF+F1E9W)E92L]
M,C -"B @(" @(" @<VAA<&4N57!D871E3W)I9VEN86Q0;VEN=',H*0T*(" @
M(" @("!S:&%P92Y#86QC=6QA=&50;VQY9V]N0V5N=')E*"D-"B @(" @(" @
M<VAA<&4N4F]T871E*'-H87!E+D=E=%@H*2P@<VAA<&4N1V5T62@I+"!M871H
M+G)A9&EA;G,H<V5L9BYD96=R964I*0T*(" @(" @("!S:&%P92Y5<&1A=&5/
M<FEG:6YA;%!O:6YT<R@I#0H@(" @(" @('-H87!E+D-A;&-U;&%T95!O;'EG
M;VY#96YT<F4H*0T*#0H@(" @(" @(&-A;G9A<RY2969R97-H*"D-"B @(" @
M(" @#0H-"F-L87-S(%1E<W1!<' H=WA!<' I.@T*(" @(&1E9B!/;DEN:70H
M<V5L9BDZ#0H@(" @(" @(&9R86UE(#T@=WA&<F%M92A.;VYE+" M,2P@(E)O
M=&%T:6]N5&5S="(I#0H@(" @(" @('-E;&8N4V5T5&]P5VEN9&]W*&9R86UE
M*0T*(" @(" @("!C86YV87,@/2!2;W1A=&EO;E1E<W0H9G)A;64I#0H@(" @
M(" @(&9R86UE+E-H;W<H,2D-"B @(" @(" @<F5T=7)N(#$-"@T*:68@7U]N
M86UE7U\@/3T@(E]?;6%I;E]?(CH-"B @("!A<' @/2!497-T07!P*"D-"B @
2("!A<' N36%I;DQO;W H*0T*
`
end

Thomas Aanensen wrote:

1) I have a polygon shape that I want to rotate arount its own axis.
Everything works fine when the shape is located at point (0,0). But when I
place the shape somewhere else on the canvas (using SetX()/SetY()) the
centre point changes everytime I do a rotation (when I click on the shape).

I've tried using the CalculatePolygonCentre and UpdateOriginalPoints methods
(although I don't hava a clue why and when to use them). This doesn't work
either!

Am I doing something wrong or is this a bug in the Rotation method?

Could be. Do you see anything wrong here?

// Rotate about the given axis by the given amount in radians
void wxPolygonShape::Rotate(double x, double y, double theta)
{
     double actualTheta = theta-m_rotation;

     // Rotate attachment points
     double sinTheta = (double)sin(actualTheta);
     double cosTheta = (double)cos(actualTheta);
     wxNode *node = m_attachmentPoints.GetFirst();
     while (node)
     {
         wxAttachmentPoint *point = (wxAttachmentPoint *)node->GetData();
         double x1 = point->m_x;
         double y1 = point->m_y;
         point->m_x = x1*cosTheta - y1*sinTheta + x*(1.0 - cosTheta) + y*sinTheta;
         point->m_y = x1*sinTheta + y1*cosTheta + y*(1.0 - cosTheta) + x*sinTheta;
         node = node->GetNext();
     }

     node = m_points->GetFirst();
     while (node)
     {
         wxRealPoint *point = (wxRealPoint *)node->GetData();
         double x1 = point->x;
         double y1 = point->y;
         point->x = x1*cosTheta - y1*sinTheta + x*(1.0 - cosTheta) + y*sinTheta;
         point->y = x1*sinTheta + y1*cosTheta + y*(1.0 - cosTheta) + x*sinTheta;
         node = node->GetNext();
     }
     node = m_originalPoints->GetFirst();
     while (node)
     {
         wxRealPoint *point = (wxRealPoint *)node->GetData();
         double x1 = point->x;
         double y1 = point->y;
         point->x = x1*cosTheta - y1*sinTheta + x*(1.0 - cosTheta) + y*sinTheta;
         point->y = x1*sinTheta + y1*cosTheta + y*(1.0 - cosTheta) + x*sinTheta;
         node = node->GetNext();
     }

     m_rotation = theta;

     CalculatePolygonCentre();
     CalculateBoundingBox();
     ResetControlPoints();
}

2) When I try to print out the polygon points using GetPoints, python
crashes!

I'll fix this.

BTW: Tried to do this on a RectangleShape but as nothing happened, I assumed
this shape doesn't support rotation. Please tell me if I'm wrong!

Correct. Rotate is a method of wxPolygonShape.

ยทยทยท

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

> Am I doing something wrong or is this a bug in the Rotation method?
>
Could be. Do you see anything wrong here?

<code>

Instead of using the shapes centre points as input I used 0 (zero). That
solved the problem.

I don't know if I misunderstand the doc or if it's wrong.

The way I understood it was that it was possible to have a point somewhere
else on the canvas, and the polygon would rotate around it (almost like a
clock). This doesn't work. The shape moves more and more to the right while
rotating.

There might be an error in the CalculatePolygonCentre() method!?!

Anyway, I'll just make a hack to make it work the way I need it.

> BTW: Tried to do this on a RectangleShape but as nothing happened, I

assumed

> this shape doesn't support rotation. Please tell me if I'm wrong!

Correct. Rotate is a method of wxPolygonShape.

Wouldn't it be better to have the documentation for this method only under
wxPolygonShape? It's very confusing the way it is now. It shouldn't be
necessary to try the rectangle to find out you have to use polygon insted
(and then make a rectangle out of the polgon shape).

Thomas