模組:RegularPolygon
外观
local p = {}
local py = require("Module:PythonScript")
-- 載入所需的 Python 有理數運算器
local fractions = py.require("fractions")
local operator = py.require("operator")
-- 建立上標與下標的 Unicode 對照表 (使用 U+2044 FRACTION SLASH)
local SUPERSCRIPTS = {
["0"]="⁰", ["1"]="¹", ["2"]="²", ["3"]="³", ["4"]="⁴",
["5"]="⁵", ["6"]="⁶", ["7"]="⁷", ["8"]="⁸", ["9"]="⁹"
}
local SUBSCRIPTS = {
["0"]="₀", ["1"]="₁", ["2"]="₂", ["3"]="₃", ["4"]="₄",
["5"]="₅", ["6"]="₆", ["7"]="₇", ["8"]="₈", ["9"]="₉"
}
-- 字串轉換輔助函數
local function to_superscript(num_str)
return (string.gsub(num_str, "%d", SUPERSCRIPTS))
end
local function to_subscript(num_str)
return (string.gsub(num_str, "%d", SUBSCRIPTS))
end
-- ====================================================
-- 核心函數:將 Python Fraction 轉為帶分數字串
-- ====================================================
local function format_mixed_fraction(frac, spacer)
spacer = spacer or ""
local n = frac.numerator
local d = frac.denominator
-- 1. 處理正負號 (Fraction 會自動將分母轉正,因此負號只會出現在分子)
local is_neg = false
if operator.lt(n, 0) then
is_neg = true
n = operator.neg(n) -- 取絕對值
end
-- 2. 利用 operator 進行帶分數的商數與餘數計算 (確保 BigInt 不會因 Lua number 溢位)
local q = operator.floordiv(n, d)
local r = operator.mod(n, d)
local sign_str = is_neg and "-" or ""
-- 3. 轉為字串 (tostring 會自動呼叫 Python Proxy 的 __str__)
local q_str = tostring(q)
local r_str = tostring(r)
local d_str = tostring(d)
-- 4. 根據餘數與商數組合字串
if r_str == "0" then
-- 剛好整除 (純整數)
return sign_str .. q_str
elseif q_str == "0" then
-- 純真分數 (沒有前面的整數部分)
return sign_str .. to_superscript(r_str) .. "⁄" .. to_subscript(d_str)
else
-- 帶分數
return sign_str .. q_str .. spacer .. to_superscript(r_str) .. "⁄" .. to_subscript(d_str)
end
end
-- ==========================================
-- SVG 正多邊形繪製邏輯
-- ==========================================
-- 輔助函數:將向量單位化並乘上指定長度
local function normalizeVector(dx, dy, length)
local d = math.sqrt(dx * dx + dy * dy)
if d == 0 then return 0, 0 end
return dx * length / d, dy * length / d
end
function p.draw(frame)
local args = frame.args
local n = tonumber(args.n) or 5
if n < 3 then n = 3 end
local width, height = 300, 300
local cx, cy = width / 2, height / 2
local R = 110 -- 外接圓半徑
-- 使用有理數引擎精確計算圓心角與內角
local centralAngleRat = fractions.Fraction(360, n)
local interiorAngleRat = fractions.Fraction(n - 2, n) * 180
local centralAngleRad = 2 * math.pi / n
local svgContent = mw.html.create('')
-- 1. 繪製外接圓
svgContent:tag('circle')
:attr('cx', cx):attr('cy', cy):attr('r', R)
:css('fill', 'none'):css('stroke', '#d3d3d3'):css('stroke-width', '2px')
-- 2. 計算頂點與繪製輻射線
local vertices = {}
for i = 0, n - 1 do
local angle = -math.pi / 2 + i * centralAngleRad
local vx = cx + R * math.cos(angle)
local vy = cy + R * math.sin(angle)
table.insert(vertices, {x = vx, y = vy})
svgContent:tag('line')
:attr('x1', cx):attr('y1', cy)
:attr('x2', vx):attr('y2', vy)
:css('stroke', '#d3d3d3'):css('stroke-width', '1.5px')
end
-- 2.5 繪製圓心處的小白圓圈
svgContent:tag('circle')
:attr('cx', cx):attr('cy', cy):attr('r', 3.5)
:css('fill', '#ffffff'):css('stroke', '#d3d3d3'):css('stroke-width', '1.5px')
-- 3. 繪製圓心角
-- 當邊數較多 (n >= 12) 時,將圓心角標示移至右側扇形並放大弧半徑,避免與上方內角重疊
local sectorIdx = (n >= 12) and math.floor(n / 4) or 1
local arcRadiusCenter = (n >= 12) and 85 or 35
local vStart = vertices[sectorIdx]
local vEnd = vertices[sectorIdx + 1]
local dx0, dy0 = vStart.x - cx, vStart.y - cy
local nx0, ny0 = normalizeVector(dx0, dy0, arcRadiusCenter)
local p0_x, p0_y = cx + nx0, cy + ny0
local dx1, dy1 = vEnd.x - cx, vEnd.y - cy
local nx1, ny1 = normalizeVector(dx1, dy1, arcRadiusCenter)
local p1_x, p1_y = cx + nx1, cy + ny1
local centerArcPath = string.format("M %.2f %.2f A %.2f %.2f 0 0 1 %.2f %.2f",
p0_x, p0_y, arcRadiusCenter, arcRadiusCenter, p1_x, p1_y)
svgContent:tag('path')
:attr('d', centerArcPath)
:css('fill', 'none'):css('stroke', '#a0a0a0'):css('stroke-width', '1.5px')
-- 圓心角文字(使用有理數帶分數輸出)
local mx_c, my_c = dx0 + dx1, dy0 + dy1
local textR_c = (n >= 12) and (arcRadiusCenter - 26) or (arcRadiusCenter + 18)
local nx_c, ny_c = normalizeVector(mx_c, my_c, textR_c)
local centralText = format_mixed_fraction(centralAngleRat,"") .. "°"
svgContent:tag('text')
:attr('x', cx + nx_c):attr('y', cy + ny_c + 4)
:css('font-family', 'sans-serif'):css('font-size', '13px')
:css('fill', '#999999'):css('text-anchor', 'middle')
:wikitext(centralText)
-- 4. 繪製內角 (以最上方的 V1 為圓心)
local topVx, topVy = vertices[1].x, vertices[1].y
local sideLength = 2 * R * math.sin(math.pi / n)
local arcRadiusInterior = math.min(30, sideLength * 0.45)
local dx_left, dy_left = vertices[n].x - topVx, vertices[n].y - topVy
local nx_left, ny_left = normalizeVector(dx_left, dy_left, arcRadiusInterior)
local pLeft_x, pLeft_y = topVx + nx_left, topVy + ny_left
local dx_right, dy_right = vertices[2].x - topVx, vertices[2].y - topVy
local nx_right, ny_right = normalizeVector(dx_right, dy_right, arcRadiusInterior)
local pRight_x, pRight_y = topVx + nx_right, topVy + ny_right
local interiorArcPath = string.format("M %.2f %.2f A %.2f %.2f 0 0 0 %.2f %.2f",
pLeft_x, pLeft_y, arcRadiusInterior, arcRadiusInterior, pRight_x, pRight_y)
svgContent:tag('path')
:attr('d', interiorArcPath)
:css('fill', 'none'):css('stroke', '#000000'):css('stroke-width', '1.5px')
-- 內角文字(使用有理數帶分數輸出)
local interiorText = format_mixed_fraction(interiorAngleRat," ") .. "°"
local textOffset = math.max(arcRadiusInterior + 14, 22)
svgContent:tag('text')
:attr('x', topVx):attr('y', topVy + textOffset)
:css('font-family', 'sans-serif'):css('font-size', '14px')
:css('fill', '#000000'):css('text-anchor', 'middle')
:wikitext(interiorText)
-- 5. 繪製多邊形本體
local polygonPoints = {}
for i = 1, n do
table.insert(polygonPoints, string.format("%.2f,%.2f", vertices[i].x, vertices[i].y))
end
svgContent:tag('polygon')
:attr('points', table.concat(polygonPoints, " "))
:css('fill', 'none'):css('stroke', '#000000'):css('stroke-width', '3px')
:css('stroke-linejoin', 'round')
-- 6. 繪製頂點小圓圈
for i = 1, n do
svgContent:tag('circle')
:attr('cx', vertices[i].x):attr('cy', vertices[i].y):attr('r', 4)
:css('fill', '#ffffff'):css('stroke', '#000000'):css('stroke-width', '2px')
end
return mw.svg.new()
:setImgAttribute('width', '100%')
:setImgAttribute('style', 'max-width: 300px; height: auto;')
:setAttribute('viewBox', "0 0 " .. width .. " " .. height)
:setAttribute('xmlns', "http://www.w3.org/2000/svg")
:setContent(tostring(svgContent))
:toImage()
end
return p