# Equilateral triangle generation

This topic is 3509 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
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
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 = 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
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
you're welcome. you can rate using the link next to the user rating at the bottom of posts.

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013162
×