“for n_sides:”答案是最简单的。对于建议您可以通过使用复数来简化计算的人来说,几乎所有数学库都具有基于表格的 cos() 和 sin() 例程以及有效的插值,因此无需深入研究相对晦涩的解决方案。通常可以初始化一个常规的 n 边形,并且 OpenGL 的硬件缩放用于针对任何特定实例对其进行缩放/转换。
如果您想成为硬核,请预先生成您需要的所有 n 边形并将它们加载到顶点缓冲区中。
顺便说一句,这是 Lua 中的上述解决方案。它只是打印出坐标,但您当然可以自由地在数组/表格中返回坐标。返回的坐标可用于初始化 OpenGL GL_LINE_LOOP 网格图元。
require 'math'
-- computes coordinates for n-sided, regular polygon of given radius and start angle
-- all values are in radians
function polypoints(sides, radius, start)
local x_center = 0.0
local y_center = 0.0
local angle = start
local angle_increment = 2 * math.pi / sides
local x=0.0
local y=0.0
print(string.format("coordinates for a %d sided regular polygon of radius %d\nVertex",sides,radius),"X"," ","Y")
for i=1,sides do
x = x_center + radius * math.cos(angle)
y = y_center + radius * math.sin(angle)
print(string.format("%d\t%f\t%f",i,x,y))
angle = angle + angle_increment
end
end
-- Generate a regular hexagon inscribed in unit circle
polypoints(6, 1.0, 0.0)