各位大大晚安
我收到一個"非常美麗"的任務
就是寫一個JAVA畫出至少19迴圈的LAVY C CURVE輸出所有的點座標用EXCEL畫圖
因為毫無方向..因此來求救
但是我怎麼"孤溝"都找不到有JAVA的
LAVY C CURVE找到的都是萊維C形曲線/ LEVY C CURVE
是有看到用JAVA在EXCEL導出長條圖
但是~萊維C形曲線..沒有
求大大指引方向
你跟我的 Google 有點不一樣
以下提供參考
算是二分之一的答案
// 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;
import java.util.concurrent.ThreadLocalRandom;
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.add(points);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
回到問題本身
我只知道可以用 Excel 畫特定的 Chart 類型
沒法子畫 free format 的 Graph
硬要試的話
可以用 Scatter Chart(沒有線的那種)硬湊看看
g.drawLine((int) fx, (int) fy, (int) (fx + (length * Math.cos(Math.toRadians(alpha)))), (int) (fy + (length * Math.sin(Math.toRadians(alpha)))));
這段應該就是你要的 from(x,y) 到 to(x,y) 的座標
試試看
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();
}
}
非常感謝大大的加碼回答