Mandlebrot (w/ zoom) 
	 
	
		| Author | 
		Message | 
	 
		 
		TheOneTrueGod
 
  
 
    
		 | 
		
		
			
				  Posted: Wed May 03, 2006 1:56 pm    Post subject: Mandlebrot (w/ zoom)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				I searched around, and I found an old post on this as well, but I decided to create this anyway, as the old version didn't have zooming capabilities (as far as in-program went, anyway)
 
 
Warning: Turing cannot handle anything zoomed in too far.  The window might start going kinda funky, and unexpected behaviour may develop.
 
 
 
	  | code: | 	 		  
 
const maxdepth := 50 %Change this if you plan on zooming in farther than, say, 3 medium zooms.  The picture will become clearer,
 
%but take longer to load.
 
var left : real := -2
 
var right : real := 2
 
var bottom : real := -2
 
var top : real := 2
 
 
type CoOrd :
 
    record
 
        a, b : real
 
    end record
 
 
function SquareIt (z : CoOrd) : CoOrd
 
    var z2 : CoOrd
 
 
    z2.a := 2 * z.a * z.b
 
    z2.b := z.b ** 2 - z.a ** 2
 
    result z2
 
end SquareIt
 
 
function AddIt (z, z3 : CoOrd) : CoOrd
 
    var z2 : CoOrd
 
    z2.a := z.a + z3.a
 
    z2.b := z.b + z3.b
 
    result z2
 
end AddIt
 
 
function FindColour (z : CoOrd) : int
 
    var z2 : CoOrd := z
 
    for i : 0 .. maxdepth
 
        z2 := SquareIt (z2)
 
        z2 := AddIt (z2, z)
 
        if sqrt (z2.a ** 2 + z2.b ** 2) > 2 then
 
            result i
 
        end if
 
    end for
 
    result maxdepth
 
end FindColour
 
 
var z : CoOrd
 
var mx, my, button : int
 
var mandlepic : int
 
var newleft, newright, newtop, newbottom : real
 
loop
 
    View.Set ('nooffscreenonly')
 
    for j : 0 .. maxy
 
        for i : 0 .. maxx
 
            z.a := (top - bottom) * j / maxy + bottom
 
            z.b := (right - left) * i / maxx + left
 
            drawdot (i, j, round (FindColour (z) / maxdepth * 254) + 1)
 
        end for
 
    end for
 
    View.Set ('offscreenonly')
 
    mandlepic := Pic.New (0, 0, maxx, maxy)
 
    loop
 
        mousewhere (mx, my, button)
 
        if button = 1 then
 
            newleft := mx
 
            newbottom := my
 
            loop
 
                mousewhere (mx, my, button)
 
                Pic.Draw (mandlepic, 0, 0, picCopy)
 
                drawbox (round (newleft), round (newbottom), mx, my, brightblue)
 
                View.Update
 
                cls
 
                exit when button = 0
 
            end loop
 
            exit when mx > newleft and my > newbottom
 
        end if
 
    end loop
 
    Pic.Free (mandlepic)
 
    newtop := (top - bottom) * my / maxy + bottom
 
    newbottom := (top - bottom) * newbottom / maxy + bottom
 
 
    newright := (right - left) * mx / maxx + left
 
    newleft := (right - left) * newleft / maxx + left
 
 
    left := newleft
 
    right := newright
 
    bottom := newbottom
 
    top := newtop
 
end loop
 
  | 	 
  | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
		 
		Sponsor Sponsor 
		 
  
		 | 
		
 | 
	 
	 
		  | 
	 
				 
		Clayton
 
  
 
    
		 | 
		
		
			
				  Posted: Wed May 03, 2006 5:01 pm    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| looks good, but i dont quite understand what you mean by "zoom", all i can do is draw a box and nothing happens, is that supposed to happen?(thats what it looks like from the code, but i cant be sure...) gj otherwise | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		TheOneTrueGod
 
  
 
    
		 | 
		
		
			
				  Posted: Wed May 03, 2006 8:15 pm    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				you have to draw the box from bottom left to top right, or else it won't work:
 
|----------------finish
 
|                     |
 
|                     |
 
|                     |
 
start ------------| | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		Clayton
 
  
 
    
		 | 
		
		
			
				  Posted: Wed May 03, 2006 8:39 pm    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| ok i got it now, very cool, although that thing with the box not being able to be drawn top to bottom... not good, you need to make it so that you can draw the box any way and end up with the same result, gj otherwise, i understand what youve typed, but i never would have been able to come up with that on my own, gj | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		codemage
 
  
 
    
		 | 
		
		
			
				  Posted: Thu May 04, 2006 7:52 am    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| It wouldn't take much to fix that bug.  Just check which of the y-click coordinates are lower, which of your x-click coords are leftmost, etc... | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		 | 
	 
 
	
	
	 
	
	 |