• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fractals  RSS feed

 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!. I have a problem using this code. I want to make centered squares by using this formula:

Fractal (x, y, r)
if r > 0
Fractal (x+r, y-r, r div 2)
Square (x, r, r)

Here is what I have so far:
-------------------------------------------------------------------
public static void Square (int x, int y, int r)
{
c.drawRect(x,y,2*r,2*r);
}
public static void Fractal (int x,int y,int r)
{
if (r > 0)
{
Fractal (x-r, y+r, r / 2);
Square (x,r,r);
}
}
-------------------------------------------------------------

I call this method by:

Fractal (x,y,r);

But, when I run the program the program draw the squares but not centered. I ask, HOW THIS CAN HAPPEN?. I don't know what is missing?. Maybe because Im not using the "div", when I try to type this:
"Fractal (x+r, y-r, r div 2)"

The program returns an error. I dont know how to call "div".
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is div supposed to do?

'/' is divide, (6/3=2)
'%' is modulus, (10%3=1)

what is 6 div 2?
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I have my code is fine. But, where is the problem then?. Why the program does not draw the squares centered?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nick George:
what is div supposed to do?

'/' is divide, (6/3=2)
'%' is modulus, (10%3=1)

what is 6 div 2?


In mathematical terms "div" usually means to perform integer division. Of course, Java does this automatically with the / operator when it's two operands are both integer types.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manuel Diaz:
Ok, so I have my code is fine. But, where is the problem then?. Why the program does not draw the squares centered?

In the pseudocode description, you have

but in your Java code you have

Which one is correct?

Layne
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java code is the correct one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!