Three ways to draw a square
One way is to work out the co-ordinates of the four corners, move the
pen to an initial position at one of them, then draw a line to each
of the others in turn.
#include "library.h"
void main()
{ make_window(500, 500);
move_to(100, 100);
draw_to(400, 100);
draw_to(400, 400);
draw_to(100, 400);
draw_to(100, 100); }
An alternative method would be to work out where one corner is, and decide
on the length of each side. Move the pen to the one known corner, then
draw four relatively positioned lines: first increasing x, then increasing
y, then decreasing x, and finally decreasing y.
#include "library.h"
void main()
{ make_window(500, 500);
move_to(100, 100);
draw_relative(300, 0);
draw_relative(0, 300);
draw_relative(-300, 0);
draw_relative(0, -300); }
That is a better method because it can draw a square from any starting point
without substantial change. We could make the four line-drawing instructions
into a new function, and use that three times to draw three squares.
#include "library.h"
void draw_a_square()
{ draw_relative(300, 0);
draw_relative(0, 300);
draw_relative(-300, 0);
draw_relative(0, -300); }
void main()
{ make_window(700, 700);
move_to(100, 100);
draw_a_square();
move_relative(20, 40);
draw_a_square();
move_relative(20, 40);
draw_a_square(); }
The sequence of instructions that draws three squares slightly
offset from each other could itself be made into a separate
function, easily allowing us to make a pattern of nine squares.
#include "library.h"
void draw_a_square()
{ draw_relative(300, 0);
draw_relative(0, 300);
draw_relative(-300, 0);
draw_relative(0, -300); }
void draw_three_squares()
{ draw_a_square();
move_relative(20, 40);
draw_a_square();
move_relative(20, 40);
draw_a_square(); }
void main()
{ make_window(700, 700);
move_to(100, 100);
draw_three_squares();
move_to(200, 100);
draw_three_squares();
move_to(300, 100);
draw_three_squares();
move_to(400, 100); }
A third way of drawing a square would be to draw a line of the desired
length, then turn by 90 degrees, and repeat four times.
#include "library.h"
void main()
{ make_window(700, 700);
move_to(100, 350);
set_heading_degrees(45);
draw_distance(300);
turn_right_by_degrees(90);
draw_distance(300);
turn_right_by_degrees(90);
draw_distance(300);
turn_right_by_degrees(90);
draw_distance(300); }
This is even more flexible, because it could draw a square at any position
and at any orientation. We can make the basic square-drawing part into
its own function, and use it to draw all sorts of squares at all sorts
of positions.
#include "library.h"
void draw_a_square()
{ draw_distance(300);
turn_right_by_degrees(90);
draw_distance(300);
turn_right_by_degrees(90);
draw_distance(300);
turn_right_by_degrees(90);
draw_distance(300); }
void main()
{ make_window(700, 700);
move_to(100, 350);
set_heading_degrees(45);
draw_a_square();
move_relative(200, 0);
set_heading_degrees(10);
draw_a_square();
move_relative(100, -200);
set_heading_degrees(130);
draw_a_square(); }