Author Topic: L-Systems : Dragon Curve Fractal [Modified]  (Read 2849 times)

0 Members and 1 Guest are viewing this topic.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
L-Systems : Dragon Curve Fractal [Modified]
« on: September 16, 2014, 06:59:15 pm »
I made this Dragon Fractal in Java. Its a bit modified. Next time I will make a better one with animation :D

Code: (java) [Select]
package dragon;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

public final class DragonCurve {

private BufferedImage img = null;
private int IMG_WIDTH;
private int IMG_HEIGHT;

private GraphicsEnvironment ge = null;
private GraphicsDevice gd = null;
private GraphicsConfiguration gc = null;

public DragonCurve() {
IMG_WIDTH = 1400;
IMG_HEIGHT = 1400;

ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
gc = gd.getDefaultConfiguration();

}

private void createDragonFractal() {
Graphics g = null;
Graphics2D g2d = (Graphics2D) g;

img = new BufferedImage(IMG_WIDTH, IMG_HEIGHT,
BufferedImage.TYPE_INT_RGB);
img = gc.createCompatibleImage(IMG_WIDTH, IMG_HEIGHT);
g2d = img.createGraphics();
g2d.setColor(Color.black);
g2d.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
g2d.setColor(Color.red);
dragonRecur(300, 500, 1000, 1100, 20, g2d);
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
writeImage();

}

private void writeImage() {
File file = new File("pic.png");
try {
file.createNewFile();
OutputStream out = new FileOutputStream(file);
ImageIO.write(img, "png", out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

private void dragonRecur(int x1, int y1, int x2, int y2, int k,
Graphics2D g2d) {
if (k > 0) {
int xn = (x1 + x2) / 2 + (y2 - y1) / 2;
int yn = (y1 + y2) / 2 - (x2 - x1) / 2;
dragonRecur(x2, y2, xn, yn, k - 1, g2d);
dragonRecur(x1, y1, xn, yn, k - 1, g2d);
} else {
g2d.drawLine(x1, y1, x2, y2);
}
}

public static void main(String... strings) {
new DragonCurve().createDragonFractal();
}

}



Sample Output




Played with the code and added rore Refactoring and now it is better antialiased and rendered.


Sample Output




Source
« Last Edit: September 17, 2014, 09:03:49 am by Psycho_Coder »
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: L-Systems : Dragon Curve Fractal [Modified]
« Reply #1 on: September 16, 2014, 07:39:06 pm »
looks nice to me. That's a topic I always wanted to get my hands on, but always some sh*t is getting in the way. It needs a push-up o my to-do-list, thanks for reminding me. +1
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: L-Systems : Dragon Curve Fractal [Modified]
« Reply #2 on: September 16, 2014, 09:51:50 pm »
Can't wait to see the animated one.
Btw, you can add a width to images on EZ:

Code: [Select]
[img width=400][/img]

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: L-Systems : Dragon Curve Fractal [Modified]
« Reply #3 on: September 16, 2014, 10:58:27 pm »
Hey I did a project with LSystems too, to generate random city layouts for my game. I actually used it to make an animated Sierpinski triangle, Koch snowflake, and many more. Yours looks pretty nice but it technically isn't a real L system. L Systems start as a string of commands which correlate to certain events, and then recursively replace and changes it to make more complex command strings. You're just using a recursive function. And although it has the idea of an LSystem, it isn't really one.

Try making a turtle, rule, and lsystem class or something and add stack (with LIFO priority) functionality. With mine you can make new rules to generate the strings and assign characters (commands) to the turtle for when it reads the string. You can make some really neat stuff once you have a basic framework.
I'll try and dig that project up, it's on my old laptop.

Good job, it looks neat!
« Last Edit: September 16, 2014, 11:00:04 pm by Matriplex »
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: L-Systems : Dragon Curve Fractal [Modified]
« Reply #4 on: September 17, 2014, 09:01:53 am »
Hey I did a project with LSystems too, to generate random city layouts for my game. I actually used it to make an animated Sierpinski triangle, Koch snowflake, and many more. Yours looks pretty nice but it technically isn't a real L system. L Systems start as a string of commands which correlate to certain events, and then recursively replace and changes it to make more complex command strings. You're just using a recursive function. And although it has the idea of an LSystem, it isn't really one.

Try making a turtle, rule, and lsystem class or something and add stack (with LIFO priority) functionality. With mine you can make new rules to generate the strings and assign characters (commands) to the turtle for when it reads the string. You can make some really neat stuff once you have a basic framework.
I'll try and dig that project up, it's on my old laptop.

Good job, it looks neat!

Actually it is an L System.

The rules would be :-

    variables : X Y
    constants : F + −
    start  : FX
    rules  : (X → X+YF), (Y → FX-Y)
    angle  : 90°


I just happened to modify it a bit and hence I said [Modified], I have made different L-Systems as well, like Dragon Curve, Tree Fractal, Sierpinski triangle. I will soon make a snowflake as well. I just love these.

I am willing to work on L-Systems with Genetic Algorithms and believe me the visualizations are as good as Anime girls  ;D (the sexy titted girls)
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: L-Systems : Dragon Curve Fractal [Modified]
« Reply #5 on: September 17, 2014, 09:05:43 am »
Can't wait to see the animated one.
Btw, you can add a width to images on EZ:

Code: [Select]
[img width=400][/img]

Thanks I will make one for sure. You know that if I have said something then I have always done that :) and now that you said so ........ <3

ahaahhaahahaha
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: L-Systems : Dragon Curve Fractal [Modified]
« Reply #6 on: September 18, 2014, 08:41:01 am »
Actually it is an L System.

The rules would be :-

    variables : X Y
    constants : F + −
    start  : FX
    rules  : (X → X+YF), (Y → FX-Y)
    angle  : 90°


I just happened to modify it a bit and hence I said [Modified], I have made different L-Systems as well, like Dragon Curve, Tree Fractal, Sierpinski triangle. I will soon make a snowflake as well. I just love these.

I am willing to work on L-Systems with Genetic Algorithms and believe me the visualizations are as good as Anime girls  ;D (the sexy titted girls)

I think Matriplex' point was that your code does not take these rules as input. You hardcoded them instead.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
Re: L-Systems : Dragon Curve Fractal [Modified]
« Reply #7 on: September 18, 2014, 03:36:14 pm »
I think Matriplex' point was that your code does not take these rules as input. You hardcoded them instead.

Yes. Later  might make something which will take any grammar and generate the equivalent fractal. In those case it would look much better. Before that I plan on implement about 5-6 fractal and then will beign to make that.

That shouldn't be very difficult.


Have a good day :)
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins