Pascal Triangle

BlitzPlus Forums/BlitzPlus Beginners Area/Pascal Triangle

diceman(Posted 2005) [#1]
My first steps in a new language . . . ;)

It's actually too small to post it in the showcase-thread, but here's my calculation of the Pascal-Triangle. I don't know, if you guys are familiar with the concept, so I'll explain it, because it's quite cool:

The program puts a random triangle on screen. Be sure, that it is big enough, because if it's too tight, you won't notice the cool effect. Then the calculation starts: The program begins somewhere outside this triangle and puts a random plot on screen, then it looks for a random aiming-point (one of the three corner marks that form the initial triangle), draws an imaginary line to this point and puts a plot on exactly the half of this line. From this new current positon it looks again for an aiming point, this may be the same as before or another one of the corner marks, and so on. Here's the code:


x_max=1024	;Determine Resolution
y_max=768

Dim x_start(3),y_start(3) 	;Reserve space for the coordinates for three points, that will form
							;the initial triangle
Graphics x_max,y_max
SetBuffer DesktopBuffer()	;We only need to draw on the visible screen, as all the graphics are
							;cumulative

SeedRnd MilliSecs()			;Make true random numbers

For lauf=0 To 2				;Create coordinates for the initial triangle

	x_start(lauf)=Rnd(0,x_max-1)	;-1, to ensure, the startpoint for the calculation will be
									;outside the triangle
	y_start(lauf)=Rnd(0,y_max-1)
	Plot x_start(lauf),y_start(lauf)	;draw the corner-marks
Next

;	Activate the following lines, if you want the triangle to be actually drawn
;	Line x_start(0),y_start(0),x_start(1),y_start(1)
;	Line x_start(1),y_start(1),x_start(2),y_start(2)
;	Line x_start(2),y_start(2),x_start(0),y_start(0)

det_start=Rnd(0,3)	;the start-point for the calculation will
					;be on one of the four sides of the screen

Select det_start
	Case 0
		x=Rnd(0,x_max)
		y=0
	Case 1
		x=0
		y=Rnd(0,y_max)
	Case 2
		x=Rnd(0,x_max)
		y=600
	Case 3
		x=800
		y=Rnd(0,y_max)
End Select

Repeat				;The actual calculation

	Color Rnd(255),Rnd(255),Rnd(255)	;Use random colors
	Plot x,y		;Draw a plot at the current position
	
	newdir=Rnd(0,2)	;Determine a new random direction to one of the three corner marks of the
					;initial triangle
					
	x=(x+x_start(newdir))/2	;Draw an imaginary line from the actual position to the determined
							;corner mark, and set the new current position at the half of this
							;line
	y=(y+y_start(newdir))/2
	
Until KeyHit(1) ;Repeat until ESCAPE has been hit



diceman(Posted 2005) [#2]
Mind, that the display is not 100% perfect, because there are a few round-off errors, when the program halves the line to set up the new position, and the coordinates for drawing a plot are always integer numbers.