PROG
Reads a rectangle's upper left hand corner x and y position
height and width
PED
To learn graphics
CONCEPTS
Graphics is a set of pixels--size varies depending upon graphics card
(0,0) at upper left hand corner
moveto(pixel_x,pixel_y) moves the electronic equivalent of the "pen"
to given position
lineto(pixel_x,pixel_y) moves from the current position of the "pen"
to the new position pixel_x,pixel_y but draws
a line.
SF
uses
Graph; must be placed write after "program" statement
invokes the Graphics Unit
var
GraphDriver:integer; these three lines must be defined GraphMode: integer; as ErrorCode: integer; indicatedTHESE THREE LINES SHOULD BE WHENEVER YOU ARE READY TO START DRAWING
GraphDriver := Detect;
InitGraph(GraphDriver,GraphMode,'w:\programs\tp6_0\bgi');
ErrorCode := GraphResult;
THESE TWO LINES SHOULD BE AFTER YOU HAVE FINISHED DISPLAYING GRAPHICS. THEY INSURE THAT THE DRAWING DOESN'T JUST FLASH AWAY.
Readln;
CloseGraph;
{ text } comment, ignored, help those reading the program
documentation freaks love these things.
seriously, use them wisely
Click here to view Unit 1.6 Pascal program.
Although I could teach you everything you need to know, without teaching graphics, if you know graphics you can have the fun of making your programs do nice pictures. I will give assignments involving graphics for your programming pleasure throughout the semester.The graphics programs lay out lines on a uniform array of pixels. The pixels correspond to pieces of phosphorus on the glass. They glay when activated by the ray of the cathode ray tube in the monitor above the computer. They form a rectangular grid like the dots in a connect-the-dot. They are sufficiently close together that if all the lines that are next to each other and in a row are turned on, it appears we have a solid line.
The point at the upper left hand corner of the screen is numbered 0,0. As the first number increases, we go further down the screen towards the ground. As the second number increases, we are talking about pixels further to the right.
The number of hte lower right hand corner varies. Some of our computers, bought recently, have VGA monitors which might allow up to 600 by 400. Our older machines might have as little as 320 and 200.
There are two fundamental graphics actions that we will learn about--
the first changes the current pixel to a given x and y number. It is simply written
as:
moveto(pixel_x,pixel_y);
If we view our actions as moving a pen around on a screen, this command lifts up the
pen, and moves to a given location.
The second draws a line from a current location to another location. It is
written:
lineto(pixel_x,pixel_y);
There is a little bit of stuff we have to copy or memorize in order to get graphics to work:
1. Just after the program statement, we have to write:
uses
Graph;
This causes the compiler to access a built-in library called a "unit" in order to perform these
graphics capabilities.
Later on in this semester, we will learn how to create our own units.
2. There are three integer variables that need to be defined for use later. Note that we can have
several integer statements after the "var" or we could put all our box_names in a single line
with a single "integer" keyword. It's a matter of preference.
They are GraphDriver, GraphMode and ErrorCode.
3. The following code sets up to start drawing.
GraphDriver := Detect;
InitGraph(GraphDriver,GraphMode,'w:\programs\tp6_0\bgi');
ErrorCode := GraphResult;
The first line figures out which monitor we have. The second changes our screen from the
normal text to graphics.
4. At the very end of the program, we need to wait until the user finishes viewing the output. The
single line "Readln" will wait for the user to press the carriage return key.
The line "CloseGraph" restores the screen to normal mode from graphics mode.
Let's look at program. Lines 3 and 4 are the "uses Graph" that are required for all
programs using graphics.
Lines 6 through 8 define the variables that required for graphics. We define four variables on line 9. xll is the x coordinate of the upper-left hand corner. yll is the y coordinate of the upper-left hand corner. Then side1 is the length from the left hand side to the right hand side. side2 is used for the distance from the top to the bottom of the rectangle. Remember that all these numbers are in pixels.
Lines 13 and 14 set up our text file to input our rectangle parameters. Lines 16 to 19 read in the four variables mentioned above.
Lines 22 to 24 are the standard code to put the screen in graphics mode.
Line 27 moves to the upper left hand corner. Line 28 moves to the right side1 length and draws the upper horizontal line. Line 29, moves down by side2 pixels and draws the right hand side of our rectangle. Line 30 moves to the left by side1 pixels. This draws the lower line. And finally line 31 draws the left hand side going back to the original starting point. Remember pixels are numbered from the top down.
Lines 35 and 36 will cause us to wait for a carriage return and then returns the screen to regular mode.
It is possible to read in some numbers from the screen, as opposed to a file, and use these to define what is displayed. If you wish to do this, you should put the statements to read in the data from the screen before you do statement 2. You should also do a Readln(xyz) where xyz is some temporary variable in step 4. Otherwise, the final "Readln" doesn't delay at all.
See next page: