【C++】多角形
R = m/2sin(180/n)
n:多角形の辺(正n角形)の数
m:正多角形の辺の長さ
R:求める円の半径
引用:正多角形の描き方
#define _USE_MATH_DEFINES // M_PI #include <cmath> #include <iostream> using namespace std; int main() { // 度をラジアンに変換する auto ToRadian = [](double deg){ return deg * M_PI / 180.0; }; // ラジアンを度に変換する auto ToDegree = [](double rad){ return rad * 180.0 / M_PI; }; // 多角形の半径を算出する auto CalcRadius = [ToRadian](double m, int n){ return m / (2 * sin(ToRadian(180/n))); }; // 3~10角形の半径を表示する(1辺の長さは1.0) int n[]{ 3, 4, 5, 6, 7, 8, 9, 10 }; for (int x : n){ cout << CalcRadius(1.0, x) << endl; } return 0; }
円に内接・外接する正n角形の辺の長さ
#define _USE_MATH_DEFINES // M_PI #include <Siv3D.hpp> void Main() { // ラジアン変換 auto toRadian = [](double deg){ return deg * M_PI / 180; }; // 座標変換 auto toPosition = [toRadian](double angle, double radius) { double sin = std::sin(toRadian(angle)) * radius; double cos = std::cos(toRadian(angle)) * radius; Vec2 position(sin, cos); return position; }; // 頂点算出 auto calcVertices = [toPosition](int n, double radius) { std::vector<Vec2> vertices; double angle = 360.0 / static_cast<double>(n); for (int i = 0; i < n; ++i) { double angleDelta = angle * i; vertices.emplace_back(toPosition(angleDelta, radius)); } return vertices; }; // 円に外接する正n角形の辺の長さ auto calcLengthOfCircumcircle = [](int n, double radius) { return 2.0 * radius * std::tan(M_PI / static_cast<double>(n)); }; // 円に内接する正n角形の辺の長さ auto calcLengthOfInscribedcircle = [](int n, double radius) { return 2.0 * radius * std::sin(M_PI / static_cast<double>(n)); }; int n = 8; // 角数 double radius = 100.0; // 半径 auto vertices = calcVertices(n, radius); // 頂点 auto offset = Vec2(Window::Center().x, Window::Center().y); // オフセット Font font(15); std::wstringstream ss; ss << L"外接円の辺の長さ:" << calcLengthOfCircumcircle(n, radius) << L"\n"; ss << L"内接円の辺の長さ:" << calcLengthOfInscribedcircle(n, radius); while (System::Update()) { font(ss.str()).draw(); for (int i = 0; i < n; ++i) { int index = (i + 1 < n) ? i + 1 : 0; Line(vertices[i] + offset, vertices[index] + offset).draw(); } } }
参考:円に外接する正多角形 - 高精度計算サイト
円に内接する正多角形 - 高精度計算サイト
正多角形 をスクリプトから生成してみる | Lonely Mobiler