views:

337

answers:

7

I am trying to convert this code from java to C# (located here)

I have some winform experience but not a lot with the drawing of pixels on a winform applications.

I feel fairly confident I can convert over most of the sub methods im just unclear on how i would draw indiviual pixels on the screen

any help or tools for converting over java to c# would be greatly apprehsated

// Buddhabrot
// j.tarbell   January, 2004
// Albuquerque, New Mexico
// complexification.net

// based on code by Paul Bourke
// astronomy.swin.edu.au/~pbourke/

// Processing 0085 Beta syntax update
// j.tarbell   April, 2005

int dim = 800;             // screen dimensions (square window)
int bailout = 200;         // number of iterations before bail
int plots = 10000;        // number of plots to execute per frame (x30 = plots per second)

// 2D array to hold exposure values
int[] exposure = new int[dim*dim];
int maxexposure;           // maximum exposure value
int time = 0;
int exposures = 0;

boolean drawing;
PFont metaBold;

//  MAIN ----------------------------------------------------------------

void setup() {
  // set up drawing area
  size(800,800,P3D);
  background(0);
  // take it nice and easy
  framerate(15);
  // load typeface
  metaBold = loadFont("Arial-48.vlw");
}

void draw() {
  plotPlots();
  time++;
  if (time%30==0) {
    // show progress every 2 seconds or so...
    findMaxExposure();
    renderBrot();
    // show exposure value
    fill(255);
    noStroke();
    textFont(metaBold, 14);
    text("bailout:  "+bailout+"    exposures: "+exposures, 5, dim-6);
  }
}

void plotPlots() {
  float x, y;
  // iterate through some plots
  for (int n=0;n<plots;n++) {
    // Choose a random point in same range
    x = random(-2.0,1.0);
    y = random(-1.5,1.5);
    if (iterate(x,y,false)) {
      iterate(x,y,true);
      exposures++;
    }
  }
}

void renderBrot() {
  // draw to screen
  for (int i=0;i<dim;i++) {
    for (int j=0;j<dim;j++) {
      float ramp = exposure[i*dim+j] / (maxexposure / 2.5);
      // blow out ultra bright regions
      if (ramp > 1)  {
        ramp = 1;
      }
      color c = color(int(ramp*255), int(ramp*255), int(ramp*255));
      set(j,i,c);
    }
  }
}

//   Iterate the Mandelbrot and return TRUE if the point exits
//   Also handle the drawing of the exit points
boolean iterate(float x0, float y0, boolean drawIt) {
  float x = 0;
  float y = 0;
  float xnew, ynew;
  int ix,iy;

  for (int i=0;i<bailout;i++) {
    xnew = x * x - y * y + x0;
    ynew = 2 * x * y + y0;
    if (drawIt && (i > 3)) {
      ix = int(dim * (xnew + 2.0) / 3.0);
      iy = int(dim * (ynew + 1.5) / 3.0);
      if (ix >= 0 && iy >= 0 && ix < dim && iy < dim) {
        // rotate and expose point
        exposure[ix*dim+iy]++;
      }

    }
    if ((xnew*xnew + ynew*ynew) > 4) {
      // escapes
      return true;
    }
    x = xnew;
    y = ynew;
  }
  // does not escape
  return false;
}

void findMaxExposure() {
  // assume no exposure
  maxexposure=0;
  // find the largest density value
  for (int i=0;i<dim;i++) {
    for (int j=0;j<dim;j++) {
      maxexposure = max(maxexposure,exposure[i*dim+j]);
    }
  }
}

// Buddhabrot
// j.tarbell   January, 2004
+3  A: 

Look at System.Drawing and the Graphics class.

EDIT: Just to clear up the confusion, the OP's code is not Java. It's Processing. It says so in the comments, but also, you can tell because there is no class definition, no imports, and the calls aren't Java calls. It compiles to bytecode, and interoperates with Java, but it's not Java -- no automatic conversion will help.

Lou Franco
+1  A: 

To draw an invidual pixel you could use:

e.Graphics.FillRectangle(aBrush, x, y, 1, 1);

You may want to look here.

Burkhard
+2  A: 

System.Drawing namespace has all sorts of graphics stuff in it. And there are some tutorials on it.

Spoike
+4  A: 

Here is a similar SO question: Tools to assist.

Here is a MS released tool for converting Java to C#.

MatthewMartin
the ms tool assumes i have the full vs.net I do not
Crash893
You also do not have Java, you have Processing.
Lou Franco
A: 

Are we playing Guess the Base Class? :)

That original code doesn't appear to be complete java, what with global variables and free functions. Is it the contents of a class declaration?

The question is, what framework was the Java code using? Because if it's a fancy double-buffering system, then you probably won't get the same results in Winforms by plotting pixels directly on the screen.

You will probably get the best results by Googling for the keywords: DirectX C#

Update: the OP didn't know what language it was.

Daniel Earwicker
It's not Java -- it's Processing
Lou Franco
I've edited the question title and tags accordingly.
Daniel Earwicker
+2  A: 

If you're trying to manipulate pixels and draw to the screen while maintaining high framerate you're probably going to want to look into System.Drawing.Bitmap using lockbits and unlockbits.

A good explanation can be found here. Otherwise you're not really going to be able to do pixel-level edits with any decent speed.

McAden
I recommend using this approach if you don't want to jump down to an unmanaged language.
Ron Warholic
+1  A: 

If you go to processing.org and download the Processing software, you can copy this code into the IDE. Then if you do "File>Export", it will create a Java applet version of the program, complete with Java source code. Then maybe you can run some other converter to get the C# code.

(Note that this code seems to be an older version of Processing; I had to change "framerate" to "frameRate" for it to run. Also, I had to do the command "Tools>Create Font..." to create the "Arial-48.vlw" font file that is needed.)

Greg Graham