Saturday, November 21, 2009

16. Basic Graphics in Free Pascal

Graphics is just painting on the screen, the screen has pixels in it and we put light on individual pixels. Each pixel on the screen has a position (x,y), where x and y are the x,y coordinates of the pixel. Top left corner is the lowest pixel coordinate (0,0) and the bottom right corner is the highest pixel coordinate (1023,711). We can activate any pixel in the screen with any color we want. If we light the pixels on the circle, we get a circle on the screen. Basically Graphics is lighting the right pixels on the screen.

In order to write a Graphics program in Pascal, we have to do three special steps.

First, we have to use graphical unit.
uses crt,graph;

Second, we have to initialize the graphical unit.
grdriver := DETECT;
initgraph(grdriver,grmode, ' ' );

Third, we have to give instructions to the graph unit to paint on the screen.
settextstyle(triplexfont,horizdir,2);
OutTextXY(50,30,'Graphics With Pascal');

In the following program I wrote, I experimented with basic shapes and colors. All the instructions for graphical pictures are in the procedure graphicpics. Study the program and do your own experiments.

Program program16;
{Author: Rao S Lakkaraju}
{* Basic Graphics Program *}

uses crt,graph;
var grmode, grdriver :integer;
x,y : integer;

Procedure graphicpics;
Begin
{Now give instructions to do graphics}

randomize;
settextstyle(triplexfont,horizdir,1);
for x := 1 to 200 do begin putpixel(49+x,500,(random(15))); end;


setcolor(Lightgreen);

settextstyle(triplexfont,horizdir,2);
OutTextXY(50,30,'Graphics With Pascal');
settextstyle(triplexfont,horizdir,1);
OutTextXY(50,300,'Screen Coordinates');
OutTextXY(50,350,'Top left corner (x,y): 0,0');
OutTextXY(50,400,'Bottom right corner (x,y): 1023,711 Pixels');
writeln('max x = ', getmaxx,' max y = ', getmaxy);

setfillstyle(1,red);
bar(100,100,250,250);
setfillstyle(2,green);
bar(0,0,23,19);
bar(1000,690,1023,711);

setcolor(red);
{line(x1,y1,x2,y2)}
line(0,0,0,23);
line(0,0,23,0);
for x := 1000 to 1023 do putpixel(x,711,red);
for y := 690 to 711 do putpixel(1023,y,red);
for x := 800 to 850 do putpixel(x,500,red);
for y := 450 to 500 do putpixel(850,y,red);
setcolor(cyan);
{rectangle(x1,y1,x2,y2);}
Rectangle(50,150,80,250);

{circle(x1,y1,radius); x1,y1 center of the circle}
circle(80,180,50);

{ellipse(x1,y1,degreesfrom,degrees to,hr,vr)}
ellipse(150,200,0,360,30,40);

{arc(x,y,p,q,radius)}
arc(180,250,0,90,60);

{pieslice(x,y,p,q,radius)}
pieslice(150,150,20,95,100);
End;


begin
{First we have to initialize the graphics mode}

clrscr;
grdriver := DETECT;
initgraph(grdriver,grmode, ' ' );

graphicpics;

Writeln('**** My Sixteenth Pascal Program ****');
readln;
CloseGraph;
end.

Summary: Graphics is simple painting on the screen. Pascal has all the basics of drawing geometric shapes(Line, Rectangle, Circle, Ellipse, Arc etc) using basic colors (red, white etc). Pleasing graphics is a combination of these basic shapes and a little imagination of placing them on the screen.

No comments:

Post a Comment