MayaFlux 0.2.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 126 of file GeometryPrimitives.cpp.

132{
133 if (sides < 3) {
134 sides = 3;
135 }
136
137 std::vector<glm::vec3> vertices;
138 vertices.reserve(sides + 1);
139
140 glm::vec3 n = glm::normalize(normal);
141 glm::vec3 u;
142
143 if (std::abs(n.z) < 0.9F) {
144 u = glm::normalize(glm::cross(n, glm::vec3(0, 0, 1)));
145 } else {
146 u = glm::normalize(glm::cross(n, glm::vec3(1, 0, 0)));
147 }
148
149 glm::vec3 v = glm::cross(n, u);
150
151 float angle_step = glm::two_pi<float>() / static_cast<float>(sides);
152
153 for (size_t i = 0; i <= sides; ++i) {
154 float angle = static_cast<float>(i) * angle_step + phase_offset;
155 float cos_a = std::cos(angle);
156 float sin_a = std::sin(angle);
157
158 glm::vec3 position = center + radius * (cos_a * u + sin_a * v);
159
160 vertices.push_back(position);
161 }
162
163 return vertices;
164}