使用嵌套for循环绘制等边三角形?

时间:2018-06-16 08:26:01

标签: java object java.util.scanner draw nested-loops

我试图弄清楚如何使用嵌套的for循环来生成以下模式:

Equilateral Triangle Pattern

到目前为止,我有

public class Triangle {

public static final int SIZE = 600;
public static final int INITIAL_X = 300;
public static final int INITIAL_Y = 50;
public static final int SIDE = 100;

/**
 * 
 * 
 * @param args command line arguments
 */
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    DrawingPanel panel = new DrawingPanel(SIZE, SIZE);
    panel.setBackground(Color.WHITE);
    System.out.print("Enter number of rows (1-5): " );
    int row = console.nextInt();
    if(row < 1) {
        row = 1;
    } else if(row > 5) {
        row = 5;
    }
    System.out.print("Specify red value (0-255): ");
    int red = console.nextInt();
    if(red < 0) {
        red = 0;
    } else if(red > 255) {
        red = 255;
    System.out.print("Specify green value (0-255): ");
    int green = console.nextInt();
    if(green < 0) {
        green = 0;
    } else if(green > 255) {
        green = 255;
    }
    System.out.print("Specify blue value (0-255): ");
    int blue = console.nextInt();
    if(blue < 0) {
        blue = 0;
    } else if(blue > 255) {
        blue = 255;
    }
    Graphics g = panel.getGraphics(); //Initializes graphics panel.
    g.setColor(new Color(red, green, blue));

    for (int i = 1; i <= row; i++) {
        for (int x = 1; x <= row; x++) {
            int lx = sx - SIDE/2;
            int rx = sx + SIDE/2;
            int ly = (int) (sy + SIDE*Math.sqrt(3)/2);
            int ry = ly;


    System.out.println("\n*CLOSE the Drawing Panel to exit the program*");
}       

/**
 * Draws an equilateral triangle with topmost point at (x, y) with the
 * given side length and color.
 * 
 * @param g
 * @param color
 * @param x
 * @param y
 * @param sideLength
 */

public static void drawTriangle(Graphics g, int sx, int sy, int rows) {
    g.drawLine(sx, sy, lx, ly);
    g.drawLine(sx, sy, rx, ry);
    g.drawLine(lx, ly, rx, ry);
}

此代码绝对没有完成。我只想使用java.awt。*和java.util。*包来解决这个问题。我的麻烦主要是在main方法中创建一个嵌套的for循环,但我相信我已经掌握了这个赋值的扫描器部分。我使用了从毕达哥拉斯定理得到的下面的等边三角形公式。每行应该有那么多三角形(例如,第5行应该有5个三角形)。三角形是连续的,并且每个三角形的左下/右下点形成下面行中三角形的最高点。第一行中三角形最顶点的(x,y)坐标必须为(300,50)。如果有人可以帮忙,请告诉我。

Equilateral Triangle formulas used to created pattern.

1 个答案:

答案 0 :(得分:1)

递归方法听起来比迭代更容易,所以看起来像这样:

private void drawTree(double x, double y, int depth, int maxDepth, Graphics graphics, double sideLength) {
    if (depth >= maxDepth) {
        return;
    }

    double leftX = x - sideLength / 2;
    double leftY = y + Math.sqrt(sideLength * 3) / 2;
    double rightX = x + sideLength / 2;
    double rightY = leftY;

    //draw line from (x,y) -> (leftX, leftY)
    graphics.drawLine(x, y, leftX, leftY);
    //draw line from (x,y) -> (rightX, rightY)
    graphics.drawLine(x, y, rightX, rightY);
    //draw line from (leftX, leftY) -> (rightX, rightY)
    graphics.drawLine(leftX, leftX, rightX, rightY);

    drawTree(leftX, leftY, depth + 1, maxDepth, graphics, sideLength);
    drawTree(rightX, rightY, depth + 1, maxDepth, graphics, sideLength);
}