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

Page 1 of 1

## 4 Replies - 817 Views - Last Post: 02 August 2014 - 03:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=351390&amp;s=c6bf9a44426a6021b3290a75ee958854&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Rhandrey

Reputation: 0
• 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;
}

GLfloat varx= s*abc;

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;

cout<< "Digite o angulo: ";
cin>>graus;
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

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• 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.

### #3 Rhandrey

Reputation: 0
• 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

vividexstance, 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?

### #4 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• 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?

### #5 Blindman67

Reputation: 139
• Posts: 615
• 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).