Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find vertices in equilateral triangle mesh originating from a center vertex

Tags:

c#

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!

like image 520
user2716609 Avatar asked Nov 11 '22 23:11

user2716609


1 Answers

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;
}
like image 83
asimes Avatar answered Nov 14 '22 22:11

asimes