PDA

View Full Version : Problems with Normal Generation & Circle co-ordinates

Zeus_666
06-19-2001, 01:30 AM
Hi,
I have written two small programs that were to generate my surface normals and the co-ordinates for drawing a circle. I however am having problems. The result's of the programs some time include

-1.#IND

I have no idea what this means, the code is:

Circles

#include <iostream.h>
#include <math.h>

int radius =0, strips =0, i;
float angle = 0.0, x =0.0, z = 0.0;

void main ()
{
cout << "Enter Radius, Number of strips:\n\n";

for (i = 0; i <= strips; i++)
{
angle = ((float) i) / ((float) strips);
angle = 360.0 * angle;

cout << "x = " << x << "\nz = " << z << "\n\n";

}

}

Surface Normals

#include <math.h>
#include <iostream.h>

float V1 [3], V2 [3], N [3], A [3], B [3], C [3];
float scale;
char temp;
bool quit;

void main()
{
while (!quit) {
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout << "Please enter the co-ordinates for A, B, C:\n\n";
cout << "B****A\n* *\n* *\n* *\n* *\nC****D\n\n";

cin >> A[0] >> A[1] >> A[2];
cin >> B[0] >> B[1] >> B[2];
cin >> C[0] >> C[1] >> C[2];

cout << A[0] << A[1] << A[2];
cout << B[0] << B[1] << B[2];
cout << C[0] << C[1] << C[2];

V1[0] = B[0] - A[0];
V1[1] = B[1] - A[1];
V1[2] = B[2] - A[2];
V2[0] = C[0] - B[0];
V2[1] = C[1] - B[1];
V2[2] = C[2] - B[2];

N[0] = (V1[1] * V2[2]) - (V1[2] * V2[1]);
N[1] = (V1[2] * V2[0]) - (V1[0] * V2[2]);
N[2] = (V1[0] * V2[1]) - (V1[1] * V2[0]);

scale = (float) sqrt ((N[0] * N[0]) + (N[1] * N[1]) + (N[1] * N[1]));

N[0] = N[0] / scale;
N[1] = N[1] / scale;
N[2] = N[1] / scale;

//B****A
//* *
//* *
//* *
//* *
//C****D

cout << "\n\n" << N[0] << N[1] << N[2] << "\n\n";
cout << "Any more surface normals calculate?\n\n";
cin >> temp;

if (temp == 'n')
quit = true;
}
}

I would be most grateful for help.

Thanks, Paul

AndersO
06-19-2001, 02:25 AM
Not sure where you get -1.#IND. Sounds like some floating point error.

You sure following code is ok?:

scale = (float) sqrt ((N[0] * N[0]) + (N[1] * N[1]) + (N[1] * N[1]));
N[0] = N[0] / scale;
N[1] = N[1] / scale;
N[2] = N[1] / scale;

Normalizing a 3d vector?

Try:

scale = (float) sqrt ((N[0] * N[0]) + (N[1] * N[1]) + (N[2] * N[2]));
N[0] = N[0] / scale;
N[1] = N[1] / scale;
N[2] = N[2] / scale;

And maybe check for zero result on scale.

Zeus_666
06-19-2001, 04:24 AM
Hi,
The surface normal error occurs when i give the following data:

25.0 6.0 -35.0 -25.0 6.0 -35.0 -25.0 -1.0 -35.0

Michael Steinberg
06-19-2001, 05:19 AM
Did you see the index error AndersO pointed to?

AndersO
06-19-2001, 06:00 AM
Yes, reread my previous message! http://www.opengl.org/discussion_boards/ubb/smile.gif

Using those vectors you specified will eventually end in a divide by zero!. (In the normalizing code).

Cheers!

06-19-2001, 06:55 AM
Well, I'm a C/C++ programmer with a part-time job with the government. My experience thru mass debugging and programming shows that -1.#IND is a imaginary number (like the sq. rt. of -5 or so, like i). Usually if it's a Div. by 0 it displays 0 and later shows encryptic bugs. However, it sounds more like an imaginary number. Step thru your program and find out what number is causing all of this.

06-19-2001, 06:56 AM
Hope that helps out

Zeus_666
06-19-2001, 10:16 AM
Ok, made that number change pointed out and now i have the results from the above data:

-1.#IND -1.#IND -1.#INF

I'm stuck, when i do the calculation on my calculator i get 0 for the first value

Zeus_666
06-19-2001, 10:22 AM
Hi,
Fixed the index error pointed out, me tired, surface normal program now working, i think i will take a break fro mthis as i have been working on my program all yesterday and all today. Thanks, i will look at the circle generation tomorrow.

Paul