# Equilateral triangle generation

## Recommended Posts

chrome68    132
Hi. I would like to generate an equilateral triangle on a plane in C++. I know the point P(p1,p2,p3) i wish to be the barycentric centre; the normal at that point, and another point on the plane 'a'. So 0 = N.(a-p) How can i calculate the 3 vertices of the triangle so they should lie on a circle (inscribed) of a radius 1, centred at P?

##### Share on other sites
Sneftel    1788
If I'm reading that right, it sounds like you don't care how the equilateral triangle is oriented, as long as it's in the plane. Correct me if I'm wrong.

First, calculate a vector in that plane. It doesn't matter which one. Try N × î, unless N is close to î, in which case use something else. I guess as long as you have a, you could use a-P. Then rotate that vector 120° around N, as well as -120° (by means of a rotation matrix, of course). Then just add those vectors to P. I guess you should probably normalize stuff somewhere in there.

##### Share on other sites
oliii    2196
easy peasy.

// for triangles, vnum = 3. supply a hint vector if you want to orientate // the polygon in a particular way. // normal should be normalised.void makePolygon(Vector* v, int vnum, const Vector& centre, const Vector& normal, float radius, const Vector* direction){    // vector used to define the direction the polygon is pointing.    Vector x;    // optional direction vector to align the polygon with.    if(direction != NULL)     {        x = *direction;    }    // else use arbitrary direction.    else    {        x.randomDirection();    }      // secondary vector, perpendicular to the third and the normal of plane    Vector z = x.crossProduct(normal);        // normal aligned with hypothetical x axis. choose another x axis.    while(z.length() < 0.00001f)    {        // compute new major and minor axes        x.randomDirection();        z = x.crossProduct(normal);    }    z.normalise();        // re-align first vector into the plane.    x = normal.crossProduct(z);    x.normalise();      // generate vertices.    float a = 0.0f;    float da = twopi() / vnum;    for(int i = 0; i < vnum; i ++, a+= da)    {        v[i] = centre + (x * cos(a) + z * sin(a)) * radius;    }}

in your case, vnum = 3, centre = P, the hint vector would be (A - P), and radius = 1.0f;

[Edited by - oliii on May 1, 2009 11:35:57 AM]

##### Share on other sites
chrome68    132
Thanks very much for your help. It worked a treat ;)

I used Vector3D 'x' directly as (A-P), without the x.randomDirection(). I couldn't quite get the 'radius' parameter to give me the correct coordinates. Instead I normalised 'x' so its length was equal to 1.0 and that essentially became the first vertex of the equilateral triangle. Thanks again oliii. How do you rep in this forum?

##### Share on other sites
oliii    2196
you're welcome. you can rate using the link next to the user rating at the bottom of posts.