溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java分形怎么繪制山脈模型

發(fā)布時(shí)間:2022-01-10 16:08:02 來源:億速云 閱讀:97 作者:柒染 欄目:開發(fā)技術(shù)

這篇文章主要為大家分析了Java分形怎么繪制山脈模型的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì)易懂,操作細(xì)節(jié)合理,具有一定參考價(jià)值。如果感興趣的話,不妨跟著跟隨小編一起來看看,下面跟著小編一起深入學(xué)習(xí)“Java分形怎么繪制山脈模型”的知識(shí)吧。

如何繪制一個(gè)山脈

構(gòu)思設(shè)計(jì)

任意選取三個(gè)點(diǎn),選取一個(gè)范圍和一個(gè)比率,每一個(gè)都對(duì)這三個(gè)點(diǎn)取中點(diǎn),中點(diǎn)的縱坐標(biāo)加一個(gè)在范圍內(nèi)的隨機(jī)值,當(dāng)完成一次遞歸之后縮小這個(gè)范圍即range*rate
連線的時(shí)候,將三角形的一個(gè)點(diǎn)和這個(gè)點(diǎn)的兩條邊生成的中點(diǎn)相連,最后將三條邊的中點(diǎn)相連
所以應(yīng)該有四個(gè)遞歸調(diào)用。

Java分形怎么繪制山脈模型

代碼實(shí)現(xiàn)及注解

package Mountion;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;

public class ShowUI {
    List<Shape> list=new ArrayList<>();
    int i=0;
    public void divide(int x1,int x2,int x3,int y1,int y2,int y3,int range,double rate,int times,Graphics g,Shape p){
        if(times==0){
            //畫線,構(gòu)成一個(gè)三角形
            g.drawLine(x1, y1, x2, y2);
            g.drawLine(x3, y3, x2, y2);
            g.drawLine(x1, y1, x3, y3);
            return ;
        }
        else{
            times--;
            Random rand=new Random();
            //注意數(shù)據(jù)需要初始化
            int x4=0,x5=0,x6=0,y4=0,y5=0,y6=0;
            int flag1=0,flag2=0,flag3=0;
            //遍歷這個(gè)表
            for(Shape s : list) {
                //相等的兩種狀況
                if((s.pX1()==x1&&s.pX2()==x2&&s.pY1()==y1&&s.pY2()==y2)||(s.pX1()==x2&&s.pX2()==x1&&s.pY1()==y2&&s.pY2()==y1)){
                    if(s.show(g)==1){
                        //如果這個(gè)邊已經(jīng)被使用過了,那么此時(shí)說明中點(diǎn)已經(jīng)生成,只需要讀出來中點(diǎn)即可
                        x4=s.pX3();
                        y4=s.pY3();
                        //System.out.println(times+":same:==================size:"+(i++));
                    }
                    else{
                        //如果這條邊存在但沒有使用,那么生成這個(gè)中點(diǎn),進(jìn)行更新,其實(shí)就是將原來的shape刪除掉,加入一個(gè)新的。
                        x4=(x1+x2)/2;
                        y4=(y1+y2)/2+rand.nextInt(range*2)-range;
                        //System.out.println(times+":1");
                        list.remove(s);
                        p =new Shape(x1,x2,x4,y1,y2,y4,1);
                        list.add(p);
                    }
                    flag1=1;
                    break;
                }
                
                
                
            }
            if(flag1==0){
                //System.out.println(times+":before:==================size:"+(i++));
                //如果不存在,這個(gè)很簡單,只需要構(gòu)造一個(gè)就好了
                x4=(x1+x2)/2;
                y4=(y1+y2)/2+rand.nextInt(range*2)-range;
                p =new Shape(x1,x2,x4,y1,y2,y4,1);
                list.add(p);
            }
            //后面兩種情況類似
            for(Shape s : list) {
                if((s.pX1()==x1&&s.pX2()==x3&&s.pY1()==y1&&s.pY2()==y3)||(s.pX1()==x3&&s.pX2()==x1&&s.pY1()==y3&&s.pY2()==y1)){
                    if(s.show(g)==1){
                        x5=s.pX3();
                        y5=s.pY3();
                    }
                    else{
                        x5=(x1+x3)/2;
                        y5=(y1+y3)/2+rand.nextInt(range*2)-range;
                        list.remove(s);
                        p =new Shape(x1,x3,x5,y1,y3,y5,1);
                        list.add(p);
                    }
                    flag2=1;
                    break;
                }
            }
            if(flag2==0){
                x5=(x1+x3)/2;
                y5=(y1+y3)/2+rand.nextInt(range*2)-range;
                //System.out.println(times+":before1:==================size:"+(i++));
                p =new Shape(x1,x2,x4,y1,y2,y4,1);
                list.add(p);
            }
            for(Shape s : list){
                if((s.pX1()==x2&&s.pX2()==x3&&s.pY1()==y2&&s.pY2()==y3)||(s.pX1()==x3&&s.pX2()==x2&&s.pY1()==y3&&s.pY2()==y2)){
                    if(s.show(g)==1){
                        x6=s.pX3();
                        y6=s.pY3();
                    }
                    else{
                        x6=(x2+x3)/2;
                        y6=(y2+y3)/2+rand.nextInt(range*2)-range;
                        list.remove(s);
                        p =new Shape(x2,x3,x6,y2,y3,y6,1);
                        list.add(p);
                    }
                    flag3=1;
                    break;
                }
            }
            if(flag3==0){
                x6=(x2+x3)/2;
                y6=(y2+y3)/2+rand.nextInt(range*2)-range;
                //System.out.println(times+":before2:==================size:"+(i++));
                p =new Shape(x1,x2,x4,y1,y2,y4,1);
                list.add(p);
            }
            //逐漸縮小范圍
            range=(int)(range*rate);
            //將邊添加進(jìn)行,類似一個(gè)初始話,標(biāo)記位為0
            p =new Shape(x1,x4,y1,y4,0);
            list.add(p);
            p =new Shape(x1,x5,y1,y5,0);
            list.add(p);
            p =new Shape(x4,x5,y4,y5,0);
            list.add(p);
            p =new Shape(x2,x4,y2,y4,0);
            list.add(p);
            p =new Shape(x2,x6,y2,y6,0);
            list.add(p);
            p =new Shape(x4,x6,y4,y6,0);
            list.add(p);
            p =new Shape(x3,x5,y3,y5,0);
            list.add(p);
            p =new Shape(x3,x6,y3,y6,0);
            list.add(p);
            p =new Shape(x5,x6,y5,y6,0);
            list.add(p);
            //畫四個(gè)三角形
            divide(x1,x4,x5,y1,y4,y5,range,rate,times,g,p);
            divide(x2,x4,x6,y2,y4,y6,range,rate,times,g,p);
            
            divide(x3,x5,x6,y3,y5,y6,range,rate,times,g,p);
            divide(x4,x5,x6,y4,y5,y6,range,rate,times,g,p);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame moun=new JFrame();
        moun.setSize(1000, 600);
        moun.setLocationRelativeTo(null);
        moun.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        moun.setVisible(true);
        Graphics g=moun.getGraphics();//獲取窗體
        int x1=500,x2=100,x3=850;
        int y1=100,y2=400,y3=400;
        int range=200;
        double rate=0.5;
        int times=7;
        ShowUI a=new ShowUI();
        try {
            Thread.sleep(566);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Shape p = null;
        //System.out.println(100);
        a.divide(x1, x2, x3, y1, y2, y3, range, rate,times,g,p);

    }

}

Shape類

package Mountion;

import java.awt.Graphics;

public class Shape {
    private int x1,x2,x3,y1,y2,y3;
    private int flag;
    //這個(gè)構(gòu)造函數(shù)存儲(chǔ)的是一個(gè)邊的兩個(gè)點(diǎn),以及這個(gè)邊生成的中點(diǎn)
    public Shape(int x1,int x2,int x3,int y1,int y2,int y3,int flag){
        this.x1=x1;
        this.x2=x2;
        this.x3=x3;
        this.y1=y1;
        this.y2=y2;
        this.y3=y3;
        this.flag=flag;
    }
//這個(gè)構(gòu)造函數(shù)存儲(chǔ)的一條邊的兩個(gè)點(diǎn)
    public Shape(int x1,int x2,int y1,int y2,int flag){
        this.x1=x1;
        this.x2=x2;
        this.y1=y1;
        this.y2=y2;
        this.flag=flag;
    }
    //flag標(biāo)記位,這條邊是否被使用過
    public int show(Graphics g) {
        // TODO Auto-generated method stub
        return flag;
    }
    public int pX1(){
        return x1;
    }
    public int pX2(){
        return x2;
    }
    public int pX3(){
        return x3;
    }
    public int pY1(){
        return y1;
    }
    public int pY2(){
        return y2;
    }
    public int pY3(){
        return y3;
    }
}

效果展示

Java分形怎么繪制山脈模型

Java分形怎么繪制山脈模型

這篇文章主要為大家分析了Java分形怎么繪制山脈模型的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì)易懂,操作細(xì)節(jié)合理,具有一定參考價(jià)值。如果感興趣的話,不妨跟著跟隨小編一起來看看,下面跟著小編一起深入學(xué)習(xí)“Java分形怎么繪制山脈模型”的知識(shí)吧。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI