python create line,如何在python tkinter画布中使用create_line创建五边形和六边形?

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)

4f4258e966ef1202d274f998094c52bf.png

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

e67c5a43e4a128cfa4618eded4b376bc.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值