How To Draw Random Shapes

This intermediate level how to, walks you through creating random shapes. This is a neat, relatively simple how to that may teach you how to create a screan saver or add random selection to your project.

howtodrawrandomshapes

Level

Intermediate

Details

In the previous how to, Draw primitive shapes we saw how easy it was to . To randomize the drawing of a shape we simple add a random function and a switch statement to test the result.

Follow the code below and change the RefreshScreen rate to alter the result.




Source Code Widget

  • program HowToDrawRandomShape;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        i : Integer;
    begin
        OpenGraphicsWindow('How To Draw Random Shape' ,800 ,600 );
        LoadDefaultColors();
        ClearScreen(ColorWhite );
        repeat
            ProcessEvents();
            i := Rnd(7 );
        case i  of
            0 :     DrawCircle(RandomColor()  ,Rnd(800 )  ,Rnd(600 )  ,Rnd(300 )  )
            1 :     DrawEllipse(RandomColor()  ,Rnd(800 )  ,Rnd(600 )  ,Rnd(800 )  ,Rnd(600 )  )
            2 :     DrawHorizontalLine(RandomColor()  ,Rnd(600 )  ,Rnd(800 )  ,Rnd(800 )  )
            3 :     DrawLine(RandomColor()  ,Rnd(800 )  ,Rnd(600 )  ,Rnd(800 )  ,Rnd(600 )  )
            4 :     DrawRectangle(RandomColor()  ,Rnd(800 )  ,Rnd(600 )  ,Rnd(800 )  ,Rnd(600 )  )
            5 :     DrawTriangle(RandomColor()  ,Rnd(800 )  ,Rnd(600 )  ,Rnd(800 )  ,Rnd(600 )  ,Rnd(800 )  ,Rnd(600 )  )
            6 :     DrawVerticalLine(RandomColor()  ,Rnd(800 )  ,Rnd(600 )  ,Rnd(600 )  )
        
        end;
            RefreshScreen(60 );
        
        until WindowCloseRequested()  or KeyTyped(vk_ESCAPE )  or KeyTyped(vk_q );
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        int32_t  i;
        open_graphics_window("How To Draw Random Shape" ,800 ,600 );
        load_default_colors();
        clear_screen(color_white );
        do 
        {
            process_events();
            i = rnd(7 ) ;
        switch (i )
        {
            case 0 : 
                draw_circle(random_color() ,rnd(800 ) ,rnd(600 ) ,rnd(300 ) );
            case 1 : 
                draw_ellipse(random_color() ,rnd(800 ) ,rnd(600 ) ,rnd(800 ) ,rnd(600 ) );
            case 2 : 
                draw_horizontal_line(random_color() ,rnd(600 ) ,rnd(800 ) ,rnd(800 ) );
            case 3 : 
                draw_line(random_color() ,rnd(800 ) ,rnd(600 ) ,rnd(800 ) ,rnd(600 ) );
            case 4 : 
                draw_rectangle(random_color() ,rnd(800 ) ,rnd(600 ) ,rnd(800 ) ,rnd(600 ) );
            case 5 : 
                draw_triangle(random_color() ,rnd(800 ) ,rnd(600 ) ,rnd(800 ) ,rnd(600 ) ,rnd(800 ) ,rnd(600 ) );
            case 6 : 
                draw_vertical_line(random_color() ,rnd(800 ) ,rnd(600 ) ,rnd(600 ) );
        
        }
            refresh_screen(60 );
        }
        while ( ! (window_close_requested() || key_typed(VK_ESCAPE ) || key_typed(VK_Q ) ) );
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon


Possibilities

  • In a new project construct a shape fro, primitives , such as a stick figure, a train or a house. Ensure the measurements are dynamic and run the program where the shape is drawn in random sizes.