gpt4 book ai didi

java - 带点的谢尔宾斯基垫片

转载 作者:行者123 更新时间:2023-12-01 11:17:56 24 4
gpt4 key购买 nike

我在为我的 Java 类(class)分配作业时遇到问题。我需要

Implement a Sierpinski gasket. Do not use triangles or recursion. This is the Chaos version that only uses points.

下面是我已经实现的。我得到的只是绘制的前三个点:x、y 和 z。 while 循环中有一个问题(或多个问题)。任何有关如何解决此问题的建议将不胜感激。谢谢,我被困住了!

SierpinskiGasket.java

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class SierpinskiGasket extends JFrame{

//Class constructor
public SierpinskiGasket(){
Container c = getContentPane();
JPanel jp = new JPanel();
jp.setBackground(Color.white);
c.add(jp);
setTitle("Sierpinski Gasket");
setSize(new Dimension(400,400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void paint(Graphics g){
super.paint(g);
int count = 0,
vert = 0;
Random rndm = new Random();

//Create the three vertices of the triangle
Point x=new Point(200,50),
y=new Point(350,350),
z=new Point(50, 350),
current=x, target = null;


if(count==0){ //Draw the three points of the vertices
g.drawLine(x.x,x.y,x.x,x.y); //Top of the triangle
g.drawLine(y.x,y.y,y.x,y.y); //Right of triangle
g.drawLine(z.x,z.y,z.x,z.y); //Left of triangle

}else{
//The loop uses a random number to chose one of the three vertices of the triangle.
//It then makes that point the target point
while(count<1000){
vert = rndm.nextInt(3);
//Switch statement assigns one of the vertices to the target
switch(vert){
case 0: target=x; break;
case 1: target=y; break;
case 2: target=z; break;
}
//Calculates the mid point between the current and the target
current = midpoint(current, target);
//Draws the point calculates in midpoint()
g.drawLine(current.x, current.y, current.x, current.y);
//Repaint
repaint();
//Increase the count
count++;
}

}
}
/**
* Calculates the midpoint between the two points
* @param c
* @param t
* @return The midpoint
*/
public Point midpoint(Point c, Point t){

return new Point((Math.round((c.x+t.x)/2)),
Math.round(((c.y+t.y)/2)));
}
}

Gasket.java

public class Gasket {

public static void main(String[] args) {

new SierpinskiGasket();
}

}

最佳答案

您的 while 循环根本没有执行。这是您修复和清理的代码(不要过度注释您的代码):

public class SierpinskiGasket extends JFrame {

public static void main(String[] args) {
new SierpinskiGasket();
}

// Class constructor
public SierpinskiGasket() {
Container c = getContentPane();
JPanel jp = new JPanel();
jp.setBackground(Color.white);
c.add(jp);
setTitle("Sierpinski Gasket");
setSize(new Dimension(400, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void paint(Graphics g) {
super.paint(g);
Random rndm = new Random();

Point pt0 = new Point(200, 50);
Point pt1 = new Point(350, 350);
Point pt2 = new Point(50, 350);
Point current = pt0;

g.setColor(Color.RED);

drawPoint(g, pt0);
drawPoint(g, pt1);
drawPoint(g, pt2);

Point[] pts = { pt0,pt1,pt2 };
for (int i = 0 ; i < 10000 ; i++) {
current = midpoint(current, pts[rndm.nextInt(3)]);
drawPoint(g, current);
}
}

private static void drawPoint(Graphics g, Point p) {
g.drawLine(p.x, p.y, p.x, p.y);
}

/**
* Calculates the midpoint between the two points
*
* @param c
* @param t
* @return The midpoint
*/
public Point midpoint(Point c, Point t) {
return new Point((Math.round((c.x + t.x) / 2)),
Math.round(((c.y + t.y) / 2)));
}
}

你的count变量是本地的,所以总是重新初始化为0。我没有保留它,因为它使用了奇怪的递归,而你的循环本身就足够了。最后,paintComponent 永远不应该调用 repaint

除此之外,你的算法是正确的:)

关于java - 带点的谢尔宾斯基垫片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31571489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com