Presentation on the topic Pascal ABC graphics mode. Presentation on the topic "Graphics in ABC Pascal"

Graphics in the programming environment

Turbo Pascal

EADC teacher: Neverova I.Yu.


Lesson plan:

  • Features of connecting the graphic mode in the Turbo Pascal programming environment
  • Drawing up the program structure
  • Calculation of coordinates for procedures
  • Features of drawing with outline and color filling
  • Review of sample programs

I.G. Semakin, A.P. Shestakov. Fundamentals of Programming, pp.88-98, 398-409.


Graphics capabilities of the Turbo Pascal language - Graph library

  • Connecting a graphics library is specified in the program using the following procedure:
  • To set the graphic screen mode, use the following procedure:

InitGraph(Var Driver, Mode: Integer, Path: String);

Path to graphics library

Driver operating mode

Driver code


Graphics program operating modes

  • Text mode. Used to write a program. The transition from program text mode to graphical program execution mode is carried out using the RUN procedure or the Ctrl+F9 keys.
  • Graphic mode. The image is constructed from individual points (pixels). The operating mode of the VGAHi graphics driver corresponds to a 640x480 pixel graphics grid, with a palette of 16 colors, automatic detection of the driver type and installation of the graphics mode. Exit the program to program text mode by pressing the Enter key.

Program structure

Program ricunok; (program title)

Uses Graph ; (connecting a graphics library)

Var Dr, Md: integer; (driver variable description)

Begin (beginning of the program body)

Dr:= Detect ; (driver type)

InitGraph(Dr, Md,‘C:\TP 70\BGI’); (enable graphics with

graph library)

ReadLn ; (program delay)

End. (end of program)


Calculation of coordinates for geometric figure inference procedures

Bar(50,100,150,150)

Fillelipse(250,125,25,25)

VGA type monitor


Using Graphical Procedures

A program that displays a white Japanese flag with a red circle in the center

on a turquoise screen background.

Var Dr, Md: Integer;

InitGraph(Dr, Md, 'C:\TP70\BGI');

ClearViewPort ; (Screen clearing, to set the screen background)

SetBkColor(Cyan); (Set the screen background color to turquoise)

SetFillStyle(1, 15); (Color of rectangle filling according to pattern and color)

Bar(10, 10, 410, 210); (Drawing a filled rectangle by coordinates)

SetColor(4); (Setting the color of the circle lines)

Circle(210, 110, 30); (Drawing a circle with center coordinate and radius)

SetFillStyle(1, 4); (Color of filling the circle according to the pattern and color)

FloodFill(200, 100, 4); (Filling a closed shape with color around the coordinate to the borders)

ReadLn ; (program delay)

CloseGraph ; (exit graphics mode)

End. (end of program body)


Program that displays a block diagram

var dr,md:integer;

begin dr:=detect;

setlinestyle(0,1,3);

ellipse(320,40,0,360,50,10);

line(320,50,320,70);

line(270,70,390,70); line(390,70,370,100); line(370,100,250,100); line(250,100,270,70); line(320,100,320,120);

rectangle(260,120,380,150);

line(320,150,320,170); line(320,170,400,190); line(400,190,320,210); line(320,210,240,190); line(240,190,320,170);

line(240,190,200,190); line(200,190,200,210);

rectangle(140,210,260,240);

line(200,240,200,260);

rectangle(140,260,260,290);

line(200,290,200,310);

Line(140,310,260,310); Line(260,310,240,340); Line(240,340,120,340); Line(120,340,140,310); line(200,340,200,360);

line(200,360,100,360); line(100,360,100,170); line(100,170,320,170); line(400,190,440,190);

line(440,190,440,380); line(440,380,320,380); line(320,380,320,400);

ellipse(320,410,0,360,50,10);

settextstyle(7,0,2);

outtextXY(300,75,’F"); outtextXY(300,125,"N:=0"); outtextXY(292,178,'N


Result of program execution


Write a program that displays the following figure

Program paravoz;

var dr,md:integer;

begin dr:=detect;

initgraph(dr,md,"C:\tp70\bgi");

SetFillStyle(1, 2);

Bar(150,30,250,225);

SetFillStyle(1, 1);

Bar(180,55,220,115);

SetFillStyle(1, 2);

Bar(250,120,450,225);

Line(350,65,390,65);

Line(350,65,360,120);

Line(390,65,380,120);

Line(380,120,360,120);

SetFillStyle(1,1);

Sector(420,245,0,360,20,20);

Sector(300,245,0,360,20,20);

Sector(185,245,0,360,20,20);

SetFillStyle(1,7);

Sector(400,50,0,360,30,10);

Sector(425,25,0,360,20,10);

Sector(445,5,0,360,10,5);


Homework

In the workbooks Topic No. 4, complete tasks 1 and 2 on the use of graphic procedures.

Table of basic graphical procedures in Appendix 4.

Slide 2

Each pixel (point) has two coordinates: x and y. The geometric dimensions of a pixel are determined by the resolution of the monitor.

Slide 3

Example 1. Demonstration of connecting the GraphAbc module program tochka; uses graphabc; (connecting the GraphAbc module) begin setwindowsize(640,480);(sets the size of the graphics window) setpixel(100,120,clBlack); (sets the pen color to black and draws a point at coordinates (100,120)) end. In this example, we were introduced to the use of the setpixel command in a special case. IN general view this command looks like this: setpixel(x:integer,y:integer,c:color) – draws a point with coordinates (x,y) with color c. clBlack – black clPurple – purple clWhite – white clRed – red clGreen – green clBrown – brown clBlue – blue clSkyBlue – light blue clYellow – yellow

Slide 4

A program for drawing a segment connecting two points with coordinates (120,150) and (150,80) using a red pen color may look like this: Example 2. Demonstration of drawing a line program Linii; uses graphabc; begin setwindowsize(640,480); setpencolor(clred); (sets the pen color to red) line(120,150,300,100); (draws a segment from (120,150) to (300,100)) end.

Slide 5

Example 3. Demonstration of drawing lines with different pen colors program treugolnik; uses graphabc; begin setwindowsize(640,480); setpenwidth(5); (sets the width of the current pen. The number of pixels that make up the line width is indicated in parentheses) setpencolor(clred); (sets the pen color to red) line(100,200,170,70); (draws a segment from a point with coordinates (100,200) to a point with coordinates (170,70)) setpencolor(clGreen); (sets green pen) line(170,70,250,200); (draws a line from point(170,70) to point(250,200)) setpencolor(clBlue); (sets the pen color to blue) line(250,200,100,200);(draws a segment from point(250,200) to point(100,200)) (the result is a triangle with sides of different colors) end.

Slide 6

It is important to remember: 1. When drawing a line, you can set its size (the coordinates of its ends), color, width (thickness) and style. 2. To set the line style in the GraphAbc module, there is a procedure SetPenStyle (Style), where Style is the pen style constants (see Appendix to Chapter 3). 3. The line can be solid, dotted, dashed, dashed. Closed figures can be painted over.

Slide 7

Example 4. Demonstration of drawing closed figures using lines and painting them Program treug_zakrash; uses graphabc; begin setwindowsize(640,480); clearwindow(clWhite);(clears the graphics window with white color) setpenwidth(3); (sets the width of the current pen) setpenstyle(pssolid); (sets the line style - solid line) setpencolor(clgreen); (sets the pen color to green) line(100,200,170,70); (draws lines in green) line(170,70,250,200); line(250,200,100,200); floodfill(440,120,clred); (Paints the triangle red) end.

Slide 8

As a result of executing the program, a triangle drawn in green and shaded in red will appear on the monitor screen in the graphics window. It is important to remember: 1. You can only paint closed shapes whose outline is drawn in one color. 2. In the filling procedure floodfill(x,y,c), the coordinate of the point (x,y) is indicated, which must fall into the inner area of ​​the shape being painted.

Slide 9

Rectangles and circles can be drawn using the commands rectangle(x1,y1,x2,y2) and circle(x,y,r), respectively. Let's look at how this can be done using the example of a program that draws a rectangle and a circle. Example 5. Demonstration of drawing a rectangle and a circle program geometry; uses graphabc; begin setwindowsize(640,480); setpencolor(clBlue); (sets the blue pen color for drawing the outline of the rectangle) setpenwidth(6); (sets the pen width) rectangle(50,50,250,150);(draws a rectangle specified by the coordinates of opposite vertices) setpencolor(clred); (sets the pen color for drawing the circle outline to red) circle(350,100,60); (draws a circle centered at coordinates (350,100) and radius 60) end.

Slide 10

Demonstration of painting a rectangle and a circle and their inscription program geometry3; uses graphabc; begin setwindowsize(640,480); clearwindow(clYellow);(sets the background color to yellow) setpencolor(clteal); (sets the blue-green color of the pen) setpenwidth(5); (sets the line width) setbrushcolor(clolive); (sets the olive color of the brush) rectangle(100,100,300,200); (draws a rectangle filled with olive color) setbrushcolor(clblue); (sets the brush color to blue) circle(400,150,50); (draws a blue circle) setfontstyle(fsbold);(sets the font style) setfontsize(15);(sets the font size) setbrushcolor(clwhite);(sets the brush color to white) setfontcolor(clolive);(sets the olive font color) textout (100,220,"Rectangle"); (makes a caption) setfontcolor(clblue); (sets the font color to blue) textout(380,220,"Circle"); (makes an inscription) end.

Slide 11

Pascal's ABC graphic procedures: 1. SetPixel(x,y,color: integer); - paints one pixel with coordinates (x,y) with color. 2. Line(x1,y1,x2,y2: integer); - draws a segment from point (x1,y1) to point (x2,y2). 3. Circle(x,y,r: integer); - draws a circle with center at point (x,y) and radius r. 4. Rectangle(x1,y1,x2,y2: integer); -draws a rectangle specified by the coordinates of opposite vertices (x1,y1) and (x2,y2). 5. TextOut(x,y: integer; s: string); - outputs string s to position (x,y) (point (x,y) specifies the upper left corner of the rectangle that will contain the text from string s). 6. FloodFill(x,y,color: integer); - fills an area of ​​the same color with color, starting at point (x,y). 7. FillRect(x1,y1,x2,y2: integer); - fills the rectangle specified by the coordinates of opposite vertices (x1,y1) and (x2,y2) with the color of the current brush.

Slide 12

What will be displayed on the monitor screen as a result of executing these programs? program graphics1; uses graphabc; begin setpenwidth(10); setpencolor(clred); Line(100,100,270,90); End. Program grafika2; uses graphabc; begin setpenwidth(8); setpencolor(clblue); circle(200,150,50); End.

Slide 13

Copy in your notebook: Topic: Graphical capabilities of a programming language. 1. Line(x1,y1,x2,y2; - segment from (x1,y1) to (x2,y2) 2. Circle(x, y, r); - circle with center at (x,y) and radius r. 3. Rectangle(x1,y1,x2,y2); - rectangle defined by the coordinates of opposite vertices (x1,y1) and (x2,y2) 4. FloodFill(x, y, color); color color, starting from point (x,y).

View all slides

Window management
SetWindowSize(w,h);
sets the dimensions of the graphics window
SetWindowWidth(w);
sets the width of the graphics window
SetWindowHeight(h);
sets the height of the graphics window
SetWindowTitle('Title') ;
changes the window title

Clearing the graphic
windows
ClearWindow;
clears the graphics window with white color
ClearWindow(color);
Clears the graphics window with the specified color.
uses GraphABC;
begin
ClearWindow;
ClearWindow(clMoneyGreen);
end.
Green money color

Graphic
primitives
Dot
Line
Rectangle
Circle
Ellipse
Sector
Arc

Dot
SetPixel(x,y,color);
paints one pixel with coordinates (x,y)
color
uses GraphABC;
begin
SetPixel(300,200,clred);
end.

Lines
LineTo(x,y);
draws a segment from the current pen position to a point
(x,y)
the pen coordinates also become
equal to (x,y)
x,y
uses GraphABC;
Begin
LineTo(300,200);
end.

Lines
MoveTo(x,y);
sets the current drawing position
to point (x,y)
x1,y1
x2,y2
uses GraphABC;
Begin
MoveTo(150,50);
LineTo(500,250);
end.

Lines
Line(x1,y1,x2,y2);
draws a segment with a start at point (x1,y1) and an end
at point (x2,y2)
x1,y1
x2,y2
uses GraphABC;
begin
line(100,50,500,250);
end.

colors
clAquamarine
clBisque
clBlue
clBurlyWood
clChocolate
clCornsilk
clDarkBlue
clDarkGray
clDarkMagenta
clDarkOrchid
clDarkSeaGreen
clDarkViolet
clDeepSkyBlue
clAzure
clBlack
clBlueViolet
clCadetBlue
clCoral
clCrimson
clDarkCyan
clDarkGreen
clDarkOliveGreen
clDarkRed
clDarkSlateBlue
clDeepPink
clDimGray
clBeige
clBlanchedAlmond
clBrown
clChartreuse
clCornflowerBlue
clCyan
clDarkGoldenrod
clDarkKhaki
clDarkOrange
clDarkTurquoise
clDarkSlateGray
clDarkSalmon
clDodgerBlue

colors
clFuchsia
clGold
clGreen
clHotPink
clIvory
clLavenderBlush
clLightBlue
clGainsboro
clGoldenrod
clGreenYellow
clIndianRed
clKhaki
clLawnGreen
clLightCoral
clGhostWhite
clGray
clHoneydew
clIndigo
clLavender
clLemonChiffon
clLightCyan
clLightGoldenrodYe
clLightGray
clLightGreen
llow
clLightPink
clLightSalmon
clLightSeaGreen
clLightSkyBlue
clLightSlateGray
clLightSteelBlue
clLightYellow
clLime
clLimeGreen
clLinen
clMagenta
clMaroon
clMediumAquamari
clMediumBlue
clMediumOrchid
ne
clMediumPurple
clMediumSeaGreen clMediumSlateBlue
clMoneyGreen
clPlum
clMistyRose
clRandom – random
color from the entire palette
Pascal's colors

Line color
SetPenColor(color);
sets the pen color specified by the parameter
color
uses GraphABC;
begin
SetPenColor(clred);
line(30,30,400,350);
end.

Dotted line
SetPenStyle(<…>);
sets pen style
uses GraphABC;
begin
setpencolor(clred);
SetPenWidth(4);
SetPenStyle(psSolid);(Solid)
Line(10,75,350,75);
SetPenStyle(psDash);(Dash)
Line(10,100,350,100);
SetPenStyle(psDot); (Dotted)
Line(10,125,350,125);
SetPenStyle(psDashDot); (Dash-dotted)
Line(10,150,350,150);
SetPenStyle(psDashDotDot);
(Alternate dashed)
Line(10,175,350,175);
end.

Line thickness
SetPenWidth(n);
sets the width (thickness) of the pen to n
pixels
uses GraphABC;
begin
setpenwidth(20);
setpencolor(clred);
line(30,30,400,350);
end.

Triangle
Line(x1,y1,x2,y2);
LineTo(x,y);
uses GraphABC;
begin
setpenwidth(20);
setpencolor(clred);
line(300,100,500,300);
lineto(100,300);
lineto(300,100);
floodfill(300,200,clgreen);
end.

Rectangle
Rectangle(x1,y1,x2,y2);
draws a rectangle given by coordinates
opposite vertices (x1,y1) and (x2,y2)
x1,y1
x2,y2
uses GraphABC;
begin
Rectangle(50,50,200,200);
end.

Fill color
FloodFill(x,y,color);
fills an area of ​​the same color with color, starting at the point
(x,y)
x1,y1
x2,y2
uses GraphABC;
begin
Rectangle(50,50,200,200);
FloodFill(100,100,clBlue);
end.

Brush Fill
SetBrushColor(color);
sets the brush color, extends to closed
circuit, the description of which follows the installation procedure
brush colors
uses GraphABC;
Begin
SetBrushColor(clGreen);
Rectangle(50,50,300,300);
end.

Brush Fill
SetBrushStyle(<…>);
sets the brush style type
bsSolid
Solid brush (by
default)
bsClear
Transparent brush
bsHatch
Line brush
bsGradient
Gradient brush

Brush Fill
Brush hatch styles are specified by an enumerated type
SetBrushHatch(<…>);
The following constants are defined for brush hatch styles:
uses GraphABC;
Begin
SetBrushStyle(bsHatch);
By
default
style is set 0 –
solid
filling
color.
SetBrushHatch(bhHorizont
al);
Rectangle(10,10,100,100);

end.

Brush Fill
For a stroke brush, you can additionally set
property:
SetHatchBrushBackgroundColor(clGold) ;
uses GraphABC;
Begin
SetBrushStyle(bsHatch);
By
default
style is set 0 –
SetHatchBrushBackgroundColor(cl
solid
filling
Gold);
color.
SetBrushColor(clCoral);
SetBrushHatch(bhHorizontal);

Outline color and thickness
SetPenWidth(w);
SetPenColor(color);
uses GraphABC;
begin
SetPenColor(clred);
SetPenWidth(20);
Rectangle(50,50,200,200);
FloodFill(100,100,clBlue);
end.

Circle
Circle(x,y,r);
draws a circle centered at (x,y) and
radius r
r
x1,y1
uses GraphABC;
begin
Circle(500,200,100);
FloodFill(500,200,clred);
end.

Ellipse
Ellipse(x1,y1,x2,y2);
draws an ellipse given by its described
rectangle with coordinates of opposite
vertices (x1,y1) and (x2,y2).
x1,y
1
x1,y
1
uses GraphABC;
begin
Ellipse(50,50,200,350);
FloodFill(50+100,50+100,clred);
Ellipse(250,150,550,300);
FloodFill(250+100,150+100,clBlue);
end.
x2,y
2
x2,y
2

Arc of a circle
Arc(x,y,r,a1,a2);
draws a circular arc with center at point (x,y) and radius r,
enclosed between two rays forming angles a1 and a2
with the OX axis (a1 and a2 are real, specified in degrees and
counted counterclockwise)
r
x,y
uses GraphABC;
Begin
SetPenWidth(10);
*
Arc(300,250,150,45,135)
;
end.

Sector
Pie(x,y,r,a1,a2);
draws a sector of a circle bounded by an arc (parameters
procedures have the same meaning as in the Arc procedure)
uses GraphABC;
begin
Pie(300,200,100,0,90);
FloodFill(300+10,200-10,
clAquamarine);
end.

Text output
TextOut(x,y,’string’);
outputs a line of text at position (x,y) (point (x,y) specifies
the top left corner of the rectangle that will contain
text)
uses GraphABC;
begin
TextOut(100,30,"Square");
Rectangle(50,50,200,200);
FloodFill(55,55,clBlue);
end.

Actions with font
SetFontName('name');
sets the font name
SetFontColor(color);
sets font color
SetFontSize(sz);
sets the font size in points
SetFontStyle(fs);
sets the font style

Font name
The default font is set to
name MS Sans Serif
The most common fonts are
Times New Roman, Arial and Courier New
The font name can be typed without taking into account
register
For example:
SetFontName('Times New Roman');

Font style
fsNormal - normal
Defined by named constants:
fsBold - bold
fsItalic – oblique
fsBoldItalic – bold italic
fsUnderline – underlined
fsBoldUnderline – bold underline
fsItalicUnderline – oblique underlined
fsBoldItalicUnderline – bold italic underline

For example:
uses GraphABC;
Begin
SetFontName('Arial');
SetFontSize(20);
SetFontColor(clRed);
TextOut(10,10,‘normal");
SetFontStyle(fsItalic);
SetFontColor(clBlue);
TextOut(10,50,‘slanted");
SetFontStyle(fsBold);
SetFontColor(clRandom);
TextOut(10,90,‘bold");
SetFontStyle(fsUnderline);
SetFontColor(clRandom);
TextOut(10,130,‘underlined");
SetFontStyle(fsBoldItalicUnderline);
SetFontColor(clRandom);
TextOut(10,170,’bold, italic, underline");
end.

Used
colors
Color can also be set using the function
RGB(r,g,b) where r, g and b are integers in
range from 0 to 255.
The function returns an integer value that is
color code that contains red, green and
blue components with intensities r, g and b
respectively (0 corresponds to the minimum
intensity, 255 – maximum).
RGB(255,255,255) – matches
white color.
RGB(0,0,0) – corresponds to black color.

For example:
uses GraphABC;
begin
Clearwindow(rgb(200,150,250));
TextOut(93,30," Square ");
Rectangle(50,50,200,200);
FloodFill(55,55,clRed);
TextOut(275,30," Ellipse");
Ellipse(250,50,350,200);
FloodFill(250+50,50+50,clYellow);