Serpinsky Triangle with recursion 
	 
	
		| Author | 
		Message | 
	 
		 
		BenLi
 
  
 
    
		 | 
		
		
			
				  Posted: Tue May 02, 2006 3:17 pm    Post subject: Serpinsky Triangle with recursion  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				Hey,
 
this is my first attempt at recursion, probably going to make a fractal tree next
 
 
	  | code: | 	 		  
 
setscreen ("graphics:800;600")
 
%serpinsky
 
var middlex, middley:int
 
%var x1,y1,x2,y2,x3,y3:int
 
 
fcn midx (x1, x2:int):int
 
middlex:= (((x2 - x1)div 2)+ x1)
 
result middlex
 
end midx
 
 
fcn midy (y1, y2:int):int
 
middley:= (((y2 - y1)div 2)+ y1)
 
result middley
 
end midy
 
 
proc serpinsky (x1, y1, x2, y2, x3, y3:int) 
 
drawline (x1, y1, x2, y2, black)
 
drawline (x2, y2, x3, y3, black)
 
drawline (x1, y1, x3, y3, black)
 
 
drawline (midx (x1, x2), midy ( y1, y2), midx (x1, x3), midy ( y1, y3), black)
 
drawline (midx (x1, x2), midy ( y1, y2), midx (x2, x3), midy ( y2, y3), black)
 
drawline (midx (x1, x3), midy ( y1, y3), midx (x2, x3), midy ( y2, y3), black)
 
 
serpinsky (midx (x1, x2), midy ( y1, y2),midx (x1, x3), midy ( y1, y3),midx (x2, x3), midy ( y2, y3))
 
end serpinsky
 
serpinsky (0, 0, 400, 600, maxx, 0)
 
  | 	  
 
 
actually, its not actually a serpinsky yet... | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
		 
		Sponsor Sponsor 
		 
  
		 | 
		
 | 
	 
	 
		  | 
	 
				 
		zylum
 
  
 
    
		 | 
		
		
			
				  Posted: Tue May 02, 2006 3:54 pm    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				suggestion: make sure you have a terminating condition for your recursion. if you let your program run long enough, it eats up all the memory and crashes.
 
 
also, like you mentioned, its not quite a serpinsky triangle. an easy way to make a serpinsky triangle is to draw your beginning triangle, then half way between its centre and each of its vertices, draw another one half its size.
 
 
	  | code: | 	 		  setscreen ("graphics:600;600")
 
 
proc triangle (cx, cy, s : real, d : int)
 
    if d = 0 then
 
        return
 
    end if
 
    var x, y : array 1 .. 3 of int
 
    for i : 1 .. 3
 
        x (i) := round (cosd (i * 120 - 30) * s + cx)
 
        y (i) := round (sind (i * 120 - 30) * s + cy)
 
        triangle (cosd (i * 120 - 30) * (s / 2) + cx, sind (i * 120 - 30) * (s / 2) + cy, s / 2, d - 1)
 
    end for
 
    drawpolygon (x, y, 3, black)
 
    delay (10)
 
end triangle
 
 
triangle (maxx div 2, maxy div 3, 340, 7)  | 	  
 
 
good luck with your fractal tree and recursion     there are a few good source codes for fractal trees. also take a look at my recursion tutorial in turing tutorials    | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		MysticVegeta
 
  
 
    
		 | 
		
		
			
				  Posted: Tue May 02, 2006 5:43 pm    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| zylum, I haven't taken grade 11 trig yet, can you guide me over what is happening in the draw polygon? I understand the recursion but I dont understand how you come up with values for the x and y array, I know they represent the vertices of the triangle, but dont know whats going on in them, lol | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		HellblazerX
 
  
 
    
		 | 
		
		
			
				  Posted: Tue May 02, 2006 7:24 pm    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				| First of all, drawpolygon takes in an array of coordinates to draw the polygon.  A triangle has three points, so he puts in an array of 3 x coordinates and an array of 3 y coordinates to draw the triangle.  To get the x and y coordinates, he just takes the trigonometric values of them in the unit circle, and mulitplying by the size of the triangle.  This way he can resize the triangles very easily. | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		zylum
 
  
 
    
		 | 
		
		
			
				  Posted: Tue May 02, 2006 9:00 pm    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				what ^he^ said    | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		MysticVegeta
 
  
 
    
		 | 
		
		
			
				  Posted: Wed May 03, 2006 1:39 pm    Post subject: (No subject)  | 
	
				
				 | 
			 
			 
				
  | 
			 
			
				HellblazerX wrote: First of all, drawpolygon takes in an array of coordinates to draw the polygon.  A triangle has three points, so he puts in an array of 3 x coordinates and an array of 3 y coordinates to draw the triangle.  To get the x and y coordinates, he just takes the trigonometric values of them in the unit circle, and mulitplying by the size of the triangle.  This way he can resize the triangles very easily. 
 
 
yeah I know that but I asked, how to "come up" withthe x and y vals, just the trig part I have a hard time understanding... | 
			 
			
				 | 
			 
		  | 
	 
	 
		 | 
		
		 | 
	 
	  
		  | 
	 
				 
		 | 
	 
 
	
	
	 
	
	 |