「for n_sides:」の答えが最も簡単です。複素数を使用して計算を簡素化できると提案した人にとって、ほとんどすべての数学ライブラリには、効率的な補間を備えたテーブルベースの cos() および sin() ルーチンがあるため、比較的あいまいなソリューションを掘り下げる必要はありません。通常、通常の n-gon を初期化して、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)