Small Basic Getting Started Guide: Chapter 7: Fun with Shapes

Small Basic > Getting Started Guide > Chapter 7: Fun with Shapes

We’re going to have some fun in this chapter with whatever we’ve learned so far. This chapter contains samples that show some interesting ways of combining all that you’ve learned so far to create some cool looking programs.

Rectangalore

Here we draw multiple rectangles in a loop, with increasing size.

GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.PenColor = "LightBlue"
GraphicsWindow.Width = 200
GraphicsWindow.Height = 200
 
For i = 1 To 100 Step 5
  GraphicsWindow.DrawRectangle(100 - i, 100 - i, i * 2, i * 2)
EndFor 

http://msdn.microsoft.com/gg605125.Rectangalore(en-us,MSDN.10).jpg
Figure 7.1 - Rectangalore

Circtacular

A variant of the previous program draws circles instead of squares.

GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.PenColor = "LightGreen"
GraphicsWindow.Width = 200
GraphicsWindow.Height = 200
 
For i = 1 To 100 Step 5
  GraphicsWindow.DrawEllipse(100 - i, 100 - i, i * 2, i * 2)
EndFor

 

http://msdn.microsoft.com/gg605125.Circtacular(en-us,MSDN.10).jpg
Figure 7.2 - Circtacular

Randomize

This program uses the operation GraphicsWindow.GetRandomColor to set random colors for the brush and then uses Math.GetRandomNumber to set the x and y co-ordinates for the circles. These two operations can be combined in interesting ways to create interesting programs that give different results each time they are run.

GraphicsWindow.BackgroundColor = "Black"
For i = 1 To 1000
  GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
  x = Math.GetRandomNumber(640)
  y = Math.GetRandomNumber(480)
  GraphicsWindow.FillEllipse(x, y, 10, 10)
EndFor

 

http://msdn.microsoft.com/gg605125.Randomize(en-us,MSDN.10).jpg
Figure 7.3 - Randomize

Fractals

The following program draws a simple triangle fractal using random numbers. A fractal is a geometric shape that can be subdivided into parts, each of which resembles the parent shape accurately. In this case, the program draws hundreds of triangles each of which resembles its parent triangle. And since the program runs for a few seconds, you can actually see the triangles forming slowly from mere dots. The logic itself is somewhat hard to describe and I’ll leave it as an exercise for you to explore.

GraphicsWindow.BackgroundColor = "Black"
x = 100
y = 100
 
For i = 1 To 100000
  r = Math.GetRandomNumber(3)
  ux = 150
  uy = 30
  If (r = 1) then
    ux = 30
    uy = 1000
  EndIf
   
  If (r = 2) Then
    ux = 1000
    uy = 1000
  EndIf
   
  x = (x + ux) / 2
  y = (y + uy) / 2 
   
  GraphicsWindow.SetPixel(x, y, "LightGreen")
EndFor

 

http://msdn.microsoft.com/gg605125.Triangle_Fractal(en-us,MSDN.10).jpg
Figure 7.4 - Triangle Fractal

If you want to really see the dots slowly forming the fractal, you can introduce a delay in the loop by using the **Program.**Delay operation. This operation takes in a number that specifies in milliseconds, how long to delay. Here’s the modified program, with the modified line in bold.

GraphicsWindow.BackgroundColor = "Black"
x = 100
y = 100
 
For i = 1 To 100000
  r = Math.GetRandomNumber(3)
  ux = 150
  uy = 30
  If (r = 1) then
    ux = 30
    uy = 1000
  EndIf
   
  If (r = 2) Then
    ux = 1000
    uy = 1000
  EndIf
   
  x = (x + ux) / 2
  y = (y + uy) / 2
   
  GraphicsWindow.SetPixel(x, y, "LightGreen")
  Program.Delay(2)
EndFor

Increasing the delay will make the program slower. Experiment with the numbers to see what’s best for your taste.

Another modification you can make to this program is to replace the following line:

GraphicsWindow.SetPixel(x, y, "LightGreen")
 
with
 
color = GraphicsWindow.GetRandomColor()
GraphicsWindow.SetPixel(x, y, color)

This change will make the program draw the pixels of the triangle using random colors.

Next Chapter