Created
February 8, 2017 05:03
-
-
Save ejherran/e5dfbe2893cc0c506cb1bd507de6ac29 to your computer and use it in GitHub Desktop.
Fractal: Curva Del Dragón - JAVA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * | |
| * Fractal: Curva Del Dragon | |
| * Javier Herran ([email protected]) | |
| * UPC - 2017 | |
| * | |
| */ | |
| import javax.swing.JFrame; | |
| import javax.swing.JPanel; | |
| import java.awt.Color; | |
| import java.awt.Graphics; | |
| public class FDragon extends JFrame | |
| { | |
| private Area area; | |
| public FDragon() | |
| { | |
| this.setSize(800, 600); | |
| this.setLocationRelativeTo(null); | |
| this.setTitle("FDragon"); | |
| this.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
| this.area = new Area(); | |
| this.add(this.area); | |
| this.setResizable(false); | |
| this.setVisible(true); | |
| Fractal f = new Fractal(this.area); | |
| f.start(); | |
| } | |
| public static void main(String[] args) | |
| { | |
| new FDragon(); | |
| } | |
| } | |
| class Area extends JPanel | |
| { | |
| private final Color c1 = new Color(255, 255, 255); // Blanco | |
| private final Color c2 = new Color(0, 0, 0); // Negro | |
| private final Color c3 = new Color(255, 0, 0); // Rojo | |
| private final Color c4 = new Color(0, 255, 0); // Verde | |
| private boolean isFin = false; | |
| private int[] xRedPoints; | |
| private int[] yRedPoints; | |
| private int nRedPoints = 0; | |
| private int[] xGreenPoints; | |
| private int[] yGreenPoints; | |
| private int nGreenPoints = 0; | |
| public void finalizar() | |
| { | |
| this.isFin = true; | |
| this.repaint(); | |
| } | |
| public int[][] getRedPoints() | |
| { | |
| int[][] points = new int[2][this.nRedPoints]; | |
| points[0] = this.xRedPoints; | |
| points[1] = this.yRedPoints; | |
| return points; | |
| } | |
| public int[][] getGreenPoints() | |
| { | |
| int[][] points = new int[2][this.nGreenPoints]; | |
| points[0] = this.xGreenPoints; | |
| points[1] = this.yGreenPoints; | |
| return points; | |
| } | |
| public void setRedPoints(int[] px, int[] py) | |
| { | |
| this.xRedPoints = px; | |
| this.yRedPoints = py; | |
| this.nRedPoints = px.length; | |
| } | |
| public void setGreenPoints(int[] px, int[] py) | |
| { | |
| this.xGreenPoints = px; | |
| this.yGreenPoints = py; | |
| this.nGreenPoints = px.length; | |
| } | |
| public void paint(Graphics g) | |
| { | |
| if(this.isFin) | |
| { | |
| g.setColor(c1); | |
| g.fillRect(0, 0, 800, 600); | |
| if(this.nGreenPoints > 0) | |
| { | |
| g.setColor(c2); | |
| g.drawPolyline(xGreenPoints, yGreenPoints, nGreenPoints); | |
| } | |
| } | |
| else | |
| { | |
| g.setColor(c2); | |
| g.fillRect(0, 0, 800, 600); | |
| if(this.nRedPoints > 0) | |
| { | |
| g.setColor(c3); | |
| g.drawPolyline(xRedPoints, yRedPoints, nRedPoints); | |
| } | |
| if(this.nGreenPoints > 0) | |
| { | |
| g.setColor(c4); | |
| g.drawPolyline(xGreenPoints, yGreenPoints, nGreenPoints); | |
| } | |
| } | |
| } | |
| } | |
| class Fractal extends Thread | |
| { | |
| private Area area; | |
| private int[] px; | |
| private int[] py; | |
| private int cp; | |
| private double minD = 1000; // Distancia minima | |
| public Fractal(Area area) | |
| { | |
| this.area = area; | |
| this.cp = 2; | |
| this.px = new int[2]; | |
| this.py = new int[2]; | |
| this.px[0] = 183; // Inicio de la recta primaria | |
| this.py[0] = 370; | |
| this.px[1] = 695; // Fin de la recta primaria | |
| this.py[1] = 370; | |
| } | |
| private double getDistancia(int[] p1, int[] p2) | |
| { | |
| double d = Math.sqrt(Math.pow( p1[0]-p2[0], 2 ) + Math.pow( p1[1] - p2[1], 2 )); | |
| return d; | |
| } | |
| private int[] getPunto(int[] p1, int[] p2, boolean flag) | |
| { | |
| int nx; | |
| int ny; | |
| double d = this.getDistancia(p1, p2); | |
| if(d < this.minD) | |
| { | |
| this.minD = d; | |
| } | |
| if(p1[0] != p2[0] && p1[1] != p2[1]) // Diagonal | |
| { | |
| double m = (p2[1]-p1[1]) / (p2[0]-p1[0]); | |
| if(flag) | |
| { | |
| if(m < 0) | |
| { | |
| nx = p1[0]; | |
| ny = p2[1]; | |
| } | |
| else | |
| { | |
| nx = p2[0]; | |
| ny = p1[1]; | |
| } | |
| } | |
| else | |
| { | |
| if(m < 0) | |
| { | |
| nx = p2[0]; | |
| ny = p1[1]; | |
| } | |
| else | |
| { | |
| nx = p1[0]; | |
| ny = p2[1]; | |
| } | |
| } | |
| int[] res = {nx, ny}; | |
| return res; | |
| } | |
| else if(p1[0] == p2[0]) // Vertical | |
| { | |
| if(flag) | |
| { | |
| if(p1[1] < p2[1]) | |
| { | |
| nx = (int)(p1[0] + d/2.0); | |
| ny = (int)(p1[1] + d/2.0); | |
| } | |
| else | |
| { | |
| nx = (int)(p1[0] - d/2.0); | |
| ny = (int)(p2[1] + d/2.0); | |
| } | |
| } | |
| else | |
| { | |
| if(p1[1] < p2[1]) | |
| { | |
| nx = (int)(p1[0] - d/2.0); | |
| ny = (int)(p1[1] + d/2.0); | |
| } | |
| else | |
| { | |
| nx = (int)(p1[0] + d/2.0); | |
| ny = (int)(p2[1] + d/2.0); | |
| } | |
| } | |
| int[] res = {nx, ny}; | |
| return res; | |
| } | |
| else if(p1[1] == p2[1]) // Horizontal | |
| { | |
| if(flag) | |
| { | |
| if(p1[0] < p2[0]) | |
| { | |
| nx = (int)(p1[0] + d/2.0); | |
| ny = (int)(p1[1] - d/2.0); | |
| } | |
| else | |
| { | |
| nx = (int)(p2[0] + d/2.0); | |
| ny = (int)(p1[1] + d/2.0); | |
| } | |
| } | |
| else | |
| { | |
| if(p1[0] < p2[0]) | |
| { | |
| nx = (int)(p1[0] + d/2.0); | |
| ny = (int)(p1[1] + d/2.0); | |
| } | |
| else | |
| { | |
| nx = (int)(p2[0] + d/2.0); | |
| ny = (int)(p1[1] - d/2.0); | |
| } | |
| } | |
| int[] res = {nx, ny}; | |
| return res; | |
| } | |
| return null; | |
| } | |
| public void run() | |
| { | |
| while(true) | |
| { | |
| try | |
| { | |
| int tn = (2*cp)-1; | |
| int[] npx = new int[tn]; | |
| int[] npy = new int[tn]; | |
| boolean flag = true; // true = Izq. false = Der. | |
| for(int i = 0; i < this.cp-1; i++) | |
| { | |
| npx[2*i] = this.px[i]; | |
| npy[2*i] = this.py[i]; | |
| int[] p1 = {this.px[i], this.py[i]}; | |
| int[] p2 = {this.px[i+1], this.py[i+1]}; | |
| int[] pn = this.getPunto(p1, p2, flag); | |
| npx[(2*i)+1] = pn[0]; | |
| npy[(2*i)+1] = pn[1]; | |
| flag = !flag; | |
| } | |
| npx[tn-1] = this.px[cp-1]; | |
| npy[tn-1] = this.py[cp-1]; | |
| this.area.setRedPoints(this.px, this.py); | |
| this.area.setGreenPoints(npx, npy); | |
| this.area.repaint(); | |
| this.px = npx; | |
| this.py = npy; | |
| this.cp = tn; | |
| Thread.sleep(1000); | |
| System.err.println(this.minD); | |
| if(this.minD <= 1) | |
| break; | |
| } | |
| catch(InterruptedException ex) | |
| { | |
| System.err.println(ex.toString()); | |
| } | |
| } | |
| this.area.finalizar(); | |
| System.out.println("Fin!"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment