使用嵌套的 for 循环绘制等边三角形?
Draw equilateral triangles using a nested for loop?
我正在尝试弄清楚如何使用嵌套 for 循环来生成以下模式:
到目前为止,我有
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 个三角形)。三角形是连续的,每个三角形的下部 left/right 点构成下一行中三角形的最高点。第一行三角形顶点的 (x, y) 坐标必须是 (300, 50)。如果有人可以提供帮助,请告诉我。
递归方法听起来比迭代更容易,所以它看起来像这样:
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);
}
我正在尝试弄清楚如何使用嵌套 for 循环来生成以下模式:
到目前为止,我有
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 个三角形)。三角形是连续的,每个三角形的下部 left/right 点构成下一行中三角形的最高点。第一行三角形顶点的 (x, y) 坐标必须是 (300, 50)。如果有人可以提供帮助,请告诉我。
递归方法听起来比迭代更容易,所以它看起来像这样:
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);
}