The Naked Scientists

The Naked Scientists Forum

Author Topic: Fractal-sorta  (Read 2559 times)

Offline realmswalker

  • Sr. Member
  • ****
  • Posts: 205
    • View Profile
Fractal-sorta
« on: 07/04/2005 01:14:09 »
This is kinda a computer programing question soooo
but anyways, i trying to figure out a program that would basically do this:
start with a line, half way through that line it is divided by a line half as long and perpedicular to it, repeat to every existing line segemnt thats formed.


 

Offline Ultima

  • Sr. Member
  • ****
  • Posts: 488
    • View Profile
    • My Homepage
Re: Fractal-sorta
« Reply #1 on: 07/04/2005 10:09:59 »
Sounds like a good place to use a recursive function :D Do you have the start and end qo-ords for each line? If you give me a bit more to work with I can help you out.

wOw the world spins?
 

Offline DoctorBeaver

  • Naked Science Forum GOD!
  • *******
  • Posts: 12656
  • Thanked: 3 times
  • A stitch in time would have confused Einstein.
    • View Profile
Re: Fractal-sorta
« Reply #2 on: 11/04/2005 14:55:31 »
It's quite straightforward if you're happy to use just up, down, left & right for the direction of the line and use them sequentially. It'd be something like:-

startfunc:
    length=startlength
    direction={up, right, down, left}
    horiz=true
    do while l>1
           locate(x,y)
           drawline(length, direction)
           horiz=horiz NOR horiz
           length=length/2
           if horiz
                x=x+length
           else
                y=y+length
           endif
           direction(next)
     enddo
           
Using random angles for the direction changes would make it more fun!
 

Offline DoctorBeaver

  • Naked Science Forum GOD!
  • *******
  • Posts: 12656
  • Thanked: 3 times
  • A stitch in time would have confused Einstein.
    • View Profile
Re: Fractal-sorta
« Reply #3 on: 11/04/2005 14:56:25 »
oops! I meant to put "do while length>1"
 

Offline Ultima

  • Sr. Member
  • ****
  • Posts: 488
    • View Profile
    • My Homepage
Re: Fractal-sorta
« Reply #4 on: 11/04/2005 15:25:49 »
Or a clean recursive function defined to do each direction, at each segment...

Pseudo Java or C:

Code: [Select]
void doLine(startX,startY,length,direction) {
    if(length != 1) {
        drawLine(startX,startY,length,direction);
        doLine(startX/2,startY/2,length/2,up);
        doLine(startX/2,startY/2,length/2,right);
        doLine(startX/2,startY/2,length/2,left);
        doLine(startX/2,startY/2,length/2,down);
        return;
    }
    else return;
}

If you are using Java I have a nice type safe enum for directions allready coded up if you want it.

Thinking about it though, doing up when you have just done up etc. is kinda pointless as you whont see it, but if you do coloured lines dependeant on length it might look quite pretty. :D once I've done my coursework I might give this a go.

wOw the world spins?
« Last Edit: 11/04/2005 15:44:20 by Ultima »
 

The Naked Scientists Forum

Re: Fractal-sorta
« Reply #4 on: 11/04/2005 15:25:49 »

 

SMF 2.0.10 | SMF © 2015, Simple Machines
SMFAds for Free Forums