d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Java
12Next
Add Reply New Topic New Poll
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 27 2014 09:41pm
i need someone to fix my code,
its about 90% complete
willing to pay fg
please post
So the code below should be generates Julia Sets like this one:

So if you input the values:
CReal: 0.3
CImag: 0.0
Xmin: -2.0
Xmax: 2.0
Ymin: -2.0
Ymax: 2.0
WhateverName.jpg

it should print out the picture above.
But my code prints out a plain red screen.
I think the problem is within the While loop but i can't find the problem
So if someone can find it that would be very helpful.


Here my code:


import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;

public class Julia {

public static void main( String[] args) throws Exception
{
Scanner scan = new Scanner( System.in );
double CReal, CImag, Xmin, Xmax, Ymin, Ymax;

int width = 512;
int height = 512;

BufferedImage outImage = new BufferedImage( width, height, BufferedImage.TYPE_3BYTE_BGR );

System.out.println("Input: CReal CImag Xmin Xmax Ymin Ymax fname");

CReal = scan.nextDouble();
CImag = scan.nextDouble();
Xmin = scan.nextDouble();
Xmax = scan.nextDouble();
Ymin = scan.nextDouble();
Ymax = scan.nextDouble();
String fname = scan.next();




Complex C = new Complex(CReal, CImag);

for(int x=0; x<height; x++ ) {
for(int y=0; y<width; y++) {
double zReal = Xmin + x*((Xmax-Xmin)/512);
double zImag = Ymin + y*((Ymax-Ymin)/512);
// Create new complex number called z
Complex Z = new Complex(zReal,zImag );
int iterations = juliaCalc(Z , C);

int pixelcolor = getHSBColor(iterations);
outImage.setRGB(x, y, pixelcolor);
//System.out.println(pixelcolor);
}
}
saveImage(outImage, new File( fname ));
}

public static void saveImage( BufferedImage img, File file ) throws IOException{

ImageWriter writer = null;
java.util.Iterator iter = ImageIO.getImageWritersByFormatName("jpg");

if( iter.hasNext() ){
writer = (ImageWriter)iter.next();
}

ImageOutputStream ios = ImageIO.createImageOutputStream( file );
writer.setOutput(ios);

ImageWriteParam param = new JPEGImageWriteParam( java.util.Locale.getDefault() );
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
param.setCompressionQuality(0.98f);

writer.write(null, new IIOImage( img, null, null ), param);

}

public static int juliaCalc(Complex Z, Complex C) {

int iterations = 0;
// Complex i = Z.multiply(Z);
// Complex j = i.add(C);
Complex k = Z.multiply(Z).add(C);
//System.out.println(k);


//for(iterations = 0; iterations < 256; iterations++) {
while (k.magnitude() < 2.0)
{
if(iterations > 255)
break;
else{
Z = Z.multiply(Z).add(C);
//System.out.println(Z);
iterations++;
}
}

// }
System.out.println(iterations);
return iterations;


}

public static int getHSBColor(int idx)
{
return Color.getHSBColor((float)(idx/255.0), 1.0f, 1.0f).getRGB();
}
}

class Complex
{
double real;
double imaginary;

Complex(double r, double i)
{
real = r;
imaginary = i;
}

//the calculation of magnitude
public double magnitude()
{
double mag = 0;
mag = Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imaginary, 2));
return mag;
}

//the calculation of multiplication
public Complex multiply(Complex toMultiply)
{
double realMultiComponent;
double imaginaryMultiComponent;
realMultiComponent = this.real * toMultiply.real - this.imaginary * toMultiply.imaginary;
imaginaryMultiComponent = this.imaginary * toMultiply.real + this.real * toMultiply.imaginary;
Complex multiplied = new Complex(realMultiComponent, imaginaryMultiComponent);
return multiplied;
}

//the calculation of addition
public Complex add(Complex toAdd)
{
double realAddComponent;
double imaginaryAddComponent;
realAddComponent = this.real + toAdd.real;
imaginaryAddComponent = this.imaginary + toAdd.imaginary;
Complex added = new Complex(realAddComponent, imaginaryAddComponent);
return added;
}
}

This post was edited by CiroC on Feb 27 2014 10:02pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 27 2014 09:51pm
why don't you post what you need help with?
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 27 2014 10:02pm
Quote (carteblanche @ Feb 27 2014 07:51pm)
why don't you post what you need help with?


there i posted it
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 27 2014 10:18pm
i'm not familiar with the math and tbh i dont care to look at it in detail for someone's homework.

as far as i can tell at a glance, this is the line of code that actually draws the shape right?

outImage.setRGB(x, y, pixelcolor);

add some logging and confirm for me that the x and y are correct.
if they are correct:
1) make sure pixelcolor is different from the background
2) make sure you're drawing on top of the background, as opposed to drawing the shape first then drawing the background on top of it

if they are not correct, please narrow down what function/line of code is causing it to be off
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 27 2014 10:40pm
Quote (carteblanche @ Feb 27 2014 08:18pm)
i'm not familiar with the math and tbh i dont care to look at it in detail for someone's homework.

as far as i can tell at a glance, this is the line of code that actually draws the shape right?

outImage.setRGB(x, y, pixelcolor);

add some logging and confirm for me that the x and y are correct.
if they are correct:
1) make sure pixelcolor is different from the background
2) make sure you're drawing on top of the background, as opposed to drawing the shape first then drawing the background on top of it

if they are not correct, please narrow down what function/line of code is causing it to be off


my TA told me the problem is in this part of the code, something about my while loop.

public static int juliaCalc(Complex Z, Complex C) {

int iterations = 0;
// Complex i = Z.multiply(Z);
// Complex j = i.add(C);
Complex k = Z.multiply(Z).add(C);
//System.out.println(k);


//for(iterations = 0; iterations < 256; iterations++) {
while (k.magnitude() < 2.0)
{
if(iterations > 255)
break;
else{
Z = Z.multiply(Z).add(C);
//System.out.println(Z);
iterations++;
}
}

// }
System.out.println(iterations);
return iterations;


}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 27 2014 10:45pm
Quote (CiroC @ Feb 27 2014 11:40pm)
my TA told me the problem is in this part of the code, something about my while loop.

public static int juliaCalc(Complex Z, Complex C) {

int iterations = 0;
// Complex i = Z.multiply(Z);
// Complex j = i.add(C);
Complex k = Z.multiply(Z).add(C);
//System.out.println(k);


//for(iterations = 0; iterations < 256; iterations++) {
while (k.magnitude() < 2.0)
{
if(iterations > 255)
break;
else{
Z = Z.multiply(Z).add(C);
//System.out.println(Z);
iterations++;
}
}

// }
System.out.println(iterations);
return iterations;


}


can you explain what you think your while loop is doing? and why are you returning iterations?

it's not obvious to me if your condition (k.magnitude() < 2.0) does anything since i can't tell if k.magnitude changes. looks like you're just executing this 256 times:

Z = Z.multiply(Z).add(C);

is that your intention?

perhaps you meant to use Z.magnitude() < 2.0 in your condition?

keep in mind that you keep re-assigning Z. so i'm not sure what you think is happening when you execute your method.

This post was edited by carteblanche on Feb 27 2014 10:47pm
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 27 2014 10:49pm
Quote (carteblanche @ Feb 27 2014 08:45pm)
can you explain what you think your while loop is doing? and why are you returning iterations?

it's not obvious to me if your condition (k.magnitude() < 2.0) does anything since i can't tell if k.magnitude changes. looks like you're just executing this 256 times:

Z = Z.multiply(Z).add(C);

is that your intention?

perhaps you meant to use Z.magnitude() < 2.0 in your condition?


k is simply Complex k = Z.multiply(Z).add(C)
so yo can replace the line
Z.multiply(Z).add(C).magnitude() < 2.0
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 27 2014 10:50pm
Quote (carteblanche @ Feb 27 2014 08:45pm)
can you explain what you think your while loop is doing? and why are you returning iterations?

it's not obvious to me if your condition (k.magnitude() < 2.0) does anything since i can't tell if k.magnitude changes. looks like you're just executing this 256 times:

Z = Z.multiply(Z).add(C);

is that your intention?

perhaps you meant to use Z.magnitude() < 2.0 in your condition?

keep in mind that you keep re-assigning Z. so i'm not sure what you think is happening when you execute your method.


SHIT IT WORKED!!!
THANK YOU SO MUCH!!!!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 27 2014 10:56pm
Quote (CiroC @ Feb 27 2014 11:49pm)
k is simply Complex k = Z.multiply(Z).add(C)
so yo can replace the line
Z.multiply(Z).add(C).magnitude() < 2.0


No, you can't. you keep re-assigning Z, so your initial k isn't changing. those two lines of code are different.

Quote (CiroC @ Feb 27 2014 11:50pm)
SHIT IT WORKED!!!
THANK YOU SO MUCH!!!!


oh, good.

notice how much more effective it is to post your problem and details?

This post was edited by carteblanche on Feb 27 2014 10:58pm
Member
Posts: 32,450
Joined: Jul 9 2007
Gold: 15.00
Feb 27 2014 11:02pm
Quote (carteblanche @ Feb 27 2014 08:56pm)
No, you can't. you keep re-assigning Z, so your initial k isn't changing. those two lines of code are different.



oh. k.


Let me know if you want some fg
dont be shy
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll