你需要计算出一个边长
的长方体
边长根据半径(从中心到顶点的距离)计算
apotem(从一条边的中心到中点的距离)是从边长计算出来的。
(more here)
在下面的示例中,bbox位于创建的多边形的中心;您可以根据需要偏移它以匹配您的首选锚定点。在
给定同一个边界框,所有多边形都被计算成内接在同一个圆内——外接圆是多边形边数趋于无穷大时的极限,见下图。在import tkinter as tk
import math
WIDTH, HEIGHT = 500, 500
class Point:
"""convenience for point arithmetic"""
def __init__(self, x, y):
self.x, self.y = x, y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __iter__(self):
yield self.x
yield self.y
class RegularPolygon:
def __init__(self, num_sides, bbox_side, x, y): # x, y are bbox center canvas coordinates
self.bbox_side = bbox_side
self.num_sides = num_sides
self.side_length = None
self.apothem = None