沿着一条线绘制圆圈使用递归&分形 - Java

时间:2014-02-06 23:59:06

标签: java recursion geometry draw fractals

我试图沿着一条线绘制下面的圆形分形图案。 它绘制一个半径为n的圆,以及一条不可见线上的中心点。然后它递归地绘制两个半径为n / 2的圆和中心点,其中圆与线交叉。 Fractal Circles 这对我来说非常混乱,因为绘制圆的Java图形代码要求你使用“instance.drawOval(int x1,int x1,int height,int width) - 它没有说明圆心。

我试图捅它,这是我到目前为止所提出的。我只试图绘制第一个圆圈然后两个向右和向左绘制。我还没有完成任何递归。我只想尝试左右两个圆圈,其中心点与最大圆圈的左右碰撞。

 package fractalcircles;

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

 public class FractalCircles {

 /**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{   //create a MyCanvas object
    MyCanvas canvas1 = new MyCanvas();

    //set up a JFrame to hold the canvas
    JFrame frame = new JFrame();
    frame.setTitle("FractalCircles.java");
    frame.setSize(500,500);
    frame.setLocation(100,100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //add the canvas to the frame as a content panel
    frame.getContentPane().add(canvas1);
    frame.setVisible(true);
 }//end main
 }//end class

 class MyCanvas extends Canvas
 {
public MyCanvas()
{} //end MyCanvas() constructor

//this method will draw the initial circle and invisible line
public void paint (Graphics graphics)
{
    int n=50; //radius of first circle

    //draw invisible line
    graphics.drawLine(0,250,500,250);

    //draw first circle
    graphics.drawOval(200,200,n*2,n*2);

    //run fractal algorith to draw 2 circles to the left and right
    drawCircles(graphics, n);
}

public void drawCircles (Graphics graphics, int n)
{
    int x1; int y1; //top left corner of left circle to be drawn
    int x2; int y2; //top left corner of right circle to be drawn

    //drawing left circle
    x1=200-((n/2)*2); 
    //***this math was found using the equation in chapter 11
    //***center point of circle = (x+(width/2), y+(height/2))
    y1=200-((n/2)*2);
    graphics.drawOval(x1, y1, ((n/2)*2), ((n/2)*2));

    //drawing right circle
    x2=300-((n/2)*2);
    y2=300-((n/2)*2);
    graphics.drawOval(x1, y1, ((n/2)*2), ((n/2)*2));
}

任何帮助都会非常感谢大家。

1 个答案:

答案 0 :(得分:3)

我要提出一点建议。如果任务更容易,你宁愿从中心画圆圈,那么让我们按照你想要的方式制作一个函数。

public void circle(Graphics g, int centerX, int centerY, int radius) {
    g.drawOval(centerX-radius, centerY-radius, radius*2+1, radius*2+1) 
}

现在你有一个功能可以从你选择的中心点以你选择的半径制作圆圈。

耶!