MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ generate_regular_polygon()

MAYAFLUX_API std::vector< glm::vec3 > MayaFlux::Kinesis::generate_regular_polygon ( const glm::vec3 &  center,
float  radius,
size_t  sides,
const glm::vec3 &  normal = glm::vec3(0, 0, 1),
float  phase_offset = 0.0F 
)

Generate vertices of a regular n-gon.

Parameters
centerPolygon centroid
radiusCircumradius (center to vertex distance)
sidesNumber of sides (must be >= 3)
normalPolygon plane normal
phase_offsetAngular offset in radians (default: 0)
Returns
N+1 vertices (closed path)

Vertices positioned at angles: θ_k = (2πk/N) + phase_offset

Definition at line 137 of file GeometryPrimitives.cpp.

143{
144 if (sides < 3) {
145 sides = 3;
146 }
147
148 std::vector<glm::vec3> vertices;
149 vertices.reserve(sides + 1);
150
151 glm::vec3 n = glm::normalize(normal);
152 glm::vec3 u;
153
154 if (std::abs(n.z) < 0.9F) {
155 u = glm::normalize(glm::cross(n, glm::vec3(0, 0, 1)));
156 } else {
157 u = glm::normalize(glm::cross(n, glm::vec3(1, 0, 0)));
158 }
159
160 glm::vec3 v = glm::cross(n, u);
161
162 float angle_step = glm::two_pi<float>() / static_cast<float>(sides);
163
164 for (size_t i = 0; i <= sides; ++i) {
165 float angle = static_cast<float>(i) * angle_step + phase_offset;
166 float cos_a = std::cos(angle);
167 float sin_a = std::sin(angle);
168
169 glm::vec3 position = center + radius * (cos_a * u + sin_a * v);
170
171 vertices.push_back(position);
172 }
173
174 return vertices;
175}