iT邦幫忙

0

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

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

各位大大晚安
我收到一個"非常美麗"的任務
就是寫一個JAVA畫出至少19迴圈的LAVY C CURVE輸出所有的點座標用EXCEL畫圖

因為毫無方向..因此來求救/images/emoticon/emoticon46.gif

但是我怎麼"孤溝"都找不到有JAVA的/images/emoticon/emoticon44.gif

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

是有看到用JAVA在EXCEL導出長條圖
但是~萊維C形曲線..沒有

求大大指引方向

/images/emoticon/emoticon47.gif

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2020-12-10 07:21:05
最佳解答

你跟我的 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(沒有線的那種)硬湊看看

看更多先前的回應...收起先前的回應...
Tzu iT邦新手 4 級 ‧ 2020-12-10 21:44:53 檢舉

https://ithelp.ithome.com.tw/upload/images/20201210/20119035VIydBZ3fwW.png

謝謝大大~
但是....我現在卡在不知道怎麼輸出所有的點座標

然後再用EXCEL畫圖

/images/emoticon/emoticon02.gif

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)))));

這段應該就是你要的 from(x,y) 到 to(x,y) 的座標

https://ithelp.ithome.com.tw/upload/images/20201211/20001787FnC4efIsgD.png
試試看

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 檢舉

非常感謝大大的加碼回答/images/emoticon/emoticon24.gif

我要發表回答

立即登入回答