Author Topic: software rotation question.  (Read 3155 times)

0 Members and 1 Guest are viewing this topic.

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
software rotation question.
« on: July 16, 2007 »

Hi all

Suppos I have a rectangle which has coordinates P1,P2,P3,P4. I want to rotate it by angle a. how to calculate the new coordinate of the rectangle ?

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: software rotation question.
« Reply #1 on: July 16, 2007 »
If you have a rectangle, that's 4 corners and you will know 2 or 3 bits of information about those corners at it's most basic;

P1 : X,Y
P2 : X,Y
P3 : X,Y
P4 : X,Y

Or;

P1 : X,Y,Z
P2 : X,Y,Z
P3 : X,Y,Z
P4 : X,Y,Z

These points are usually described as points on a grid that can go from  negative to positive.

It will help you to understand how this works if you know the formula to generate a circle;

X = RADIUS * SIN ( THETA )
Y = RADIUS * COS ( THETA )

Essentially that's more or less what we will do to your points :)

Rotation is applied to the points and they are transformed, a screen offset can be applied, the points can be scaled too and if you rotate areound all three axis the X and Y points are divided by thier respective Z co-ordinates to give perspective.

Here's some code to rotate points around thier centre which would work for any flat object including your rectangle.

It assumes that your points are stored in 2 dimensional arrays (XP,YP)
Theta is the angle that you want to rotate them by.

This code is quite quick as the sin / cos remains constant in the loop so is calculated once beforehand, this can make a large difference if you decide to have a few thousand points in the future ;)

Code: [Select]
    MO1= COS( THETA ):' GENERATE MATRIX CONSTANT 1
    MO2= SIN ( THETA ):' GENERATE MATRIX CONSTANT 2
   
FOR X=1 TO POINTS
FOR Y=1 TO POINTS
       
        '-----------------------------------------------------------------------
        ' ROTATE THE GRID
        '-----------------------------------------------------------------------
       
        NNN= XP (X,Y)
        MMM=YP(X,Y)
       
        XP(X,Y) = MO1 * NNN - MO2 * MMM
        YP(X,Y) = MO1 * MMM + MO2 * NNN
       

NEXT   
NEXT

Hope that helps :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: software rotation question.
« Reply #2 on: July 16, 2007 »
Thanks shocky very much for your explaination.  :)  , it is really helpful , but i am still stuck.

i will show you my code , hope you can help me.

1- have a Box structure which holds a 4 points represinting the rectangle and halfWidth , halfHeight.
2- given a center coordinate of the rectangle (CX ,CY) and the rotation angle THETA.

here is my function that calculte the final 4 point of the rectabgle. but it does not work as expected.

Code: [Select]

struct Tcoord
 {
     float x,y;
 };

 struct BoundBox
  {
      float halfWidth;
      float halfHeight;
      Tcoord point1;
      Tcoord point2;
      Tcoord point3;
      Tcoord point4;
  };

void UpdateBoundBox(BoundBox* bBox,float Cx,float Cy,float THETA )
     {
        float cosw = cos(THETA);
        float sinw  = sin(THETA);
        float x1 = Cx - bBox->halfWidth;
        float x2 = Cx + bBox->halfWidth;
        float x3 = Cx + bBox->halfWidth;
        float x4 = Cx - bBox->halfWidth;
        float y1 = Cy + bBox->halfHeight;
        float y2 = Cy + bBox->halfHeight;
        float y3 = Cy - bBox->halfHeight;
        float y4 = Cy - bBox->halfHeight;
        bBox->point1.x = x1 * cosw - y1 * sinw;
        bBox->point1.y = x1 * cosw + y1 * sinw;
        bBox->point2.x = x2 * cosw - y2 * sinw;
        bBox->point2.y = x2 * cosw + y2 * sinw;
        bBox->point3.x = x3 * cosw - y3 * sinw;
        bBox->point3.y = x3 * cosw + y3 * sinw;
        bBox->point4.x = x4 * cosw - y4 * sinw;
        bBox->point4.y = x4 * cosw + y4 * sinw;

     }


Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: software rotation question.
« Reply #3 on: July 16, 2007 »
ok, sorry. Here I made a working example for you.

Code: [Select]
' declare all vars, fixed size arrays.

option static
option explicit

' screen size
const xres=800
const yres=600

' x offset ;
dim shared halfx as integer = (xres/2)
' y offset;
dim shared halfy as integer = (yres/2)

' set up 4 points.

dim shared as integer points=4
dim shared as double xp(points)
dim shared as double yp(points)
dim as integer a

for a=1 to 4
    read xp(a),yp(a)
next

#include "tinyptc.bi"

declare sub rotate_draw()

dim shared as uinteger buffer (xres*yres)

        IF( PTC_OPEN( " + SAREK + ", XRES, YRES ) = 0 ) THEN
        END -1
    END IF   
   
        while(1)
            rotate_draw()
            ptc_update@buffer(0)
            erase buffer
        wend
       
sub rotate_draw()
   
    dim as double MO1,MO2, THETA,nnn,mmm
    dim as integer x,tx,ty
' convert 1 radian to 1 degree;   
    theta=1*3.14/180
   
    MO1= COS( THETA ):' GENERATE MATRIX CONSTANT 1
    MO2= SIN ( THETA ):' GENERATE MATRIX CONSTANT 2
   
FOR X=1 TO POINTS

       
        '-----------------------------------------------------------------------
        ' ROTATE
        '-----------------------------------------------------------------------
       
        NNN= XP (X)
        MMM=YP (X)
       
        XP(X) = MO1 * NNN - MO2 * MMM
        YP(X) = MO1 * MMM + MO2 * NNN
       
        ' transform;
       
tx=(xp(x)*20)+halfx
ty=(yp(x)*20)+halfy

' plot;

if tx>0 and tx<xres and ty>0 and ty<yres then
    buffer(tx+(ty*xres))=&hffffff
end if
   
NEXT

end sub       
' points data;
data -10,-10,10,-10,10,10,-10,10
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: software rotation question.
« Reply #4 on: July 16, 2007 »
thanks again shocky for your example.  :)

I have found the selution and it works ok.

again thank you.
« Last Edit: July 16, 2007 by Emil_halim »