圆形包装 - java

发布于 2024-07-25 05:51:40 字数 1463 浏览 7 评论 0原文

我的任务是画一个圆圈,然后用最多数量的圆圈填充而不接触侧面。 我可以画圆,也可以制作循环以六边形/蜂窝格式包装圆,但无法控制它们是在圆内还是在圆外。

我用过这个:g.drawOval(50, 50, 300, 300); 指定我的圈子。 鉴于我实际上指定了一个正方形作为边界,我实际上无法确定圆形边界在哪里。 所以我基本上是把正方形装满圆圈,而不是把圆圈装满圆圈。

有人可以指出我正确的方向吗? 我是java新手,所以不确定我是否以完全错误的方式完成了这件事。 我的代码如下。 我有另一个框架类和另一个包含主类的类。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class DrawCircle extends JPanel
{
    private int width, height, diameter;
    public DrawFrame d;

    public DrawCircle()
    {
        width = 400;
        height = 400;
        diameter = 300;
    }


    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.blue);
        g.drawOval(50, 50, 300, 300);

        for(int i=50; i<200; i=i+20)
        {
            for(int j=50; j<350; j=j+10)
            {
                g.drawOval(j, i, 10, 10);
            }
        }

        for(int i=60; i<200; i=i+20)
        {
            for(int j=55; j<350; j=j+10)
            {
                g.drawOval(j, i, 10, 10);
            }
        }

        for(int i=330; i>190; i=i-20)
        {
            for(int j=340; j>40; j=j-10)
            {
                g.drawOval(j, i, 10, 10);
            }
        }

        for(int i=340; i>190; i=i-20)
        {
            for(int j=345; j>40; j=j-10)
            {
                g.drawOval(j, i, 10, 10);
            }
        }




    }
}

I have a task to draw a circle and then fill in with the most amount of circles without touching the sides. I can draw the circle, and I can make loops to pack the circle in a hexagonal/honeycomb format, but can't control whether they are inside or outside the circle.

I have used this: g.drawOval(50, 50, 300, 300); to specify my circle. Given I'm actually specifying a square as my boundaries I can't actually determine where the circle boundaries are. So I'm basically packing the square full of circles rather than the circle full of circles.

Can some please point me in the right direction? I'm new to java so not sure if I have done this the complete wrong way. My code is below. I have another class for the frame and another with the main in it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class DrawCircle extends JPanel
{
    private int width, height, diameter;
    public DrawFrame d;

    public DrawCircle()
    {
        width = 400;
        height = 400;
        diameter = 300;
    }


    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.blue);
        g.drawOval(50, 50, 300, 300);

        for(int i=50; i<200; i=i+20)
        {
            for(int j=50; j<350; j=j+10)
            {
                g.drawOval(j, i, 10, 10);
            }
        }

        for(int i=60; i<200; i=i+20)
        {
            for(int j=55; j<350; j=j+10)
            {
                g.drawOval(j, i, 10, 10);
            }
        }

        for(int i=330; i>190; i=i-20)
        {
            for(int j=340; j>40; j=j-10)
            {
                g.drawOval(j, i, 10, 10);
            }
        }

        for(int i=340; i>190; i=i-20)
        {
            for(int j=345; j>40; j=j-10)
            {
                g.drawOval(j, i, 10, 10);
            }
        }




    }
}

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

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

发布评论

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

评论(1

小鸟爱天空丶 2024-08-01 05:51:40

所有这些神奇的数字让我有点畏缩。 你是 Java 新手,这是家庭作业,所以我理解你为什么这样做,但如果你将来进行大量编程,我不会推荐它。

您需要一种算法或方法来确定内部的小圆圈何时落在您要打包的大圆圈之外。 考虑一下可以采取的方法:

  1. 如果大圆和小圆的中心之间的距离大于它们的半径差,则小圆将与大圆重叠或完全落在大圆之外。

您可以将此检查添加到您的代码中:在绘制圆圈之前,执行此检查。 仅当该圆圈通过时才绘制。

暂时不用担心 Java; 在一张纸上给自己画一幅画,画出封闭的圆圈,看看这个说法是否正确。 然后考虑一下它可能无法涵盖的任何角落情况,就像检查一样。

我会再提出两个建议。 首先,在没有计算机的情况下手动执行一次,这样您就会看到“正确”答案可能是什么样子。 其次,看看是否可以将圆的计算与绘图部分分开。 这可能会让您的工作变得更轻松,因为您可以一次专注于一件事。 这就是所谓的“分解”。 您可以通过将复杂的问题分解为更小、更易于管理的部分来解决它们。 在这种情况下,它也称为“模型视图分离”。 有一天你可能需要知道这一点。

也许思考这个问题的另一种方法是想象一个二维的圆排列,以最接近的排列方式排列,在 x 和 y 方向上延伸到无穷大。 现在,将您的封闭圆放在 2D 排列的顶部,并消除与大圆重叠的所有圆。 我不知道它是否是最佳的,但很容易想象。

All those magic numbers make me cringe a bit. You're new to Java, and it's homework, so I understand why you're doing it, but I would not recommend it if you do much programming in the future.

You need an algorithm or recipe for deciding when a small circle on the inside falls outside the big one you're trying to pack. Think about the ways you might do this:

  1. If the distance between the center of the big circle and the small circle is is greater than the difference in their radii, the small circle will overlap the big circle or fall completely outside it.

You can add this check to your code: Just before you draw the circle, perform this check. Only draw if that circle passes.

Don't worry about Java for a second; draw yourself a picture on a piece of paper, draw that enclosing and packed circle, and see if that statement is correct. Then think about any corner situations that it might not cover, just as a check.

I'll make two more recommendations. First, do this by hand without a computer once so you'll see what the "right" answer might look like. Second, see if you can separate the calculation of the circles from the drawing part. It might make your job easier, because you can concentrate on one thing at a time. It's called "decomposition". You solve complex problems by breaking them up into smaller, more manageable pieces. In this case, it's also called "model-view separation". You might need to know that someday.

Maybe another way to think about this problem would be to imagine a 2D arrangement of circles, packed in their closest arrangement, extending to infinity in both the x- and y-directions. Now take your enclosing circle, put it on top of the 2D arrangement, and eliminate all the circles that overlap the big circle. I don't know if it'll be optimal, but it's easy to visualize.

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