Small Steps II — Making Things

Draw Shapes and Text

Draw Shapes and Text

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Drawing with coordinates

What you will learn

Coordinates are numbers describing a screen position. The top-left corner is (0, 0); x increases to the right and y increases downwards.

Try it

Example · example1.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    window.clear(White);
    window.fill_circle(320, 240, 80, Yellow);
    window.draw_circle(290, 220, 10, Black);
    window.draw_circle(350, 220, 10, Black);
    window.draw_line(285, 275, 355, 275, Black);
    window.show();

    while (window.is_open())
        window.show();
}

Clear clears the background. fill_circle(320, 240, 80, Yellow) fills a yellow circle centered at (320, 240) with radius 80. draw_circle draws only its outline.

draw_line's four numbers are the starting x and y and the ending x and y. Later drawings cover earlier ones. When you see the yellow face, try changing a circle's position.

Braces can be omitted for a single statement, as with while at the end of the example. You may use braces when writing it yourself.

Adding colors and text

What you will learn

rgb makes a color by mixing the intensities of red, green, and blue. Each value ranges from 0 to 255.

Try it

Example · drawing_2_example2.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    Color orange = rgb(255, 140, 0);

    window.clear(White);
    window.fill_rectangle(60, 80, 180, 120, orange);
    window.draw_rectangle(60, 80, 180, 120, Black);
    window.draw_text(80, 110, "Small C++", Blue, 24);
    window.show();

    while (window.is_open())
        window.show();
}

Color is the type that stores a color; rgb is a function that creates and returns one.

fill_rectangle takes the top-left x and y and the width and height. draw_rectangle draws only the outline. draw_text takes x, y, a string, a color, and a font size. Check that the text appears over the orange rectangle.

Exercise

Draw a traffic light with three vertically arranged Red, Yellow, and Green circles inside a black or Gray rectangle.

Exercise starter · exercise1_starter.cpp
void small_main()
{
    Window window;
    window.open(300, 500);

    window.clear(White);

    // Draw a traffic light here.

    window.show();
    while (window.is_open())
        window.show();
}

Hint

First draw the body with fill_rectangle, then use fill_circle three times.

show solution
void small_main()
{
    Window window;
    window.open(300, 500);

    window.clear(White);
    window.fill_rectangle(75, 30, 150, 420, Gray);
    window.fill_circle(150, 110, 50, Red);
    window.fill_circle(150, 240, 50, Yellow);
    window.fill_circle(150, 370, 50, Green);
    window.show();

    while (window.is_open())
        window.show();
}
Try More (Optional)

Exercise

Draw a simple face with circles and lines. Include two eyes and a mouth. Choose any colors and positions.

Exercise starter · drawing_2_exercise2_starter.cpp
void small_main()
{
    Window window;
    window.open(500, 500);

    window.clear(White);

    // Draw your face here.

    window.show();
    while (window.is_open())
        window.show();
}

Hint

Make a face with one large fill_circle, then add two small circles and a draw_line.

show solution
void small_main()
{
    Window window;
    window.open(500, 500);

    window.clear(White);
    window.fill_circle(250, 250, 160, Yellow);
    window.fill_circle(195, 210, 18, Black);
    window.fill_circle(305, 210, 18, Black);
    window.draw_line(190, 315, 310, 315, Black);
    window.show();

    while (window.is_open())
        window.show();
}
← Previous lessonNext lesson →