4 Replies - 366 Views - Last Post: 02 August 2014 - 03:31 PM Rate Topic: -----

#1 Rhandrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-July 14

(OpenGL)Drawing not updated when there is a conditional in the code

Posted 01 August 2014 - 12:03 AM

I'm making a program to draw a trajectory, was using as a basis the tangent, but when I put 90 degrees, the tangent is very large, so I thought of putting an if (angle == 90) did not use the tangent.
the design is right, but after choosing it 90 degrees it stops to draw.
for example
1-30 degrees - draws
2-45 degrees ---- draws
3 draws -90 degrees ---
4-60 degrees --- not drawing
5 --- 80 degrees --- not drawing
*
*
anyone know what is happening? making routine design is presented below:
thank


void Draw() {
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);

convert(graus);

if (ant){
x=xx;
y=yy;
}

double rad = (graus*3.1415926)/180;
GLfloat varx= s*abc;
GLfloat vary= f*abc*tan(rad);


if (graus==90 || graus == 270) {varx = 0;
vary = f*abc;
}

glVertex3f(x, y, 0.0); 
glVertex3f(x+varx, y+vary, 0.0); 

if(_kbhit())
{
xx= x+s*abc;
yy= y+f*abc*tan(rad);

cout<< "Digite o angulo: ";
cin>>graus;
rad = (graus*3.1415926)/180; 
convert(graus);

ant =1;
abc = 0.0;
}

glEnd();
glFlush();
abc += var;
glutPostRedisplay();
}


Is This A Good Question/Topic? 0
  • +

Replies To: (OpenGL)Drawing not updated when there is a conditional in the code

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 2,264
  • Joined: 31-December 10

Re: (OpenGL)Drawing not updated when there is a conditional in the code

Posted 01 August 2014 - 06:12 AM

The tangent of 90 degrees is undefined because it results in a division by zero.
Was This Post Helpful? 0
  • +
  • -

#3 Rhandrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-July 14

Re: (OpenGL)Drawing not updated when there is a conditional in the code

Posted 01 August 2014 - 07:42 AM

View Postvividexstance, on 01 August 2014 - 06:12 AM, said:

The tangent of 90 degrees is undefined because it results in a division by zero.


I know, but I used a case for 90 degrees, and when de glutmainloop goes to that case, it stops to draw,
do you know why?
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 2,264
  • Joined: 31-December 10

Re: (OpenGL)Drawing not updated when there is a conditional in the code

Posted 01 August 2014 - 09:35 AM

Could you post the smallest and complete code instead of just a snippet?
Was This Post Helpful? 0
  • +
  • -

#5 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 125
  • View blog
  • Posts: 531
  • Joined: 15-March 14

Re: (OpenGL)Drawing not updated when there is a conditional in the code

Posted 02 August 2014 - 03:31 PM

As tan approaches 90 deg it approaches infinity. Tan is not a very useful function for calculating trajectories as there is a great loss of precision as you approach 90 deg (PI/2)

I am not quit sure what you are trying to do as I do not know what s, f, abc are for.

I am guessing you want the vector of an angle (ang). Depending on the coordinate system you can get it with sin and cos.

double magnitude = 1;   // to get a normalized unit vector
double angle = ?; // angle in radians
double vectorX = cos(angle)*magintude;
double vectorY = sin(angle)*magnitude;


If you want the vector perpendicular to that then just add or subtract PI/2 to the angle
double vectCrossX = cos(ang+3.14/2)*magintude;
double vectCrossY = sin(ang+3.14/2)*magnitude;
// or
double vectCrossX = -sin(ang)*magintude;
double vectCrossY = cos(ang)*magnitude;







Vector results for 0,90,180,270,360......

double PI = 3.14159....
cos(0) == 1;   //X
sin(0) == 0;   //Y

sin(PI/2) == 0; // PI/2 is 90 degrees
cos(PI/2) == 1;

sin(PI) == -1; // 180
cos(PI) == 0;

sin(PI*(3/2)) == 0; // 270
cos(PI*(3/2)) == -1;

sin(PI*2) == 1; //360 or back to 0
cos(PI*2) == 0;



As you can see there is no need for any if statements, nor do you get the very large numbers, and precision loss, as you get to PI*(1/2) and PI*(2/3).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1