Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw Mountain Model with Java Fractal

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly analyzes the Java fractal how to draw the mountain model of the relevant knowledge points, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "how to draw mountain models with Java fractals".

How to draw a mountain range

Conception and design

Randomly select three points, select a range and a ratio, each take the midpoint to the three points, the ordinate of the midpoint plus a random value in the range, and narrow the range after completing a recursion, that is, range*rate

When connecting a line, connect a point of the triangle to the midpoint formed by the two sides of the point, and finally connect the midpoint of the three sides

So there should be four recursive calls.

Code implementation and annotations 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 list=new ArrayList (); int iTunes 0 Public void divide {if (times==0) {/ / draw a line to form a triangle g.drawLine (x1, y1, x2, x2, y2); g.drawLine (x3, y3, x2, y2); g.drawLine (x1, y1, x3, y3); return } else {times--; Random rand=new Random (); / / Note that the data needs to be initialized, int x4500rec x5500rec x650rec y450ref y550je y650m; int flag1=0,flag2=0,flag3=0 / / traverse the table for (Shape s: list) {/ / two equal conditions 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) {/ / if this edge has already been used Then at this point, the midpoint has been generated, and you only need to read the midpoint to x4=s.pX3 () Y4=s.pY3 (); / / System.out.println (times+ ": same:=size:" + (iTunes +)) } else {/ / if this edge exists but is not used, then to generate the midpoint and update it is to delete the original shape and add a new one. X4 = (x1+x2) / 2; Y4 = (y1+y2) / 2+rand.nextInt (range*2)-range; / / System.out.println (times+ ": 1"); list.remove (s); p = new Shape } flag1=1; break;}} if (flag1==0) {/ / System.out.println (times+ ": before:=size:" + (iTunes +)) / / if it doesn't exist, it's simple. You just need to construct one: x4 = (x1+x2) / 2; y4 = (y1+y2) / 2+rand.nextInt (range*2)-range; p = new Shape. } / / the latter two cases are similar to 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; 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:" + (iTunes +)); p = new Shape (x1Personx2); 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; 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:" + (iTunes +)); p = new Shape (x1, int, x4); list.add (p); / / gradually narrowing down the range range= (int) (range*rate) / / add edges to proceed, similar to an initial sentence, marked with the bit 0p = new Shape; list.add (p); p = new Shape (x5lemen y5mem0); list.add (p); p = new Shape (x4memx5recitation y4mem0); list.add (p) P = new Shape; list.add (p); p = new Shape (x2 recorder x6); list.add (p); p = new Shape (x4memx6); list.add (p); p = new Shape (x3memx5); list.add (p) P = new Shape, list.add (p), new Shape, list.add (p), / / draw four triangles, divide. Divide (x2 recollection x4); divide (x3 recorder x5); divide (x6); divide (x4); (x6) }} public static void main (String [] args) {/ / TODO Auto-generated method stub JFrame moun=new JFrame (); moun.setSize (1000); moun.setLocationRelativeTo (null); moun.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); moun.setVisible (true); Graphics g=moun.getGraphics (); / / get the form int x1, 500, X2, 100, 500, 350, 850 Int y1y100qingy2y400qingy3y400; 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 A.divide (x1, x2, x3, y1, y2, y3, range, rate,times,g,p);}}

Shape class

Package Mountion;import java.awt.Graphics;public class Shape {private int x1rect x2rec y2jinjue y2jiny3; private int flag; / / this constructor stores two points of an edge, as well as the midpoint public Shape generated by this edge (int x1mint x2rect int y3Powerint y2Powerint y2int flag) {this.x1=x1; this.x2=x2; this.x3=x3; this.y1=y1 This.y2=y2; this.y3=y3; this.flag=flag;} / / two points of an edge stored by this constructor public Shape (int x1 minint x2 int y1 int y1 int y2 int flag) {this.x1=x1; this.x2=x2; this.y1=y1; this.y2=y2; this.flag=flag } / / flag tag bit, whether this edge has been used by 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;}}

Effect display

This article mainly analyzes the Java fractal how to draw the mountain model of the relevant knowledge points, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "how to draw mountain models with Java fractals".

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report