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

◆ generate_regular_polygon()

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 136 of file GeometryPrimitives.cpp.

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

References position, and radius.