Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: SoldierBoy on February 25, 2008

Title: 2D Rotation by a Center Axis
Post by: SoldierBoy on February 25, 2008
For the life of me I can't figure this out.  I've read many tutorials and looked at other examples about rotation, and to be honest I'm ready to pull all my hair out. LOL

Can somebody please take a look at this and tell me what I should do to fix it?

I want to draw a star and rotate it on itself by its center.

Code: [Select]
void drawRotatedStar(buffer &buf, int centerX, int centerY, int radius, double angle, int rgb) {
line line1;
line line2;
line line3;
line line4;
line line5;

double matrix[2][2]; //Matrix of 2 * 2
double radians = angle*(PI/180);
matrix[0][0] = cos(angle);
matrix[1][0] = -sin(angle);
matrix[0][1] = sin(angle);
matrix[1][1] = cos(angle);
int oldX, oldY;

//Create our Stars
    line1.x1 = centerX;
    line1.y1 = centerY + radius;
    line1.x2 = (centerX + sin(4*PI/5) * radius);
    line1.y2 = (centerY + cos(4*PI/5) * radius);

line2.x1 = (centerX + sin(4*PI/5) * radius);
    line2.y1 = (centerY + cos(4*PI/5) * radius);
    line2.x2 = (centerX + sin(8*PI/5) * radius);
    line2.y2 = (centerY + cos(8*PI/5) * radius);

line3.x1 = (centerX + sin(8*PI/5) * radius);
    line3.y1 = (centerY + cos(8*PI/5) * radius);
    line3.x2 = (centerX + sin(2*PI/5) * radius);
    line3.y2 = (centerY + cos(2*PI/5) * radius);

line4.x1 = (centerX + sin(2*PI/5) * radius);
    line4.y1 = (centerY + cos(2*PI/5) * radius);
    line4.x2 = (centerX + sin(6*PI/5) * radius);
    line4.y2 = (centerY + cos(6*PI/5) * radius);

    line5.x1 = (centerX + sin(6*PI/5) * radius);
    line5.y1 = (centerY + cos(6*PI/5) * radius);
    line5.x2 = centerX;
    line5.y2 = centerY + radius;

//Rotate our stars
    oldX = line1.x1;
    oldY = line1.y1;
    line1.x1 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line1.y1 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
oldX = line1.x2;
    oldY = line1.y2;
    line1.x2 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line1.y2 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
oldX = line2.x1;
    oldY = line2.y1;
    line2.x1 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line2.y1 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
oldX = line2.x2;
    oldY = line2.y2;
    line2.x2 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line2.y2 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
    oldX = line3.x1;
    oldY = line3.y1;
    line3.x1 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line3.y1 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
    oldX = line3.x2;
    oldY = line3.y2;
    line3.x2 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line3.y2 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
    oldX = line4.x2;
    oldY = line4.y2;
    line4.x2 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line4.y2 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
    oldX = line4.x1;
    oldY = line4.y1;
    line4.x1 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line4.y1 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
    oldX = line5.x1;
    oldY = line5.y1;
    line5.x1 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line5.y1 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;
oldX = line5.x2;
    oldY = line5.y2;
    line5.x2 = (oldX * matrix[0][0]) + (oldY * matrix[0][1]) + centerX;
    line5.y2 = (oldX * matrix[1][0]) + (oldY * matrix[1][1]) + centerY;

//Draw our stars
    drawLine(buf, line1.x1, line1.x2, line1.y1, line1.y2, rgb);
    drawLine(buf, line2.x1, line2.x2, line2.y1, line2.y2, rgb);
    drawLine(buf, line3.x1, line3.x2, line3.y1, line3.y2, rgb);
    drawLine(buf, line4.x1, line4.x2, line4.y1, line4.y2, rgb);
    drawLine(buf, line5.x1, line5.x2, line5.y1, line5.y2, rgb);

}


Thanks in advance!  I plan on release the sourcecode to this little demo when I'm done.
Title: Re: 2D Rotation by a Center Axis
Post by: p01 on February 25, 2008
Make sure your object is centered around its own origin when you rotate it.

In your case, it is NOT since you add centerX and centerY during the initialization of the lineN.xM and lineN.yM variables. Remove these additions and voila.

The centerX and centerY variables are just an offset for where/when you want to draw on screen. They have nothing to do the calculations themselves.
Title: Re: 2D Rotation by a Center Axis
Post by: rain_storm on February 25, 2008
you can make a star by dividing a full circle (2*pi) by the number of points on the star and drawing lines between every second one. Then to rotate it just add you rotation to the starting angle. It works so long as the star has at least 5 points well it wouldnt be a star if it didnt but there is no upper limit. Heres the pseudo code

Code: [Select]
  rotation = rotation + pi/180
  points = 5
  size = 100
  pstep = 2*pi / points
  ang = rotation
  for p = 1 to points
    ang = ang + pstep
    x1 = cos(ang)*size + centerX
    y1 = sin(ang)*size + centerY
    x2 = cos(ang +2*pstep)*size + centerX
    y2 = sin(ang + 2*pstep)*size + centerY
    line x1,y1 to x2,y2
  next