如何创建多个50便士硬币

发布于 2025-01-27 17:50:38 字数 567 浏览 2 评论 0原文

如何在不重叠或不互动的页面中创建单独的硬币。下面的代码显示了重叠的硬币,如果数字大于2。

import turtle

tegan = turtle.Turtle()

turtle.fillcolor('grey')

turtle.begin_fill()


numbers = int(input("How many 50 pence coins you have: "))
print(numbers)

length = 100  
degrees = 51.42857
angle = 40

def draw_heptagon(tegan, length, numbers, angle):

    for i in range(numbers):
        for x in range(7):
            turtle.forward(length)
            turtle.left(degrees)
        turtle.right(angle)

draw_heptagon(tegan, length, numbers, angle)

turtle.end_fill()
turtle.done()

How can I create separate coins in a page that are not overlapping or not touching each other. The code below shows overlapping coins if the number is greater than 2.

import turtle

tegan = turtle.Turtle()

turtle.fillcolor('grey')

turtle.begin_fill()


numbers = int(input("How many 50 pence coins you have: "))
print(numbers)

length = 100  
degrees = 51.42857
angle = 40

def draw_heptagon(tegan, length, numbers, angle):

    for i in range(numbers):
        for x in range(7):
            turtle.forward(length)
            turtle.left(degrees)
        turtle.right(angle)

draw_heptagon(tegan, length, numbers, angle)

turtle.end_fill()
turtle.done()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉目亦如画i 2025-02-03 17:50:38

这是您可以完善的大概方法。我们将跟踪我们绘制的随机放置的七琴的中心。我们将它们视为圆圈,以绘制和检查它们之间的距离:

from turtle import Screen, Turtle
from random import randint

RADIUS = 100
ANGLE = 40

number = int(input("How many 50 pence coins you have: "))

def draw_heptagons(t, number):
    coins = []

    for _ in range(number):
        while True:
            x = randint(RADIUS - width/2, width/2 - RADIUS)
            y = randint(RADIUS - height/2, height/2 - RADIUS)

            t.goto(x, y)
            t.right(ANGLE)

            if coins and any(t.distance(position) < RADIUS * 2 for position in coins):
                continue

            break

        t.right(90)
        t.forward(RADIUS)  # draw heptagon centered at x, y
        t.left(90)

        turtle.begin_fill()
        t.circle(RADIUS, steps=7)
        turtle.end_fill()

        coins.append((x, y))

screen = Screen()
width, height = screen.window_width(), screen.window_height()

turtle = Turtle()
turtle.speed('fastest')  # because I have no patience
turtle.penup()

draw_heptagons(turtle, number)

turtle.hideturtle()
screen.exitonclick()

这做了您所描述的,但是七个人实际上可能比圆圈更接近(窗口中更多),因此您需要修改测试,以及您在位置中存储的哪些信息,如果要更紧:

< img src =“ https://i.sstatic.net/tkmgj.png” alt =“在此处输入图像说明”>

Here's an approximate approach that you can refine. We'll keep track of the centers of the randomly placed heptagons we draw. We'll treat them as circles for purposes of drawing and checking the distances between them:

from turtle import Screen, Turtle
from random import randint

RADIUS = 100
ANGLE = 40

number = int(input("How many 50 pence coins you have: "))

def draw_heptagons(t, number):
    coins = []

    for _ in range(number):
        while True:
            x = randint(RADIUS - width/2, width/2 - RADIUS)
            y = randint(RADIUS - height/2, height/2 - RADIUS)

            t.goto(x, y)
            t.right(ANGLE)

            if coins and any(t.distance(position) < RADIUS * 2 for position in coins):
                continue

            break

        t.right(90)
        t.forward(RADIUS)  # draw heptagon centered at x, y
        t.left(90)

        turtle.begin_fill()
        t.circle(RADIUS, steps=7)
        turtle.end_fill()

        coins.append((x, y))

screen = Screen()
width, height = screen.window_width(), screen.window_height()

turtle = Turtle()
turtle.speed('fastest')  # because I have no patience
turtle.penup()

draw_heptagons(turtle, number)

turtle.hideturtle()
screen.exitonclick()

This does what you describe but the heptagons could actually be closer (fit more of them in the window) than circles, so you need to modify the test, and what information you store in positions, if you want to pack them tighter:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文