Question

I am trying to draw a moving point on an sine curve and draw the tangent and normal to this point on the curve. I would also like to normalize these lines, but this is not my main concern.

So far I think my tangent is good but I can't find how to draw the normal.

Here is what I have so far : Two defines to avoid typing the in the middle of the code

#define SIN(A, y, x) A * sin(((2 * M_PI) / y) * x)
#define COSDY(A, y, x) A * ((2 * M_PI) / y) * cos(((2 * M_PI) / y) * x)

And those are the drawings I do for the curve, the tangent and then the normal:

glBegin(GL_LINE_STRIP);

for (x = -1.f; x <= 1.f; x += 0.01) {
  glVertex3f(x, SIN(1, 2, x), 1.f);
}

glEnd();

glPointSize(10.0);
glBegin(GL_POINTS);

glVertex3f(position, SIN(1, 2, position), 1.f);

glEnd();
glPointSize(1.0);

float dx = 1;
float dy = COSDY(1, 2, position);

float mag = sqrtf(dx * dx + dy * dy);
dx /= mag;
dy /= mag;

glBegin(GL_LINES);

glVertex3f(position, SIN(1, 2, position), 1.f);
glVertex3f(position + dx, position + dy, 1.f);

glEnd();

glBegin(GL_LINES);

glVertex3f(position, SIN(1, 2, position), 1.f);
glVertex3f(position + dy, position - 1 / dx, 1.f);

glEnd();

As I said the tangent looks ok but the normal is definitely not ok.

Bad normals

Can you see what is the problem in this?

Was it helpful?

Solution

The calculation of the tangent basically looks ok. I don't quite understand the way you draw it, though. If (dx, dy) is your tangent vector, shouldn't your drawing code look like this?

glVertex3f(position, SIN(1, 2, position), 1.f);
glVertex3f(position + dx, SIN(1, 2, position) + dy, 1.f);

In any case, with (dx, dy) being the tangent vector, your two options for the normal vector are (dy, -dx) and (-dy, dx). Remember that the scalar product of the two vectors needs to be zero for the two vectors to be orthogonal. Your normal drawing becomes:

glVertex3f(position, SIN(1, 2, position), 1.f);
glVertex3f(position + dy, SIN(1, 2, position) - dx, 1.f);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top