user2716609
user2716609

Reputation: 51

Find vertices in equilateral triangle mesh originating from a center vertex

I'd like to ask whether there is code out there or if you can give me some help in writing some (C#, but I guess the maths is the same everywhere).

I'd like to specify a center point from which an equilateral triangle mesh is created and get the vertex points of these triangles. The center point should not be a face center, but a vertex itself. A further input would be the size of the triangles (i.e side length) and a radius to which triangle vertices are generated.

The reason behind it is that I want to create a mesh which is centered nicely on the screen/window center with as little code as possible. I just find mesh generation code, but not a "radial outward propagation" example.

In the end, I'd like to have the subsequently farther away vertices being displaced in a logarithmic fashion, but I guess that's just an easy addition once the mesh code is there.

Can anybody help me with that? Thanks!

Upvotes: 5

Views: 1724

Answers (2)

user2716609
user2716609

Reputation: 51

Thank you very much for your answer. I will play around with your code - the propagation part will come handy for sure.

In the meantime I have played around with hexagons instead of triangles and this codes works fairly alright for the same purpose.:

//populate array from the centre hex, going outwards the desired number of hex rings

            for (int i = 0; i < numberOfHexagonRings; i++)
            {
                for (double j = -i; j <= i; j++)
                    for (double k = -i; k <= i; k++)
                        for (double l = -i; l <= i; l++)
                            if ((Math.Abs(j) + Math.Abs(k) + Math.Abs(l) == i * 2) &&   (j + k + l == 0))
                            {
                                positionX = (int)(screenCenterX + ((double)sideLength * (l / 2 + j)));
                                positionY = (int)(screenCenterY + (3/2 * ((double)sideLength / Math.Sqrt(3)) * l));

Upvotes: 0

asimes
asimes

Reputation: 5894

You need to specify two things, a radius and the direction that the first triangle points.

  • The radius will be the distance from the initial point to the vertices of the first triangle. All triangles will have the same radius.
  • The direction is some specification in radians. I will assume that 0 means pointing to the right (PI would be point to the left).

Finding the vertices of the first triangle can be done like this (pseudo-code, not language specific):

float theta = 0; // The direction, 0 means pointing to the right
float thetaInc = TWO_PI/3; // 3 because you want a triangle
for (int i = 0; i < 3; i++) {
    vertX[i] = initialPointX+cos(theta)*radius;
    vertY[i] = initialPointY+sin(theta)*radius;
    theta += thetaInc;
}

There are many ways to find the center points of the neighboring triangles. One way would be to use the same code but initialize theta = TWO_PI/6, replace radius with foo (see math below), assign new center points of neighboring triangles in the for loop, and then use the same code with an appropriately rotated direction (theta += PI) to find the vertices of those triangles.

Distance from one triangle center to another only knowing radius:

  • hypotenuse = sqrt(sq(radius)+sq(radius));
  • halfHypotenuse = hypotenuse/2.0;
  • Pythagorean theorem to find distance from center of triangle to center of an edge: foo = sqrt(sq(radius)-sq(halfHypotenuse));
  • Final distance = foo*2.0;

Code to find the center points of the neighboring triangles:

float[] nx = new float[3];
float[] ny = new float[3];

float theta = TWO_PI/6;
float hyp = sqrt(sq(radius)+sq(radius));
float halfHyp = hyp/2.0;
float foo = sqrt((sq(radius)-sq(halfHyp)))*2.0;
for (int i = 0; i < 3; i++) {
    nx[i] = initialPointX+cos(theta)*foo;
    ny[i] = initialPointY+sin(theta)*foo;
    theta += thetaInc;
}

Upvotes: 1

Related Questions