Advertisement

triangle calculations?

Started by November 02, 2000 11:54 PM
6 comments, last by avianRR 24 years ago
I''m trying to write a program that requites me to draw two triangles with 4 points. The for points are a unit squate in the x-z plane with the y axis baing variable. i.e. p1 = 0, y1, 0 p2 = 1, y2, 0 p3 = 1, y3, 1 p4 = 0, y4, 1 The triangles are drawn as p1->p2->p3 and p2->p4->p3. The problem is that given any point (x,y) I need to know what y is. I''ve been trying to get this to work but every formula I''ve tryed so far hasn''t worked. The values passed for x and y are always non negative and always less than 1 any ideas would be greatly appreciated!!!
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
I was facing the exact same problem before. I never actually thought of a way to make it work, but I have an idea. This may not be the best method. What you must do it create a plane that splits the square. If the square is on the x-z plane then the normal of the plane would point to like (1, 0, 1). Once you have the plane, you need to test what side the point is on. Once you find out which side, then it should be easy to find the height on the one triangle. Hope that helps.

*** Triality ***
*** Triality ***
Advertisement
Not sure I understood your question right... cos its ssoo simple.
Anyways, here goes:
You start with 4 points (square):
x1 y1 z1
x2 y2 z2
x3 y3 z3
x4 y4 z4

and now instead of drawing a square, you wanna draw 2 triangles (so it will appear as a square, but will draw faster on most cards)
So:

Triangle 1:
x1 y1 z1
x2 y2 z2
x4 y4 z4

Triangle 2:
x2 y2 z2
x3 y3 z3
x4 y4 z4

Just fill those into glVertex*(blah blah);
should work great
Why don''t you just do this

glBegin(GL_TRIANGLE_STRIP);
// Specify your 4 points here
glEnd();

a triangle strip will do it for you without you having to do calculations.

Hope this helped !!

I don''t think you guys read his message.
"The problem is that given any point (x,y) I need to know what y is"

*** Triality ***
*** Triality ***
But that doesn''t make any sense at all...
Given a point (x, y)... y is y. Whats the problem??
Advertisement
Arrg!!! Given a point (x, z), find y, and that is over two triangles.

*** Triality ***
*** Triality ***
Thanks anyway, I found the answer...
Oh, and apologies for the typo... I was stating the point x,y and meant x,z! here's the solution for anyone that's interested.

For any set of 4 y values I'm drawing 2 triangles p1-p2-p3 and p2-p3-p4 with the points...

p1 = [0,y1,0];
p2 = [1,y2,0];
p3 = [1,y3,1];
p4 = [0,y4,1];

I then need to know the value of y at any point x,z where x >= 0 && x < 1, z >= 0 && z < 1

float GetY(float x,float z
float y1,float y2,float y3,float y4){
float yl,yc,yr;

yl = y1 + z * (y3 - y1);
yc = y2 + z * (y3 - y2);
yr = y2 + z * (y4 - y2);

if(x < (1 - z)){
if(1-z == 0)
return e3;
return yl + (x / (1 - z)) * (yc - yl);
}
else{
if(z == 0)
return e2;
return yr + ((1 - x) / z) * (yc - yr);
}

return -999999.999999;
}


Edited by - avianRR on November 5, 2000 1:09:25 AM
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]

This topic is closed to new replies.

Advertisement