turtle图形

设置画布尺寸为1600*800,在其中填充随机圆形(半径为15至40,颜色随意),要求所有的圆形之间不重叠,并统计最多可以填充多少个。

//要自行下载turtle包哦
import turtle
import random

t = turtle.Pen()
# 速度最快
t.speed(0)
# 圆的颜色
colors = ["red", "blue", "yellow", "orange", "green", "gold", "purple", "pink"]
# 设置画圆的版面
w = 1600
h = 800
turtle.screensize(w, h, bg="black")


# turtle.setup(w+10,h+10)
# 确定画圆的起笔
def move(pos=(0, 0)):
    t.penup()
    t.goto(pos)
    t.pendown()


# 画圆函数
def func1(pos=(0, 0), colorturtle="red", rturtle=30):
    move(pos)
    t.color(colorturtle)
    t.begin_fill()
    t.circle(rturtle)
    t.end_fill()


# 用于计算两个圆心之间的距离:(((x1-x2)^2+(y1-y2)^2))^(1/2)
def distance(p1, p2):
    d = ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5
    return d


circles = []
# 遍历判断是否可以在该院新下画圆


def measure(pos, rturtle):
    for c in circles:
        if distance(c[0], pos) < c[1] + rturtle:
            return False
    return True


cnt = 0
cnt1 = 0
# turtle.begin_fill()
for i in range(200000):
    color = random.choice(colors)
    r = random.randint(15, 40)
    x = random.randint(-w // 2, w // 2)
    y = random.randint(-h // 2, h // 2)
    # 第一个if用于把整个圆控制在所设置屏幕范围内;第二个if遍历判断是否放得下该圆;
    if x - r >= -(w / 2) and x + r <= (w / 2) and y - r >= -(h / 2) and y + r <= (h / 2):
        if measure((x, y), r):
            # cnt用于计数所方的圆个数;
            cnt += 1
            # 把圆的圆心坐标和半径放入链表circles中;func1函数用于画圆
            circles.append([(x, y), r])
            func1(pos=(x, -r + y), colorturtle=color, rturtle=r)
        # else用于计数不满足画圆条件的次数
        else:
            cnt1 += 1
            if cnt1 == 1000000:
                break
# 输出所设置的屏幕能画的圆的个数
print(cnt)
turtle.done()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来回答您的问题。 以下是实现 turtle 图形 API 的 Java 类,并编写了一个递归方法来绘制希尔伯特曲线的代码示例: ```java import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class Turtle { private double x, y; // turtle position private double angle; // turtle orientation public Turtle(double x, double y, double angle) { this.x = x; this.y = y; this.angle = angle; } public void forward(double dist) { double newx = x + dist * Math.cos(Math.toRadians(angle)); double newy = y + dist * Math.sin(Math.toRadians(angle)); drawLine(x, y, newx, newy); x = newx; y = newy; } public void turnLeft(double delta) { angle += delta; } public void turnRight(double delta) { angle -= delta; } // Method to draw a line using Java's graphics library private void drawLine(double x1, double y1, double x2, double y2) { JFrame frame = new JFrame("Turtle Graphics"); JPanel panel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); g.drawLine((int)x1, (int)y1, (int)x2, (int)y2); } }; frame.add(panel); frame.setSize(500, 500); frame.setVisible(true); } // Method to recursively draw a Hilbert curve public void hilbertCurve(int level, double step, int direction) { if (level == 0) { return; } turnLeft(direction * 90); hilbertCurve(level-1, step, -direction); forward(step); turnRight(direction * 90); hilbertCurve(level-1, step, direction); forward(step); hilbertCurve(level-1, step, direction); turnRight(direction * 90); forward(step); hilbertCurve(level-1, step, -direction); turnLeft(direction * 90); } // Main method to create a Turtle object and draw a Hilbert curve public static void main(String[] args) { Turtle turtle = new Turtle(250, 250, 0); turtle.hilbertCurve(6, 5, 1); } } ``` 在这个示例中,我们创建了一个名为 Turtle 的类来实现 turtle 图形 API,并演示如何使用递归方法绘制希尔伯特曲线。在 hilbertCurve() 方法中,我们使用了四个递归调用来绘制一个更高级别的希尔伯特曲线,直到达到最低级别为止。为了绘制希尔伯特曲线,我们使用了 turnLeft()、turnRight() 和 forward() 方法来旋转和移动 turtle,从而绘制出曲线的各个部分。 在 main() 方法中,我们创建了一个 Turtle 对象,并调用 hilbertCurve() 方法来绘制希尔伯特曲线。通过调整 hilbertCurve() 方法中的参数,可以生成不同级别的希尔伯特曲线。 希望这个代码示例能够帮助您了解如何使用 Java 实现 turtle 图形 API 和递归方法绘制希尔伯特曲线。如果您还有其他问题,请随时提出,我会尽力回答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值