<tfoot id='5rWx1'></tfoot>

    <small id='5rWx1'></small><noframes id='5rWx1'>

  1. <legend id='5rWx1'><style id='5rWx1'><dir id='5rWx1'><q id='5rWx1'></q></dir></style></legend>
  2. <i id='5rWx1'><tr id='5rWx1'><dt id='5rWx1'><q id='5rWx1'><span id='5rWx1'><b id='5rWx1'><form id='5rWx1'><ins id='5rWx1'></ins><ul id='5rWx1'></ul><sub id='5rWx1'></sub></form><legend id='5rWx1'></legend><bdo id='5rWx1'><pre id='5rWx1'><center id='5rWx1'></center></pre></bdo></b><th id='5rWx1'></th></span></q></dt></tr></i><div id='5rWx1'><tfoot id='5rWx1'></tfoot><dl id='5rWx1'><fieldset id='5rWx1'></fieldset></dl></div>

      • <bdo id='5rWx1'></bdo><ul id='5rWx1'></ul>

      如何在 python tkinter 画布中使用 create_line 创建五边

      时间:2023-09-11
        • <bdo id='0DFtR'></bdo><ul id='0DFtR'></ul>

            <i id='0DFtR'><tr id='0DFtR'><dt id='0DFtR'><q id='0DFtR'><span id='0DFtR'><b id='0DFtR'><form id='0DFtR'><ins id='0DFtR'></ins><ul id='0DFtR'></ul><sub id='0DFtR'></sub></form><legend id='0DFtR'></legend><bdo id='0DFtR'><pre id='0DFtR'><center id='0DFtR'></center></pre></bdo></b><th id='0DFtR'></th></span></q></dt></tr></i><div id='0DFtR'><tfoot id='0DFtR'></tfoot><dl id='0DFtR'><fieldset id='0DFtR'></fieldset></dl></div>

            <legend id='0DFtR'><style id='0DFtR'><dir id='0DFtR'><q id='0DFtR'></q></dir></style></legend>
          • <small id='0DFtR'></small><noframes id='0DFtR'>

            <tfoot id='0DFtR'></tfoot>

                  <tbody id='0DFtR'></tbody>
                本文介绍了如何在 python tkinter 画布中使用 create_line 创建五边形和六边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

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

                 self.x, self.y = 50, 50定义三角形(自我):宽度 = self.width.get()长度 = self.length.get()颜色 = 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(长度)),填充 = 颜色)自我.x += 50def 平方(自我):宽度 = self.width.get()长度 = self.length.get()颜色 = 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(长度)self.canvas.create_line(self.x, self.y, self.x + int(width), self.y, fill = color)self.x += int(宽度)self.canvas.create_line(self.x, self.y, self.x, self.y - int(length), fill = color)self.y -= int(长度)自我.x += 50def 五边形(自我):宽度 = self.width.get()长度 = self.length.get()颜色 = self.color_select.get()def 六边形(自我):宽度 = self.width.get()长度 = self.length.get()颜色 = self.color_select.get()

                解决方案

                要从边界框创建正多边形,需要计算边长apothem强>.
                边长是根据半径(从中心到顶点的距离)计算得出的
                Apothem(从中心到边中点的距离)是根据边长计算得出的.
                (

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

                Here is the code I have for drawing a triangle and square in python tkinter canvas using create_lines, how would I use create_lines to create a pentagon and hexagon?

                Note: For the pentagon and hexagon, the length and width refer to the total square shaped area the shape is contained within, not the width and length of the sides.

                    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()
                

                解决方案

                To create a regular polygon from a bounding box, you need to calculate the side length, and the apothem.
                The side length is calculated from the radius (the distance from the center to a vertex)
                The Apothem (the distance from the center to the mid-point of a side), is calculated from the side length.
                (more here)

                in the following example, the bbox is centered on the center of the polygon created; you can offset it as you please to match your preferred anchoring point.

                Given the same bounding box, all polygons are calculated to be inscribed in the same circle - the circumcircle, which is the limit of a polygon when the number of sides tends to infinity, see image below.

                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()  
                

                Drawing regular polygons from 3 to 12 sides (incl)

                Given the same bounding box, all polygons are calculated to be inscribed in the same circle, the circumcircle.

                这篇关于如何在 python tkinter 画布中使用 create_line 创建五边形和六边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在python中生成一个随机定向的高维圆? 下一篇:如何找到平面上点投影的坐标

                相关文章

                最新文章

                1. <tfoot id='ys52q'></tfoot>

                  <small id='ys52q'></small><noframes id='ys52q'>

                  <legend id='ys52q'><style id='ys52q'><dir id='ys52q'><q id='ys52q'></q></dir></style></legend>

                    • <bdo id='ys52q'></bdo><ul id='ys52q'></ul>
                    <i id='ys52q'><tr id='ys52q'><dt id='ys52q'><q id='ys52q'><span id='ys52q'><b id='ys52q'><form id='ys52q'><ins id='ys52q'></ins><ul id='ys52q'></ul><sub id='ys52q'></sub></form><legend id='ys52q'></legend><bdo id='ys52q'><pre id='ys52q'><center id='ys52q'></center></pre></bdo></b><th id='ys52q'></th></span></q></dt></tr></i><div id='ys52q'><tfoot id='ys52q'></tfoot><dl id='ys52q'><fieldset id='ys52q'></fieldset></dl></div>