第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何盡可能準(zhǔn)確地繪制蝴蝶曲線?

如何盡可能準(zhǔn)確地繪制蝴蝶曲線?

慕桂英3389331 2021-12-18 15:12:45
我想畫一個(gè)蝶形線使用Java。這是上述曲線的參數(shù)方程:根據(jù)我在大學(xué)時(shí)的記憶,繪制參數(shù)方程的方法Java是下一個(gè):public void paintComponent(Graphics g) {    super.paintComponent(g);    Graphics2D g2 = (Graphics2D)g;    g2.translate(300,300);    int x1,y1;    int x0 = 0;    int y0 = (int)(Math.E-2); //for x = 0, we get y = Math.E - 2    int nPoints = 1000;    g2.scale(30,-30);    for(int i=0;i<nPoints;i++) {        double t= 12*i*Math.PI/nPoints; //to make it between 0 and 12*PI.        x1=(int)(Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));        y1 = (int)(Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));        g2.drawLine(x0,y0,x1,y1);        x0=x1;        y0=y1;    }}現(xiàn)在,這給了我下一個(gè)結(jié)果:好吧,這與預(yù)期的結(jié)果相差甚遠(yuǎn)。然后我決定嘗試一下,Line2D.Double認(rèn)為這會(huì)提供更準(zhǔn)確的繪圖。public void paintComponent(Graphics g) {    super.paintComponent(g);    Graphics2D g2 = (Graphics2D)g;    g2.translate(300,300);    double x1,y1;    double x0 = 0;    int nPoints = 500;    g2.scale(30,-30);    double y0 = Math.E-2;    for(int i=0;i<nPoints;i++) {        double t= 12*i*Math.PI/nPoints;        x1=(Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));        y1 = (Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));        g2.draw(new Line2D.Double(x0,y0,x1,y1));        x0=x1;        y0=y1;    }}這產(chǎn)生了下一個(gè)結(jié)果:好的,這肯定看起來更好,但肯定不是預(yù)期的結(jié)果。因此我問,有沒有辦法使用這個(gè)參數(shù)方程繪制最準(zhǔn)確的曲線Java?它不必像上圖那樣 100% 看起來,而是最接近的。
查看完整描述

1 回答

?
鴻蒙傳說

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

你的 scale 語句也會(huì)縮放你的線的寬度,導(dǎo)致你的曲線形狀奇怪。有兩種簡單的方法可以解決這個(gè)問題:


減少線的寬度,例如到 0.01f:


Graphics2D g2 = (Graphics2D)g;

g2.translate(300,300);

double x1,y1;

double x0 = 0;

int nPoints = 500;

// Alternative 1 ---------------------

g2.scale(30,-30);

g2.setStroke(new BasicStroke(0.01f ));

// -----------------------------------

double y0 = Math.E-2;

for(int i=0;i<nPoints;i++) {

    double t= 12*i*Math.PI/nPoints;

    x1= (Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));

    y1 = (Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));

    g2.draw(new Line2D.Double(x0,y0,x1,y1));

    x0=x1;

    y0=y1;

}  

這導(dǎo)致:

http://img1.sycdn.imooc.com//61bd8a440001666f02090190.jpg

刪除您的比例聲明并使用其幅度縮放曲線,即使用關(guān)于您的 x 和 y 值的恒定前置因子,例如 -30:


Graphics2D g2 = (Graphics2D)g;

g2.translate(300,300);

double x1,y1;

double x0 = 0;

int nPoints = 500;

// Alternative 2 ---------------------

double amp = -30.0;

// -----------------------------------

double y0 = Math.E-2;

for(int i=0;i<nPoints;i++) {

    double t= 12*i*Math.PI/nPoints;

    // Alternative 2 ----------------------------------------------------------------------------------

    x1=amp*(Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));

    y1=amp*(Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));

    // ------------------------------------------------------------------------------------------------

    g2.draw(new Line2D.Double(x0,y0,x1,y1));

    x0=x1;

    y0=y1;

}  

這導(dǎo)致(或多或少相同):

http://img1.sycdn.imooc.com//61bd8a500001edb202090187.jpg

此外,您可以通過使用抗鋸齒和增加 nPoints 來提高繪圖的質(zhì)量:


    Graphics2D g2 = (Graphics2D)g;

    // Optimization ------------------------------------

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

            RenderingHints.VALUE_ANTIALIAS_ON);

    int nPoints = 1500;

    // -------------------------------------------------

    g2.translate(300,300);

    double x1,y1;

    double x0 = 0;

    // Alternative 1 ---------------------

    g2.scale(50,-50);

    g2.setStroke(new BasicStroke(0.01f ));

    // -----------------------------------

    double y0 = Math.E-2;

    for(int i=0;i<nPoints;i++) {

        double t= 12*i*Math.PI/nPoints;

        x1= (Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));

        y1 = (Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));

        g2.draw(new Line2D.Double(x0,y0,x1,y1));

        x0=x1;

        y0=y1;

    }  

這導(dǎo)致(看起來好多了):

http://img1.sycdn.imooc.com//61bd8a5f000150ed03370294.jpg

到目前為止,兩點(diǎn)之間的連接是一條直線。當(dāng)然,您可以使用樣條曲線(貝塞爾曲線等)進(jìn)行進(jìn)一步優(yōu)化,但這可能并非易事。


查看完整回答
反對 回復(fù) 2021-12-18
  • 1 回答
  • 0 關(guān)注
  • 323 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號