Change vertical of a plane by rotating

Asked

Viewed 67 times

8

I have a plan on the screen and I want to rotate it by five degrees without using the glRotate, I have the code:

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>

#define HEIGHT 500
#define WIDTH 500
#define POINTS 5

#define TRANSLATE 5
#define ROTATION 5.0
#define SCALE_M 0.5
#define SCALE_P 1.5

void display();
void specialKeys();

typedef struct {
  float x;
  float y;
  float z;
} Dot;

Dot dots[5];
int i, translate = 0, rotate = 0, scale = 0;
char typeRotate = '\0';

Dot point(float x, float y, float z) {
  Dot p;
  p.x = x;
  p.y = y;
  p.z = z;

  return p;
}

void createPoints() {
  dots[0] = point(00, 00, 00);
  dots[1] = point(20, 00, 00);
  dots[2] = point(20, 20, 00);
  dots[3] = point(00, 20, 00);
}

void drawDot(float x, float y, float z) { glVertex3f(x / 30, y / 30, z / 30); }

void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  for (i = 0; i < POINTS; i++) {
    dots[i].x = (dots[i].x * cos(-ROTATION)) - (dots[i].y * sin(-ROTATION));
    dots[i].y = (dots[i].x * sin(-ROTATION)) + (dots[i].y * cos(-ROTATION));
  }

  glBegin(GL_POLYGON);
  glColor3f(1.0, 1.0, 0.0);
  for (i = 0; i < POINTS; i++)
    drawDot(dots[i].x, dots[i].y, dots[i].z);

  glEnd();

  glFlush();
  glutSwapBuffers();
}

int main(int argc, char *argv[]) {
  createPoints();
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(WIDTH, HEIGHT);
  glutInitWindowPosition(150, 150);
  glutCreateWindow("Exercicio Lista");
  glEnable(GL_DEPTH_TEST);
  glutDisplayFunc(display);
  glutMainLoop();

  return 0;
}

The part where I try to rotate the plane is:

for (i = 0; i < POINTS; i++) {
    dots[i].x = (dots[i].x * cos(-ROTATION)) - (dots[i].y * sin(-ROTATION));
    dots[i].y = (dots[i].x * sin(-ROTATION)) + (dots[i].y * cos(-ROTATION));
}

Where I try to rotate the dots by following a formula I saw in Geometric Transformations USP. But the result was not a 5-degree rotation on the Z axis, you can see while running the file.

P.S.: Compilation is:

gcc -o lista lista.c -lm -lglut -lGL -lGLU && ./lista

I hope they help to discover the error in the calculation, I did not try to do with the matrices of the PDF because I did not understand.

  • Depending on what you do, you can optimize nonsense with pre-calculated tables. If it is always one in a degree, you can already make a loop that calculates a vector seno[angulo] = sin( angulo / 360 * 2 * M_PI ); ai in the formula already uses ( dots[i].x * seno[ROTATION] ). For values less than zero or greater than 360 just use module. Remember that the sine equals cosine, only need to move 90 degrees.

  • Really, in the case was just a simple exercise so I did not more elaborate, but this way would be great for a job I have, thanks :) I’m still readjusting the formula, because in addition to rotating is decreasing the scale

1 answer

8


The trigonometric functions as sin and cos do not work with degrees of arc (from 0 to 360) and yes with radians.

A radian is equivalent to a projection of the radius on the circumference:

proporção de um radiano em relação à circunferência

The whole loop equals to 2 * PI, which is more or less 6,28318530717958647692528676656.


So instead of using something like

sin( ROTATION )

You need

sin( ROTATION / 360 * 2 * M_PI )

Just adapt to your case. The constant M_PI is standard of math.h, adjustment if necessary.


Image taken from: http://www.differencebetween.com/difference-between-radian-and-vs-degree/

  • 1

    Thanks, I would never think of it, it worked!

  • 1

    Note that virtually everything from trigonometry is usually in radians in general languages. Rarely does anything use arc degree. In more specialized languages or libraries you usually have an option, but in everyday life everything is radial.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.