From Gossip@caterpillar

Computer Graphics: 十字繡曲線

 

 


以正方為基礎的十字繡,其1次與2次曲線如下所示:


以下為四個十字繡曲線所圍成的小方巾圖案程式:

  • StachCurve.java
package onlyfun.caterpillar.graphics.recursive;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
import onlyfun.caterpillar.graphics.Turtle;

public class StechCurve extends JApplet {
private Turtle t;

public void init() {
t = new Turtle();

setBackground(Color.black);
}

public void stech(int n, double leng, Turtle t) {
if(n == 0) {
t.move(leng);
}
else {
stech(n-1, leng, t); t.turn(-90);
stech(n-1, leng, t); t.turn(90);
stech(n-1, leng, t); t.turn(90);
stech(n-1, leng, t); t.turn(-90);
stech(n-1, leng, t);
}
}

public void paint(Graphics g) {
g.setColor(Color.yellow);

t.setGraphics(g);
t.window(0, 0, getSize().width, getSize().height);
t.view(0, 0, getSize().width, getSize().height);
t.setpoint(100, 150);
t.setangle(0);

for(int i = 1; i <= 4; i++) {
stech(4, 2, t);
t.turn(90);
}
}
}