locked
A fun bug with random colors RRS feed

  • General discussion

  • I have been going threw examples from introduction and I noticed very fun bug.

    Program draws circles on random positions with random colors.

    However if you add a line for random pen width colors distribution will not be random but will take a rainbow pattern at least on my computer.

     

    Sub LinesAndRainbowColorRain
     GraphicsWindow.BackgroundColor = "Black"
     GraphicsWindow.Width = 800
     GraphicsWindow.Height = 600
     GraphicsWindow.PenWidth = 1
     For cnt = 1 To 100000
     GraphicsWindow.PenWidth = Math.GetRandomNumber(3)'try commenting out this line :)
     GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
     GraphicsWindow.DrawEllipse( Math.GetRandomNumber(640),Math.GetRandomNumber(480), 5, 5 )
     EndFor
    EndSub
     
     
    LinesAndRainbowColorRain() 
    

     


    1. Whiter than the white is UV bright! 2. Eat your vegetables or you are going to get cancer! 3. Fruits won't cut it 4 wheel drive with levers to lock differentials was a hack
    • Changed type litdev Saturday, November 6, 2010 9:23 PM
    • Edited by D3F84U Tuesday, April 7, 2015 11:12 PM
    Thursday, October 21, 2010 1:43 AM

All replies

  • very creative, Igor Feher. Though it is not my decision, could you please change your signature? little kids come on this site, and see inapropriate words written all over the place

    ~~AirWaves!!~~
    Monday, October 25, 2010 1:32 AM
  • It has been some time but hey, I will use this opportunity to post fun samples that might amuse someone. My favorite is number 6.

    Sub LinesAndRainbowColorRain
      GraphicsWindow.Clear()
      GraphicsWindow.BackgroundColor = "SteelBlue"
      GraphicsWindow.Title = "LinesAndRainbowColorRain"
      GraphicsWindow.Width = 320
      GraphicsWindow.Height = 200
      GraphicsWindow.Show()
      GraphicsWindow.PenColor = "Green"
      GraphicsWindow.DrawLine(10,10,100,100)  
      GraphicsWindow.PenWidth = 10
      GraphicsWindow.PenColor = "Gold"
      GraphicsWindow.DrawLine(10,100,100,10)  
      For cnt = 1 To 100 Step 2
        GraphicsWindow.PenWidth = cnt
        GraphicsWindow.DrawLine( 0, cnt * 10, 100, cnt * 15 )
      EndFor
      Program.Delay(800)
      GraphicsWindow.Clear()
      GraphicsWindow.BackgroundColor = "Black"
      GraphicsWindow.Width = 640
      GraphicsWindow.Height = 480
      GraphicsWindow.PenWidth = 1
      
      For cnt = 1 To 1200
        Program.Delay(1)
        GraphicsWindow.PenWidth = Math.GetRandomNumber(3)
        GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
        GraphicsWindow.DrawEllipse( Math.GetRandomNumber(640),Math.GetRandomNumber(480), 5, 5 )
        GraphicsWindow.PenWidth = 1
        GraphicsWindow.BrushColor = "Orange"
        GraphicsWindow.FillRectangle(0, 10, 40, 15)
        GraphicsWindow.BrushColor = "White"
        GraphicsWindow.DrawText(0, 10, 1200 - cnt)
      EndFor
    EndSub
    
    Sub Fractals
      GraphicsWindow.Clear()
      GraphicsWindow.Width = 900
      GraphicsWindow.Height = 900  
      GraphicsWindow.BackgroundColor = "Blue"  
      MaxFractalPoints=6
      MaxFractalDepth=6
      FractalDepth = MaxFractalDepth
      For i = 1 To 9000
        r=math.GetRandomNumber(MaxFractalPoints)
        ux=0
        uy=0
        color = "Red"
        If(r=1 )Then
          ux=600
          uy=0
          color = "Green"
        EndIf
        
        If(r=2)Then    
          ux=600
          uy=600
          color = "Blue"
        EndIf
        
        If(r=3)Then    
          ux=0
          uy=600
          color = "Yellow"
        EndIf
        
        If(r=4)Then    
          ux=800
          uy=300
          color = "White"
        EndIf
        
        If(r=5)Then    
          ux=400
          uy=300
          color = "Cyan"
        EndIf
        
        
        if(FractalDepth=MaxFractalDepth)Then
          ux=0
          uy=0
          x=ux/(MaxFractalPoints-2)
          y=uy/(MaxFractalPoints-2)
          FractalDepth=0
        EndIf  
        FractalDepth=FractalDepth+1
        
        ux=ux*(MaxFractalPoints-2)
        uy=uy*(MaxFractalPoints-2)
        x=(x+ux)/(MaxFractalPoints-1)
        y=(y+uy)/(MaxFractalPoints-1)
        
        GraphicsWindow.SetPixel(x,y,color)
      EndFor
    EndSub
    
    Sub TurtleTest
      GraphicsWindow.Clear()
      GraphicsWindow.BackgroundColor = "DarkBlue"   
      Turtle.Show()
      sides = 10
      length = 400
      AnglePhase = 36
      for  e= 1 To (360/AnglePhase)
        For i = 1 To sides
          GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
          Turtle.Turn(360/sides)
          Turtle.Move(length/sides)
        EndFor
        Turtle.Turn(AnglePhase)
      EndFor  
    EndSub  
    
    Sub TurtleTest2
      GraphicsWindow.Clear()
      GraphicsWindow.Width = 900
      GraphicsWindow.Height = 600  
      GraphicsWindow.BackgroundColor = "DarkBlue"   
      Turtle.Show()
      sides = 10
      length = 300
      for  sidescnt = 3 To sides
        For i = 1 To sidescnt
          GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
          Turtle.Turn(360/sidescnt)
          Turtle.Move(length)
        EndFor
      EndFor  
    EndSub  
    
    Sub TurtleTest3
      GraphicsWindow.Clear()
      GraphicsWindow.BackgroundColor = "DarkBlue"   
      Turtle.Show()
      sides = 10
      length = 400
      For i = 1 To sides
        GraphicsWindow.PenColor = "Yellow"
        Turtle.Turn(360/sides)
        For cnt2 = 1 to length /sides / 10
          Turtle.Move(length/sides / 5)
          Turtle.PenUp()
          Turtle.Move(length/sides / 5)
          Turtle.PenDown()
        EndFor
      EndFor
    EndSub
    
    
    Sub FractalsMouseInit
      GraphicsWindow.BackgroundColor = "Black"  
      MaxFractalPoints=0
      '  MaxFractalDepth=500
      '  FractalDepth = MaxFractalDepth
      FractalPointsX = 0
      FractalPointsY = 0
      FractalOffsetX = 0
      FractalOffsetY = 0
      FractalZoom = 1
      FractalFactor = 1.1
      
      ColorArray[Array.GetItemCount(ColorArray)+1]="IndianRed"
      ColorArray[Array.GetItemCount(ColorArray)+1]="Gold"
      ColorArray[Array.GetItemCount(ColorArray)+1]="Tomato"
      ColorArray[Array.GetItemCount(ColorArray)+1]="Pink"
      ColorArray[Array.GetItemCount(ColorArray)+1]="MediumSpringGreen"
      ColorArray[Array.GetItemCount(ColorArray)+1]="DarkKhaki"
      ColorArray[Array.GetItemCount(ColorArray)+1]="DeepPink"
      ColorArray[Array.GetItemCount(ColorArray)+1]="DarkGreen"
      ColorArray[Array.GetItemCount(ColorArray)+1]="Magenta"
      ColorArray[Array.GetItemCount(ColorArray)+1]="LightSeaGreen"
      ColorArray[Array.GetItemCount(ColorArray)+1]="SlateBlue"
      ColorArray[Array.GetItemCount(ColorArray)+1]="Turquoise"
      ColorArray[Array.GetItemCount(ColorArray)+1]="Chartreuse"
      ColorArray[Array.GetItemCount(ColorArray)+1]="LightSteelBlue"
      ColorArray[Array.GetItemCount(ColorArray)+1]="SkyBlue"
      ColorArray[Array.GetItemCount(ColorArray)+1]="Blue"
      ColorArray[Array.GetItemCount(ColorArray)+1]="Navy"
      ColorArray[Array.GetItemCount(ColorArray)+1]="SandyBrown"
      ColorArray[Array.GetItemCount(ColorArray)+1]="MistyRose"
      ColorArray[Array.GetItemCount(ColorArray)+1]="DarkSlateGray"
      ColorArray[Array.GetItemCount(ColorArray)+1]="HotPink"
    EndSub
    
    Sub FractalTranslateX
      PixelX=FractalZoom*(x+FractalOffsetX) - (GraphicsWindow.Width / 2) * (FractalZoom - 1)
    EndSub
    
    Sub FractalTranslateY
      PixelY=FractalZoom*(y+FractalOffsetY) - (GraphicsWindow.Height / 2) * (FractalZoom - 1)
    EndSub
    
    Sub FractalReverseTranslateX
      x = (PixelX + (GraphicsWindow.Width / 2) * (FractalZoom - 1)) / FractalZoom - FractalOffsetX
    EndSub
    
    Sub FractalReverseTranslateY
      y = (PixelY + (GraphicsWindow.Height / 2) * (FractalZoom - 1)) / FractalZoom - FractalOffsetY
    EndSub
    
    Sub FractalsMouse
      If MaxFractalPoints = 0 Then
        Goto ExitThisSub
      endif  
      
      r=math.GetRandomNumber(MaxFractalPoints)
      ux = FractalPointsX[r]
      uy = FractalPointsy[r]
      
      '  if FractalDepth>=MaxFractalDepth Then
      '    x=ux/(MaxFractalPoints-2)
      '    y=uy/(MaxFractalPoints-2)
      '    FractalDepth=0
      '  EndIf  
      '  FractalDepth=FractalDepth+1
      
      ux=ux*(MaxFractalPoints-2)
      uy=uy*(MaxFractalPoints-2)
      x=(x+ux)/(MaxFractalPoints-1)
      y=(y+uy)/(MaxFractalPoints-1)
      
      if r <= Array.GetItemCount(ColorArray)then
        color = ColorArray[r]
      else
        color = GraphicsWindow.GetRandomColor()
      endif
      'qx(x+px)   qy(y+py) 
      FractalTranslateX()
      FractalTranslateY()
      GraphicsWindow.SetPixel(PixelX,PixelY,color)
      ExitThisSub:  
    EndSub
    
    Sub FractalTest
      'Left Mouse button to add fractal point
      'Right Mouse button to center to point
      'Num +/- for zoom in/out
      GraphicsWindow.Width = 900
      GraphicsWindow.Height = 600  
      GraphicsWindow.MouseDown = OnMouseDown
      GraphicsWindow.KeyDown = OnKeyDown
      FractalsMouseInit()
      DrawFractalsMouse = 1
      While DrawFractalsMouse = 1
        FractalsMouse()
      EndWhile 
    EndSub
    
    Sub OnMouseDown
      PixelX = GraphicsWindow.MouseX
      FractalReverseTranslateX()
      PixelY = GraphicsWindow.MouseY
      FractalReverseTranslateY()
      If Mouse.IsRightButtonDown Then
        FractalOffsetX = GraphicsWindow.Width / 2 - x
        FractalOffsetY = GraphicsWindow.Height / 2 - y
      Else
        MaxFractalPoints=MaxFractalPoints+1  
        FractalPointsX[MaxFractalPoints] = x
        FractalPointsY[MaxFractalPoints] = y
        'FractalDepth = MaxFractalDepth
      EndIf
      
      GraphicsWindow.Clear()
    EndSub
    
    
    Sub OnKeyDown
      'TextWindow.WriteLine(GraphicsWindow.LastKey )
      If GraphicsWindow.LastKey = "Add" Then
        FractalZoom = FractalZoom * FractalFactor
      ElseIf GraphicsWindow.LastKey = "Subtract" Then
        FractalZoom = FractalZoom / FractalFactor
      EndIf 
      GraphicsWindow.Clear()
    EndSub
    
    Sub DrawTree
      NestedDrawTreeCalls = NestedDrawTreeCalls + 1
      'TextWindow.WriteLine( " Nested Call No:" + NestedDrawTreeCalls )
      If ( distance > 0 ) Then
        Turtle.Move( distance )
        Turtle.Turn( angle )
        
        Stack.PushValue( "distance", distance )
        distance = distance - delta
        DrawTree()
        Turtle.Turn( -angle * 2 )
        DrawTree()
        Turtle.Turn( angle )
        distance = Stack.PopValue( "distance" )
        
        Turtle.Move( -distance )
      EndIf
      NestedDrawTreeCalls = NestedDrawTreeCalls - 1
    EndSub
    
    Sub TurtleFractal
      angle = 30
      distance = 60
      delta = 10
      Turtle.Speed = 9
      Turtle.PenUp()
      Turtle.MoveTo(GraphicsWindow.Width / 2,GraphicsWindow.Height / 2)
      Turtle.Angle = 0
      Turtle.PenDown()
      GraphicsWindow.BackgroundColor = "Black"
      GraphicsWindow.PenColor = "LightGreen"
      DrawTree()
      NestedDrawTreeCalls = 0
    EndSub
    
    
    Sub RandomFlickrPhotos
      GraphicsWindow.BackgroundColor = "Black"
      GraphicsWindow.MouseDown = OnMouseDownFlickr
      GraphicsWindow.Width = 800
      GraphicsWindow.Height = 600
      GraphicsWindow.DrawText( GraphicsWindow.Width / 4, GraphicsWindow.Height / 2, "Right click to retrive image...")
    EndSub
    
    Sub OnMouseDownFlickr
      If Mouse.IsRightButtonDown Then
        TextWindow.ForegroundColor = "Green"
        TextWindow.WriteLine("")
        TextWindow.WriteLine("Enter search tags separated by commas:")
        FlickrSearchPhrases = TextWindow.Read()
        TextWindow.WriteLine("Trying to retrive image...")
        FlickrPicture = Flickr.GetRandomPicture( FlickrSearchPhrases )
        GraphicsWindow.Clear()
        GraphicsWindow.DrawImage( FlickrPicture, 0, 0 )
      EndIf
    EndSub
    
    
    Sub RandomFlickrDesktop
      LoopForever = 1
      While LoopForever = 1
        Desktop.SetWallPaper( Flickr.GetPictureOfMoment() )
        Program.Delay( 10000 )
      EndWhile
    EndSub
    
    Sub PaddleGame
      GraphicsWindow.BackgroundColor = "DarkBlue"
      paddle = Shapes.AddRectangle( 120, 12 )
      ball = Shapes.AddEllipse( 16, 16 )
      GraphicsWindow.MouseMove = OnMouseMovePaddle
      
      x = 0
      y = 0
      deltaX = 1
      deltaY = 1
      
      RunLoop:
      x = x + deltaX
      y = y + deltaY
      
      gw = GraphicsWindow.Width
      gh = GraphicsWindow.Height
      If( x >= gw - 16 Or x <= 0 ) Then
        deltaX = -deltaX
      EndIf
      If( y <= 0 ) Then
        deltaY = -deltaY
      EndIf
      
      padX = Shapes.GetLeft( paddle )
      If( y = gh - 28 And x >= padX And x <= padX + 120 ) Then
        deltaY = -deltaY
      EndIf
      
      Shapes.Move( ball, x, y )
      Program.Delay( 5 )
      
      If( y < gh ) Then
        Goto RunLoop
      EndIf
      
      GraphicsWindow.ShowMessage( "You Lose", "Paddle" )
      
    EndSub
    
    Sub OnMouseMovePaddle
      paddleX = GraphicsWindow.MouseX
      Shapes.Move( paddle, paddleX - 60, GraphicsWindow.Height - 12 )
    EndSub
    
    
    TextWindow.ForegroundColor = "Yellow"
    TextWindow.WriteLine("  Demos created following official MS articles.")
    TextWindow.WriteLine("")
    TextWindow.WriteLine("Select demo:")
    TextWindow.WriteLine(" 1: LinesAndRainbowColorRain")
    TextWindow.WriteLine(" 2: Fractals")
    TextWindow.WriteLine(" 3: TurtleTest 'Draws a flower")
    TextWindow.WriteLine(" 4: TurtleTest2 'From triangle up")
    TextWindow.WriteLine(" 5: TurtleTest3")
    TextWindow.WriteLine(" 6: FractalTest 'Click on several spots on graphic screen to create fractals")
    TextWindow.WriteLine(" 7: TurtleFractal")
    TextWindow.WriteLine(" 8: RandomFlickrPhotos 'Enter search term in to console")
    TextWindow.WriteLine(" 9: RandomFlickrDesktop 'Will change your desktop background image periodically")
    TextWindow.WriteLine("10: PaddleGame")
    
    
    SelectAgain:
    TextWindow.Write("Enter Selection:")
    Demo = TextWindow.Read()
    If( "1" = Demo ) Then
      LinesAndRainbowColorRain()
    ElseIf( "2" = Demo ) Then
      Fractals()
    ElseIf( "3" = Demo ) Then
      TurtleTest()
    ElseIf( "4" = Demo ) Then
      TurtleTest2()
    ElseIf( "5" = Demo ) Then
      TurtleTest3()
    ElseIf( "6" = Demo ) Then
      TextWindow.WriteLine("")
      TextWindow.WriteLine("Click on screen at least three times and watch fractal build.")
      TextWindow.WriteLine("Add more elements to fractal by clicking again.")
      Program.Delay(3000)
      FractalTest()
    ElseIf( "7" = Demo ) Then
      TurtleFractal()
    ElseIf( "8" = Demo ) Then
      RandomFlickrPhotos()
    ElseIf( "9" = Demo ) Then
      RandomFlickrDesktop()
    ElseIf( "10" = Demo ) Then
      PaddleGame()
    Else
      Goto SelectAgain
    EndIf
      


    1. Whiter than the white is UV bright! 2. Eat your vegetables or you are going to grow old faster! 3. Fruits won't cut it 4 wheel drive with levers to lock differentials was a hack


    • Edited by D3F84U Tuesday, April 7, 2015 11:15 PM
    Saturday, May 12, 2012 8:41 PM
  • I went browsing a little and actually read your post. Sorry everyone, I feel like changing all my posts with similar signatures but that is too much work. Maybe there is a way for you to do it for me automatically...

    • Edited by D3F84U Wednesday, April 8, 2015 12:50 AM
    Tuesday, April 7, 2015 11:14 PM