0

## 寫一個JAVA畫出至少19迴圈的LAVY C CURVE輸出所有的點座標用EXCEL畫圖

Tzu 2020-12-09 23:02:04847 瀏覽

LAVY C CURVE找到的都是萊維C形曲線/ LEVY C CURVE

### 1 個回答

1

iT邦大神 1 級 ‧ 2020-12-10 07:21:05

``````// Java Sample Implementation of Levy C Curve

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class C_curve extends JPanel {

public float x, y, len, alpha_angle;
public int iteration_n;

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
c_curve(x, y, len, alpha_angle, iteration_n, g2d);
}

public void c_curve(double x, double y, double len, double alpha_angle, int iteration_n, Graphics2D g) {
double fx = x;
double fy = y;
double length = len;
double alpha = alpha_angle;
int it_n = iteration_n;
if (it_n > 0) {
length = (length / Math.sqrt(2));
c_curve(fx, fy, length, (alpha + 45), (it_n - 1), g); // Recursive Call
fx = (fx + (length * Math.cos(Math.toRadians(alpha + 45))));
fy = (fy + (length * Math.sin(Math.toRadians(alpha + 45))));
c_curve(fx, fy, length, (alpha - 45), (it_n - 1), g); // Recursive Call
} else {
Color[] A = {Color.RED, Color.ORANGE, Color.BLUE, Color.DARK_GRAY};
g.setColor(A[ThreadLocalRandom.current().nextInt(0, A.length)]); //For Choosing Different Color Values
g.drawLine((int) fx, (int) fy, (int) (fx + (length * Math.cos(Math.toRadians(alpha)))), (int) (fy + (length * Math.sin(Math.toRadians(alpha)))));
}
}

public static void main(String[] args) {
C_curve points = new C_curve();
points.x = 200; // Stating x value
points.y = 100; // Stating y value
points.len = 150; // Stating length value
points.alpha_angle = 90; // Stating angle value
points.iteration_n = 15; // Stating iteration value

JFrame frame = new JFrame("Points");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}
}
``````

Tzu iT邦新手 4 級 ‧ 2020-12-10 21:44:53 檢舉

Tzu iT邦新手 4 級 ‧ 2020-12-11 14:17:05 檢舉

``````g.drawLine((int) fx, (int) fy, (int) (fx + (length * Math.cos(Math.toRadians(alpha)))), (int) (fy + (length * Math.sin(Math.toRadians(alpha)))));
``````

``````import java.util.concurrent.ThreadLocalRandom;
public class C_curve {
public float x, y, len, alpha_angle;
public int iteration_n;

public void paint() {
c_curve(x, y, len, alpha_angle, iteration_n);
}
public void c_curve(double x, double y, double len, double alpha_angle, int iteration_n) {
double fx = x;
double fy = y;
double length = len;
double alpha = alpha_angle;
int it_n = iteration_n;
if (it_n > 0) {
length = (length / Math.sqrt(2));
c_curve(fx, fy, length, (alpha + 45), (it_n - 1)); // Recursive Call
fx = (fx + (length * Math.cos(Math.toRadians(alpha + 45))));
fy = (fy + (length * Math.sin(Math.toRadians(alpha + 45))));
c_curve(fx, fy, length, (alpha - 45), (it_n - 1)); // Recursive Call
} else {
System.out.println("From ("+(int)fx+","+(int)fy+") to ("+(int) (fx + (length * Math.cos(Math.toRadians(alpha))))+","+(int) (fy + (length * Math.sin(Math.toRadians(alpha))))+")");
}
}
public static void main(String[] args) {
C_curve points = new C_curve();
points.x = 200; // Stating x value
points.y = 100; // Stating y value
points.len = 150; // Stating length value
points.alpha_angle = 90; // Stating angle value
points.iteration_n = 3;//15; // Stating iteration value
points.paint();
}
}
``````
Tzu iT邦新手 4 級 ‧ 2020-12-12 18:08:48 檢舉