図形の回転
メニューを表示するにはスワイプしてください
幾何図形(多角形など)をある点(多くの場合は原点)を中心に回転させるには、特定の数学的な公式を使用します。点 (x, y) を原点の周りに角度 θ(ラジアン単位)で回転させると、新しい座標 (x', y') は次のように計算されます。
x' = x * cos(θ) - y * sin(θ);y' = x * sin(θ) + y * cos(θ)。
この公式は三角法に基づいており、各点が原点を中心とした円弧に沿ってどのように移動するかを示します。Python では、math モジュールを使ってサインやコサイン関数にアクセスでき、これらの関数には角度をラジアンで指定する必要があります。
この公式を段階的に適用します。例えば、頂点が (1, 0)、(0, 1)、(-1, 0) にある三角形を原点の周りに90度(π/2ラジアン)回転させたいとします。各頂点について、上記の公式にxとyの値を代入し、新しい位置を計算します。すべての頂点に対してこれを行うことで、回転後の三角形が得られます。
幾何モデリングでは、多くの場合、点のリストとして表現された多角形を扱います。多角形全体を回転させるには、リスト内の各頂点に回転公式を適用します。この方法により、辺の数に関係なく、すべての多角形をその頂点を順に処理して位置を更新することで変換できます。
12345678910111213141516171819202122232425import math def rotate_polygon(points, angle_radians): """Rotate a polygon around the origin by a specified angle. Args: points: List of (x, y) tuples representing the polygon's vertices. angle_radians: The rotation angle in radians. Returns: List of (x', y') tuples representing the rotated vertices. """ cos_theta = math.cos(angle_radians) sin_theta = math.sin(angle_radians) rotated = [] for x, y in points: x_new = x * cos_theta - y * sin_theta y_new = x * sin_theta + y * cos_theta rotated.append((x_new, y_new)) return rotated # Example usage: triangle = [(1, 0), (0, 1), (-1, 0)] rotated_triangle = rotate_polygon(triangle, math.pi / 2) print(rotated_triangle)
すべて明確でしたか?
フィードバックありがとうございます!
セクション 2. 章 3
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 2. 章 3