1.2 - Graphic features

This tutorial will illustrate the artistic capabilities of Agoid.
You will learn to draw some fun pieces of art using colors, pen size, shapes and texts.
Enjoy it!

synopsis

  1. prerequisite
  2. basics
  3. show / hide
  4. colors
  5. turtle visibility
  6. pen size
  7. more shapes
  8. it is your turn to play
  9. summary

  10. << back to tu(r)torials

prerequisite

Before reading this tutorial, it is important to have a good knowledge of: 1.1 - What is a program ?

basics

What is a library ?
All programming languages are divided in two major kinds of instructions :

  • primitives, which will be described in detail in next tutorials
  • libraries

Libraries are collections of tools used to do perform complex tasks.
In the case of AL all these tools are contained in functions which are themselves contained in packages.
AL has several packages like Math (for some arithmetic calculations), Util, UI, .... and Algo.
The Algo package contains all the tools needed to draw graphics with turtle and more besides.

show / hide

In tutorial 1.1 we saw how to go forward and draw a line with the algo.go command.
It is also possible to move the same way but without drawing anything using the algo.jump command.
Look at what the following code does:

 
Algo jump: 
  • 1
  • 2
  • 3
algo.go (70);
algo.jump (70);
algo.go (70);

colors

The Algo package provide 2 properties with which to change colors.
The foreground color uses the property algo.setColor, which changes the color of the next line (or other shapes).
The background color uses the property algo.setBgColor.
Both color properties use the same table of numbers, where each number correspond to a color as follows:

color value color value
transp.-1   
black0 dark gray8
dark blue1 blue9
dark green2 green10
dark cyan3 cyan11
dark red4 red12
dark magenta5 magenta13
brown6 yellow14
light gray7 white15


An example of color handling:

 
Algo colors: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
algo.setBgColor (11); // dark cyan
algo.setColor(3); // cyan
// our square
algo.go (100);
algo.turnRight (90);
algo.go (100);
algo.turnRight (90);
algo.go (100);
algo.turnRight (90);
algo.go (100);
algo.turnRight (90);

turtle visibility

It is possible to hide and show the turtle again using algo.hide and algo.show.

pen size

To change the size of the future drawing stroke, use the algo.setStroke property.
Numbers greater than 1 will be thicker than the default stroke.
Numbers smaller than 1 (e.g. 0.2, 0.5) will be thinner than the default stroke.

 
Algo stroke: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
algo.setStroke (0.2); // thinest
algo.go (25);
algo.setStroke (1); // thicker
algo.go (25);
algo.setStroke (2); // and thicker
algo.go (25);
algo.setStroke (3); // ....
algo.go (25);
algo.setStroke (4); // ....
algo.go (25);
algo.setStroke (5); // ....
algo.go (25);
algo.hide ();

more shapes

circle

Draw a circle with a specified diameter.

 
Shape: 
  • 1
algo.circle (200);

disc

Same as circle but filled.

 
Shape: 
  • 1
algo.disc (200);

oval

Draw an oval with specified x radius and y radius.

 
Shape: 
  • 1
algo.oval (400, 200);

platter

Same as oval but filled.

 
Shape: 
  • 1
algo.platter (400, 200);

rectangle

Draw a rectangle with particular width and height.

 
Shape: 
  • 1
algo.rect (400, 200);

plane

Same as rectangle but filled.

 
Shape: 
  • 1
algo.plane (400, 200);

text

Draw a text message.

 
Shape: 
  • 1
  • 2
algo.text ("hello algoid !");
algo.jump (-100);

it is your turn to play

ex1: modify our square

By modifying the following piece of code (our square code), try to obtain the following sketch:

 
Ex1: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
// left
algo.go (100);
algo.turnRight (90);
// top
algo.go (100);
algo.turnRight (90);
// right
algo.go (100);
algo.turnRight (90);
// bottom
algo.go (100);
algo.turnRight (90);
 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
// left
algo.go (100);
algo.turnRight (90);
// top
algo.jump (100); // modify here
algo.turnRight (90);
// right
algo.go (100);
algo.turnRight (90);
// bottom
algo.jump (100); // and here and thats all
algo.turnRight (90);

ex2: target

Try to draw the following figure :

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
algo.disc (400);
algo.setColor (0); // dark
algo.disc (300);
algo.setColor (7); // light
algo.disc (200);
algo.setColor (0); // dark
algo.disc (100);

Try it with oval :

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
algo.platter (400, 300);
algo.setColor (0); // dark
algo.platter (300, 200);
algo.setColor (7); // light
algo.platter (200, 100);

And rectangle :

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
algo.plane (400, 300);
algo.setColor (0); // dark
algo.plane (300, 200);
algo.setColor (7); // light
algo.plane (200, 100);

ex3: circles

Try to draw the following figure :

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
algo.circle (100);
algo.jump (50);
algo.circle (200);
algo.jump (100);
algo.circle (400);
algo.jump (200);
algo.circle (800);
algo.jump (400);
algo.circle (1600);
 

ex4: triangle definition

Write a script to draw the following figure:
Tip : The angle at each corner of the triangle is 360° / 3 sides = 120°

 
Solution: 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
// initialize turtle s position
algo.turnLeft (90);
// the begin of the bottom side
algo.go (50);
algo.circle (200);
algo.turnRight (120); // 360 / 3
// side 2
algo.go (100);
algo.circle (200);
algo.turnRight (120);
// side 3
algo.go (100);
algo.circle (200);
algo.turnRight (120);
// finnish the bottom side
algo.go (50);
algo.hide ();


summary

Summary

In this tutorial, you have learned:

  • what are libraries ?
  • the main functions in algo library.
  • how to do some fun drawings.

Functions used

FunctionDescription