Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 17
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
Vaxian (17)


Next birthdays
05/21 Dalus (34)
05/21 Kizmo (37)
05/22 Skynet (32)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

Julia set in Java

Move Thread LAN_403
Myke
Tue Jun 09 2009, 05:14AM Print
Myke Registered Member #540 Joined: Mon Feb 19 2007, 07:49PM
Location: MIT
Posts: 969
Hi. I tried coding up a program to render a Julia set given the complex number c. For some reason, the code's algorithm always escapes to infinity leading it to plot no points in the window. Can someone look over my code and try to find what is causing it to do this?
import gpdraw.DrawingTool;
import gpdraw.SketchPad;

public class JuliaSet {
	private double Cre, Cim;
	private double newRe, newIm, oldRe, oldIm;
	private int a = -100;
	private int b = -100;
	private int maxIterations = 150;
	SketchPad paper = new SketchPad(200, 200);
	DrawingTool pen = new DrawingTool(paper);

	public JuliaSet(double re, double im) {
		Cre = re;
		Cim = im;
	}

	public void render() {
		for (int re = a; re <= 100; re++) {
			for (int im = b; im <= 100; im++) {
				newRe = re / 100.0;
				newIm = im / 100.0;

				int k;
				for (k = 0; k < maxIterations; k++) {
					oldRe = newRe;
					oldIm = newIm;
					newRe = oldRe * oldRe - oldIm * oldIm + Cre;
					newIm = 2 * oldRe * oldIm + Cim;
					if ((newRe * newRe + newIm * newIm) > 100)
						break;
				}
				if ((newRe * newRe + newIm * newIm) <= 4)
					pointAt(re, im);
			}
		}
	}

	public void pointAt(int x, int y) {
		System.out.println(x + " " + y);
		pen.up();
		pen.move(x, y);
		pen.down();
		pen.move(x + 1, y);
	}

	public static void main(String[] args) {
		JuliaSet set = new JuliaSet(1, 0);
		set.render();
	}
}

EDIT: Nevermind, I got it working and here is the working code: (I still need to implement the color part of the fractal)
import java.awt.Color;
import gpdraw.DrawingTool;
import gpdraw.SketchPad;

public class JuliaSet {
	private double Cre, Cim;
	private double newRe, newIm, oldRe, oldIm;
	private int a = -800;
	private int b = -800;
	private int maxIterations = 256;
	private double zoom = 400;
	SketchPad paper = new SketchPad(1600, 1600);
	DrawingTool pen = new DrawingTool(paper);

	public JuliaSet(double re, double im) {
		Cre = re;
		Cim = im;
	}

	public void render() {
		for (int re = a; re <= Math.abs(a); re++) {
			for (int im = b; im <= Math.abs(b); im++) {
				newRe = re / zoom;
				newIm = im / zoom;

				int k;
				for (k = 0; k < maxIterations; k++) {
					oldRe = newRe;
					oldIm = newIm;
					newRe = oldRe * oldRe - oldIm * oldIm + Cre;
					newIm = 2 * oldRe * oldIm + Cim;
					if ((newRe * newRe + newIm * newIm) > 4)
						break;
				}
				if ((newRe * newRe + newIm * newIm) <= 4)
					pointAt(re, im);
//				else
//					pointAt(re, im, new Color(Color.HSBtoRGB(k/(float)maxIterations, 1, 1)));
			}
		}
		System.out.println("DONE RENDERING");
	}

	public void pointAt(int x, int y) {
		pen.up();
		pen.move(x, y);
		pen.down();
		pen.move(x + 1, y);
	}
	
//	public void pointAt(int x, int y, Color col) {
//		System.out.println(x + " " + y);
//		pen.up();
//		pen.setColor(col);
//		pen.move(x, y);
//		pen.down();
//		pen.move(x + 1, y);
//	}

	public static void main(String[] args) {
		JuliaSet set = new JuliaSet(-1, 0);
		set.render();
	}
}

And here is a screenshot of the julia set for when C = -1.17-1.6i:
6i Julia Set
Back to top
Arkin
Sat Jun 13 2009, 02:20AM
Arkin Registered Member #2140 Joined: Tue May 26 2009, 09:16PM
Location:
Posts: 53
Just a quick tip, You use this code:
int k;
for (k = 0; k < maxIterations; k++) { ...}

But you can do:
for (int k = 0; k < maxIterations; k++) { ..}

Pointless, but keeps it more organized :).

You should also learn to use the Graphics Method (for applets, but you can do standalone applets also). Much more flexible, and very easy.
Back to top
Myke
Sat Jun 13 2009, 08:31AM
Myke Registered Member #540 Joined: Mon Feb 19 2007, 07:49PM
Location: MIT
Posts: 969
I am going to be using AWT for rendering. Also, I initialized k outside of the loop so that I could use the max number of times that it had iterated to determine the color that I should set it outside the set (probably am going to move it to being initialize it with all the other fields...).
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.