# Pascal graphics

usesGraphABC;

begin

...

end.

The coordinate system in Pascal corresponds to the screen coordinate system. Here's what it looks like:

## How to manage color

To use a color, you need to apply it to the pen tool:

Sets the color of the pen that creates the color parameter. | SetPenColor(color) |

Sets the color of the brush, which is specified by the color parameter. | setBrushColor(color) |

For the RGB palette or for the fill. | SetPenColor(rgb(0-255, 0-255, 0-255)) |

Fills an area of one color with the specified color from the dot (x, y). | FloodFill(x,y,color) |

You can then use the procedures to draw various geometric shapes.

## Basic procedures (commands)

Color number | Constant name | Color |

0 | Black | Black |

1 | Blue | Sapphire |

2 | Green | Dark Green |

3 | Cyan | Turquoise |

4 | Red | Red |

5 | Magenta | Violet |

6 | Brown | Brown |

7 | LightGray | Light gray |

8 | DarkGray | Dark Grey |

9 | LightBlue | Blue |

10 | LightGreen | Light Green |

11 | LightCyan | Light turquoise |

12 | LightRed | Pink |

13 | LightMagenta | Raspberry |

14 | Yellow | Yellow |

15 | White | White |

For example, the color of the lines on the screen can be set as follows:

1 SetColor(14); Will be yellow

## Set the background color

In order to set the background color of the entire screen, the following procedure is used:

1 SetBkColor(14);

**Attention!** If it is not called, the screen will be black.

## Points, segments and broken

In Pascal, the following procedure is used to display the point:

SetPixel(x,y,color) — Paints a single pixel with (x,y) coordinates with color

usesGraphABC;

begin

SetPixel(300,200,clred);

end.

To draw a line, use:

Line(x1,y1,x2,y2) — draws a segment with the beginning (x1,y1) and the end in x2,y2

usesGraphABC;

begin

SetPenColor(clgreen);

line(100,50,500,250);

end.

To draw broken lines, use the following procedures: MoveTo (x1, y1) and LineTo (x2, y2).

The procedures work together: MoveTo moves the cursor to the desired point, and the LineTo procedure draws a line from that point to the point defined by the procedure parameter.

usesGraphABC;

begin

...

SetPenColor(clblue);

MoveTo(x1, y1);

LineTo(x2, y2);

LineTo(x3, y3);

LineTo(x4, y4);

LineTo(x5, y5);

end.

**Task**. Draw a square and an equilateral triangle using SetPenColor(), LineTo (x2, y2), and MoveTo (x1, y1).

To set the dimensions of the graphical window, use the procedure

SetWindowSize(width, height)

or, for example:

SetWindowWidth(600);

SetWindowHeight(400);

## Draw shapes

### The rectangle in Pascal is drawn:

Rectangle(x1,y1,x2,y2) — draws a rectangle with the coordinates of opposite vertices (x1,y1) and (x2,y2).

usesGraphABC;

begin

Rectangle(50,50,200,200);

end.

### Filled shapes:

usesGraphABC;

begin

Rectangle(50,50,200,200);

FloodFill(100,100,clBlue);

end.

### The triangle is drawn by the following procedures:

Line(x1,y1,x2,y2);

LineTo(x,y);

usesGraphABC;

begin

setpenwidth(20);

setpencolor(clred);

moveTo(300,100);

lineTo(500,300);

lineto(100,300);

lineto(300,100);

floodfill(300,200,clgreen);

end.

### Circle

With this procedure, you can draw a circle:

Circle(x,y,r) — draws a circle centered at point (x,y) and radius r.

usesGraphABC;

begin

Circle(500,200,100);

FloodFill(500,200,clred);

end.

### Arc of the circle

Arc(x,y,r,a1,a2) — allows you to draw an arc of a circle centered at the point (x,y) and radius r enclosed between two rays forming angles a1 and a2 with an axis of OX (a1 and a2 are real, set in degrees and counted counterclockwise).

usesGraphABC;

Begin

SetPenWidth(10);

Arc(300,250,150,45,135);

end.

**Task 1: **"Frog"

**Task 2: **Corona

## Random function for using coloring

SetPenColor (rgb(random(256), random(256), random(256))); — selects a random number from a 256-color palette for the colors: red, green, and blue.

**Task 3:** Draw a horizontal row of circles with a radius of 10 at a distance of 100 from the top edge of the screen and with such horizontal coordinates as 50, 80, 110, 140, ... , 290.

* Circles must be painted with a random color.

**Task 4:** "Circles on the Water".

Condition: Draw several dozen concentric circles (circles of different radii with a common center).

**Task 5:** Play the image using the application:

## Hatching

You can draw a hatch on Pascal by using the procedures for drawing a rectangle and a line:

The result is the following code:

usesgraphABC;

var i, x1, x2, y1, y2, N:integer;

h, x:real;

begin

x1 :=100; y1 :=100;

x2 :=300; y2 :=200;

N :=10;

Rectangle(x1, y1, x2, y2);

h :=(x2 — x1)/(N +1);

x := x1 + h;

for i:=1to N dobegin

Line(round(x), y1,round(x), y2);

x := x + h;

end;

end.

## Animation in Pascal

Animation in Pascal consists in the fact that the figure is drawn first with a colored tool, after which the same figure is drawn with the same coordinates in white. Then the shape is shifted. And so the actions are repeated.

Reproduce the movement of the circle horizontally. Example:

usesGraphABC;

var x:integer;

begin

x:=40;

repeat

SetPenColor(clWhite);

Circle(x,100,10); {Drawing a white circle}

SetPenColor(clBlack);

Circle(x,100,10); {Drawing a black circle}

x:=x+1 {Moving a little to the right}

until x>600;

end.

**Task 6:** Create a square tween along a given path:

How are you doing with graphics in Pascal? Share your experience by leaving a comment.