Jump to content

Rectangle Rotation problem

- - - - -

  • Please log in to reply
5 replies to this topic

#1
varunit

varunit

    Newbie

  • Members
  • Pip
  • 4 posts
Hello friends i am facing a typical problem..
The following is my code to rotate a rectangle. It works fine for 60 and 90 degrees of rotation but not for 30 and 45 degrees. I am unable to figure out where i have went wrong.. Please have a look at this..

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

 int gd=DETECT,gm,deg;

int left,top,right,bottom;

 float rad,x1,y1,x2,y2,x3,y3,x4,y4;

 clrscr();

 initgraph(&gd,&gm,"");

   printf("Enter the co-ordinates of the rectangle :");

   scanf("%d%d%d%d",&left,&top,&right,&bottom);

 printf("\nEnter the angle to rotate :");

 scanf("%d",°);

 rad=deg*(3.14/180);


line(left,bottom,right,bottom);

line(left,bottom,left,top);

line(right,bottom,right,top);

line(left,top,right,top);


   x1=left*cos(rad)-bottom*sin(rad);

   y1=left*sin(rad)+bottom*cos(rad);

   x2=right*cos(rad)-bottom*sin(rad);

   y2=right*sin(rad)+bottom*cos(rad);

   x3=left*cos(rad)-top*sin(rad);

   y3=left*sin(rad)+top*cos(rad);

   x4=right*cos(rad)-top*sin(rad);

   y4=right*sin(rad)+top*cos(rad);


   line(fabs(x1),fabs(y1),fabs(x2),fabs(y2));

   line(fabs(x1),fabs(y1),fabs(x3),fabs(y3));

   line(fabs(x3),fabs(y3),fabs(x4),fabs(y4));

   line(fabs(x2),fabs(y2),fabs(x4),fabs(y4));

   getch();

}

Can you please guide me to rectify the error...



#2
varunit

varunit

    Newbie

  • Members
  • Pip
  • 4 posts
please reply guys.. very urgent assignment to be submitted by tomorrow...

#3
d0s

d0s

    Newbie

  • Members
  • PipPip
  • 10 posts
You have a rectangle which you are trying to rotate. But what are you rotating that rectangle around? Its center? Those equations using sin and cosine appear to be attempting to rotate the points of the rectangle around the origin of the coordinate space. If the center of your rectangle is not the same as your origin.. you're going to have problems.

#4
varunit

varunit

    Newbie

  • Members
  • Pip
  • 4 posts
yes my centre of the rectangle is not origin.. How to make it center ????

#5
d0s

d0s

    Newbie

  • Members
  • PipPip
  • 10 posts
Determine the center and then offset the X and Y values of the 4 points by that much. After rotation, offset them back.

so if I had a 1 unit square with origin being the lower left point (0, 0) Lower Left, (0, 1) Upper Left, (1, 1) Upper Right, (1, 0) Lower Right.

Center x = (x right - x left) / 2 = (1 - 0) / 2 = .5
Center y = (y top - y bottom) / 2 = (1 - 0) / 2 = .5

Subtract the center X and Y from their respective values (-.5, -.5) Lower Left, (-.5, .5) Upper Left, (.5, .5) Upper Right, (.5, -.5) Upper Right.

Now rotate the four points around the origin and then add back in how much to offset to move the rectangle back to the original center.

There may be some fancy equations that factor in the offset to be able to rotate around a given point in one step... i'll leave that to some math guru.

#6
varunit

varunit

    Newbie

  • Members
  • Pip
  • 4 posts
thanks a lot the information. I will try your suggestion and reply...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users