【问题标题】:How do I create a pentagon and hexagon using create_line in python tkinter canvas?如何在 python tkinter 画布中使用 create_line 创建五边形和六边形?
【发布时间】:2019-04-25 05:37:00
【问题描述】:

这是我使用 create_lines 在 python tkinter 画布中绘制三角形和正方形的代码,我将如何使用 create_lines 创建五边形和六边形?

注意:对于五边形和六边形,长度和宽度是指包含形状的正方形的总面积,而不是边的宽度和长度。

    self.x, self.y = 50, 50

def triangle(self):
    width = self.width.get()
    length = self.length.get()
    color = self.color_select.get()

    self.canvas.create_line(self.x, self.y, (self.x + (int(length) / 2)), (self.y + int(length)), fill = color)

    self.canvas.create_line(self.x, self.y, (self.x - (int(length) / 2)), (self.y + int(length)), fill = color)

    self.canvas.create_line((self.x - (int(length) / 2)), (self.y + int(length)), (self.x + (int(length) / 2)), (self.y + int(length)), fill = color)

    self.x += 50

def square(self):
    width = self.width.get()
    length = self.length.get()
    color = self.color_select.get()

    self.canvas.create_line(self.x, self.y, self.x + int(width), self.y, fill = color)

    self.canvas.create_line(self.x, self.y, self.x, self.y + int(length), fill = color)
    self.y += int(length)

    self.canvas.create_line(self.x, self.y, self.x + int(width), self.y, fill = color)
    self.x += int(width)

    self.canvas.create_line(self.x, self.y, self.x, self.y - int(length), fill = color)
    self.y -= int(length)

    self.x += 50

def pentagon(self):
    width = self.width.get()
    length = self.length.get()
    color = self.color_select.get()

def hexagon(self):
    width = self.width.get()
    length = self.length.get()
    color = self.color_select.get()

【问题讨论】:

  • 您必须使用math.sin()math.cos()math.tan() 来计算一些距离。
  • 添加到 furas 答案,使用create_polygon 并在画布上创建一个实体,而不是用多行填充它

标签: python python-3.x tkinter geometry tkinter-canvas


【解决方案1】:

要从边界框创建正多边形,您需要计算边长apothem
边长由半径(从中心到顶点的距离)计算得出
Apothem(从中心到边中点的距离)是根据边长计算得出的。
(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
        self._calc_side_length()
        self.points = [Point(x - self.side_length // 2, y - self.apothem)]
        self._make_points()
        self.lines = []
        self._make_lines()

    def _calc_side_length(self):
        """Side length given the radius (circumradius):
        i/e the distance from the center to a vertex
        """
        self.side_length = 2 * (self.bbox_side // 2) * math.sin(math.pi / self.num_sides)

        # Apothem, i/e distance from the center of the polygon 
        # to the midpoint of any side, given the side length 
        self.apothem = self.side_length / (2 * math.tan(math.pi / self.num_sides))

    def _make_points(self):
        _angle = 2 * math.pi / self.num_sides
        for pdx in range(self.num_sides):
            angle = _angle * pdx
            _x = math.cos(angle) * self.side_length
            _y = math.sin(angle) * self.side_length
            self.points.append(self.points[-1] + Point(_x, _y))

    def _make_lines(self):
        for p0, p1 in zip(self.points[:-1], self.points[1:]):
            self.lines.append((*p0, *p1))

    def draw(self, canvas):
        for line in self.lines:
            canvas.create_line(line)
        # alternatively, use canvas.create_polygon(points coordinates) instead


root = tk.Tk()

canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="cyan")
canvas.pack()

CENTER = Point(WIDTH // 2, HEIGHT // 2)

for n_sides in range(3, 12):
    p = RegularPolygon(n_sides, 300, *CENTER)
    p.draw(canvas)


root.mainloop()  

从 3 到 12 边(包括)绘制正多边形

给定相同的边界框,所有多边形都被计算为内接在同一个圆中,即外接圆。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2020-09-18
    • 2023-03-21
    • 2021-12-09
    相关资源
    最近更新 更多