Tuesday, December 8, 2009

18. More Graphics with Free Pascal

The following program generates circles and ellipses. By using random numbers for colors and delayed activation of pixels on the screen, I tried to paint a pleasing picture. The static(standard) graphics commands are in the main program and the variable calls for activating pixels(pictures) are in the createcircles procedure. The program follows:

Program program18;
{Author: Rao S Lakkaraju}
{Graphics with Pascal}
Uses Crt,Graph;

Var GraphicsDriver, GraphicsMode,
n,radius,radius1,radius3 : Integer;
ax,ay,ar : integer;

Procedure Createcircles;
Begin
Outtextxy(120,30,'My Beautiful Painting');

setcolor(red);
setfillstyle(1,red);
rectangle(75,120,510,570);
radius1 := 0;
ax := getmaxx div 2;
ay := getmaxy div 2;


for radius := 50 to 100 do begin

SetColor(Random(14) + 1);
radius1 := radius ;
ar := radius div 2;
Outtextxy(120,30,'My Beautiful Painting');

{Left Side}
rectangle(10+ar,70+ar,200+ax-ar,290+ay-ar);
circle(ax-280, ay, radius1);
circle(ax-280, ay, radius1-49);
circle(ax-280, ay, radius1+55);
ellipse(ax-280, 420+radius1, 0,360,150,40);
ellipse(ax-280, 120+radius1, 0,360,150,40);

{ellipse(x1,y1,degreesfrom,degrees to,hr,vr)}
{ Right Side}

circle(ax, ay, radius1);
circle(ax , ay - radius1- ar, ar );
circle(ax , ay + radius1+ ar, ar );
ellipse(430, 220+radius1, 0,360,60,60);
ellipse(430+150, 220+radius1, 0,360,60,60);
ellipse(430, 220+2*radius1+30, 0,360,60,40);
ellipse(430+150, 220+2*radius1+30, 0,360,60,40);
delay(300 );

end;
End;

Begin
ClrScr;
Writeln('Graphics Initialization');
GraphicsDriver := Detect;
InitGraph(GraphicsDriver, GraphicsMode,'');
ClearViewPort;
Randomize;
SetColor(cyan);
settextstyle(triplexfont,horizdir,2);
for n:= 1 to 1 do begin

ClearViewPort;
createcircles; delay(300); end;

SetColor(cyan);
settextstyle(triplexfont,horizdir,1);
OutTextXY(135,70,'Is It not Wonderful ????');
OutTextXY(580,580,'lakkaraju');
Readln;
CloseGraph;

End.

Summary: Graphics is activation of pixels on the screen. Simply sketch it on paper . transform into rectangles, circles and ellipses with proper coordinates and colors. Now vary some of the coordinates and colors; A beautiful picture comes out in real time, as you imagined.

Tuesday, November 24, 2009

17. Free Pascal Graphics

In the previous post we learned the fundamentals of graphics and were able to do basic painting on the screen. In this post we use the same instructions to create circles etc. and use delay and loops to create visual effects. Study the program17 and play with it.

There are two procedures in the program. First procedure createcircles creates circles etc. The second procedure destroycircle destroys the created circle and also creates more visuals while destroying the circle.

The main program is just initiating graphics, declaring variables, calling the procedures and closing the Graphical unit.

Study the two procedures and play with them and enjoy creating your own paintings. Use delay and variation of pixel coordinates for visual effects.

Always remember Graphics is just activation of pixels on the screen. Screen is your canvas and use your imagination and mathematical formulae for timing the activation and deactivation of pixels.

Program program17;
{Author: Rao S Lakkaraju}
{Graphics with Pascal}
Uses Crt,Graph;

Var GraphicsDriver, GraphicsMode,
x,y,radius,radius1,radius3 : Integer;

Procedure Createcircles;
Begin
setcolor(yellow);
rectangle(100,120,510,570);
radius1 := 0;
Outtextxy(20,40,' Creating Circles');

For radius := 1 to 200 do
Begin
while radius1 <> radius then begin SetColor(black); {Set text colour}
Circle(GetMaxX Div 2, GetMaxY Div 2, Radius3);
radius3 := radius - 5;
SetColor(Random(15) + 1);
Circle(100,200, Radius3 div 2);
{ellipse(x1,y1,degreesfrom,degrees to,hr,vr)}
ellipse(250,330 + radius3 ,0,360,80,40);
pieslice(370,570,20,80,radius3 div 2+40);
Delay(905 );
setfillstyle(1,red);
bar(240,570,260, radius3 + 300 );
{pieslice(x,y,p,q,radius)}
pieslice(430,200,20,80,radius3 div 2+40);
end; end;
End;


Begin
ClrScr;
Writeln('Graphics Initialization');
GraphicsDriver := Detect;
InitGraph(GraphicsDriver, GraphicsMode,'');
ClearViewPort;
Randomize;
SetColor(cyan);
settextstyle(triplexfont,horizdir,2);
OutTextXY(20,20,'Welcome to the Disappearing Circle');


createcircles;
destroycircle;

SetColor(cyan);
OutTextXY(20,80,' I am done');
Readln;
CloseGraph;

End.

SUMMARY: Graphics is just activating the pixels on the screen. We can use delay, color and timing in activating the pixels to give visual effects. Your paint brush is your program, control the paint brush movements and you get an excellent pictures on the screen.

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.

Thursday, November 19, 2009

15. Recursive programming in Free Pascal

Recursion is the process of working on a procedure repeatedly until we reach a solution. For example if you want to find out the sum of numbers starting from 1 increasing by 1 until 6, we start with 1 and make it as temporary sum and add to it 2,3 etc until we reach 6. The sum now gives the value we want.
We can also get the sum doing reverse computation. We start with 6(n) as the temporary sum and add to it 5(n-1) , decreasing 6 by 1.
Thus sum of 1 thru 6 is: sum(6) := 6 + sum 1 thru 5.
We keep on doing this repeatedly (recursive) until we reach 1, where we know the answer, the sum of a series of numbers starting with 1 and ends in 1, is 1.
if n =1 then sum := 1
Once we reach sum(1) = 1, we go back and solve all the equations and find the value of sum(6).

sum := n + sum(n-1);
sum(6) := 6 + sum(5),
sum(5) := 5 + sum(4) ,
sum(4) := 4 + sum(3),
sum(3) := 3 + sum(2),
sum(2) := 1 + sum(1),
sum(1) := 0 + 1 ** We know the answer for sum(1) and go backwards and solve the problem.

The main points in recursive programming is: Doing repeatedly the same process until we reach a condition to break the repetition. The condition is important because, if it is not there we will be repeatedly calling the process until we fill up the memory and crash.

Program15 Illustrates Recursive programming with two recursive functions sum and fact.

if n = 1 then sum := 1 else sum := n + sum(n-1);

if n = 0 then fact := 1 else fact := n * fact(n-1);

Program 15 follows:

Program program15;
{Author: Rao S Lakkaraju}
{Recursive Programming}
uses crt;

var n,m : integer;

function sum (n:integer) :integer;
begin
writeln('n is ',n);
if n = 1 then sum := 1 else sum := n + sum(n-1);
Writeln('sum is ',sum);
end;

function fact(n:integer) : integer;
begin
if n = 0 then fact := 1 else
fact := n * fact(n-1);
end;

begin
clrscr;
write('Please enter an integer to sum to ');
readln(n);
m := sum(n);
writeln('Sum of 1 to ',n,' is ',m);

write('Please enter an integer for factorial ');
readln(n);
m := fact(n);
writeln('Factorial of ',n,' is ',m);
Writeln( '**** My fifteenth Pascal Program ****');
readln;
end.

SUMMARY: Recursive programming is calling a known process repeatedly until we reach an end condition. We should know the process and the end condition where that process stops, otherwise we end up nowhere like driving a car without knowing where to stop.

Thursday, November 12, 2009

14. Binary Tree Lists in Free Pascal

A binary tree is an extension of Link List. Basic Link List has only one link attached to the Node whearas basic Binary tree has two links attached to the Link(Node). The very first link in the tree forms the root of the tree, has a data and two link addresses showing the next links. The rule of Link attachments are: If data in current link(Link to be attached) > root data, current link is attached to the right if not it is attached to the left.

1. First the link is described with a type declaration as having a data and two link pointers right and left. root,temp,current are pointer variables are the type link.

type point = ^link;
link = record
data : integer;
right : point;
left : point;
end;

var i : integer;
root,temp,current : point;

2. The main program is to create tree root and get links to attach to the root.

createroot;
getlink ;

3. Read input integer and create root.

Procedure createroot;
Begin
Write('enter root integer = ');
readln(i);
new(root);
root^.data := i;
root^.right := nil;
root^.left := nil;
end;


4. Make links and attach to the tree. We create a current link and call addnode to add to the tree.

Procedure getlink ;
begin

for i := 1 to 5 do begin

new(current);
current^.data := i;
current^.right := nil;
current^.left := nil;

addnode(i,root);
readln;
end;
end;{end getlink }

6. In addnode we compare data of current node to be attached to the root and attach the node if the next address is vacant. If it can not be attached, start calling addnode with failed node address until vacant node is found for attaching the current node.

Procedure addnode( i : integer; temp : point );
begin
writeln;
write('i=',i,' trying to add node value: ',i,' at as= ',temp^.data,' ',i);


if i > temp^.data then if temp^.right = nil then
begin writeln(' right'); temp^.right := current; end
else addnode(i,temp^.right);

if i <= temp^.data then if temp^.left = nil then
begin writeln(' left'); temp^.left := current; end
else addnode(i,temp^.left);

end;


6. Program follows: If you run the program it will ask you for an integer to create the root node.

Program Program14;
(* Author: Rao S Lakkaraju *)
(* Pointer Basics _ Binary trees *)
uses crt,graph;

type point = ^link;
link = record
data : integer;
right : point;
left : point;
end;

var i : integer;
root,temp,current : point;


Procedure createroot;
Begin
Write('enter root integer = ');
readln(i);
new(root);
root^.data := i;
root^.right := nil;
root^.left := nil;
end;

Procedure addnode( i : integer; temp : point );
begin
writeln;
write('i=',i,' trying to add node value: ',i,' at as= ',temp^.data,' ',i);


if i > temp^.data then if temp^.right = nil then
begin writeln(' right'); temp^.right := current; end
else addnode(i,temp^.right);

if i <= temp^.data then if temp^.left = nil then begin writeln(' left'); temp^.left := current; end else addnode(i,temp^.left); end; Procedure getlink ; begin for i := 1 to 5 do begin new(current); current^.data := i; current^.right := nil; current^.left := nil; addnode(i,root); readln; end; end;{end getlink } Procedure displayorder (root : Point); begin IF root <> nil then begin

displayorder(root^.Left);
write(root^.Data);
displayorder(root^.Right);

end;
end;
{ Main Program Begins}

begin
clrscr;

createroot;
getlink ;

Write('Display tree Nodes: ');
Displayorder(root);
writeln;
Writeln('My fourteenth Pascal Program');

readln;
end.

Summary: Binary Trees in Pascal are just ordered Link Lists. The order is if the data in the current link is greater than the data in the last link, it is attached to the right of the last link otherwise attached to the left. With this rule in mind we traverse the tree(visit the links) starting from the root for a vacant spot to hang the branch(Link).

Monday, November 9, 2009

13. Link Lists in Free Pascal

Creating Link Lists in Pascal is very easy with pointers.

1.First define the link as a record type. It will have two areas. A data area and a pointer area.
type point = ^link;
link = record
data : integer;
next : point;
end;

2. Define variables for Link Record. We need three pointer variables of type link to track the growth of the List, first, last and current.
var i : integer;
first,last,current : point;

3. Create the Link Record and make it as the current record for linking to the list. If there is no first record, make the current record as first and last records in the List. When we create the next current link , we just replace the last record's next pointer by the current record pointer and make the current record as the last record, effectively increasing the links by 1.

new(current);
current^.data := i;
current^.next := nil;
if first = nil then first := current
else last^.next := current;

last := current;

4. we can create links, display them and dispose them too in this simple way.
for i:= 1 to 10 do
createlinks(i);

Displaylinks;
disposelinks;

5. Program13 follows:



Program program13;
(* Author: Rao S Lakkaraju *)
(* Pointer Basics - Link Lists *)
uses crt,graph;

type point = ^link;
link = record
data : integer;
next : point;
end;

var i,linktot : integer;
first,last,current : point;

Procedure createlinks(i:integer);
begin
new(current);
current^.data := i;
current^.next := nil;
if first = nil then first := current
else last^.next := current;

last := current;
linktot := linktot + 1;

writeln('last data= ', last^.data);

end;

Procedure Displaylinks;
begin

textbackground(white);
textcolor(red);

Current := first;
repeat
write(current^.data, ' ');
current := current^.next;
until current = nil;
end;

Procedure disposelinks;
{ Store next link address and dispose first link}
{Make next link address as first link}
{Repeat until there are no more next links}
begin
repeat
{current := first;}
current := first^.next;
dispose(first);
first := current^.next;
until first = nil;
end;

begin
clrscr;
first := nil;
linktot := 0;

for i:= 1 to 10 do
createlinks(i);

Displaylinks;
disposelinks;

writeln;
writeln('Total Links in the List = ',linktot);
writeln('My thirteenth Pascal program');
readln;
end.


SUMMARY: It is very easy to create links in Pascal. Create a typed record with desired elements in it, the last element should be a pointer to link to the next record. Keep track of first, Last and current records while adding to the List. We will have a linked list in Pascal in no time.

Thursday, November 5, 2009

12. Pointer Basics in Free Pascal

In Pascal there is what is called dynamic allocation of memory. We reserve space when we need it and dispose it off after using it. As each space in memory has an address, we grab the addresses depending upon our needs and dispose them when they are no longer needed.

Just like integer variables which store integers only, Pointer variables store only addresses in Pascal. Just like we use variables to manipulate values in them we manipulate pointer variables also with their values(addresses). Additionally with pointers, we can manipulate the values in spaces they point in the memory.

First we have to declare them just like anything else.
Ex: var p,q : ^integer;
the ^ before integer saying that p and q are pointers which store only addresses of integer spaces in memory.

Next we grab space in memory for pointers.
ex: new(p);
Give me new space to store integer and put that address in pointer p.

Next put an integer(3) in that space.
ex: p^ := 3;
p^ means put 3 in the space specified by the address in p.

Make pointer q also contain same address as p. ex: q := p;

Now get a new space for p. store 4 in it. ex: new(p); p^:= 4;

Display the contents of spaces pointed by p and q with writeln. ex: writeln('p = ', p^,' q = ',q^);

After we are done finished using spaces, we can dispose the spaces.
ex: dispose(p); dispose(q);

The following program describes all.

Program Program12;
(* Author: Rao S Lakkaraju *)
(* Pointer Basics *)
uses crt,graph;

var p,q : ^integer; { declares p and q as an integer pointer variables }

begin
clrscr;
new(p); { allot space for an integer for pointer p }
p^ := 3; { Dereference p to store integer 3 in that location }
q := p; { make pointer q pointing to the same location as p }
new(p); {allot another space for an integer for pointer p }
p^:= 4; (* store 4 in that location*)

{ Display the value in the location pointed by p and q }
writeln('p = ', p^,' q = ',q^);

dispose(p); { destroy pointer p and release memory }
dispose(q); (* destroy pointer q and release memory *)

writeln('My twelth Pascal program');
readln;
end.

Summary: Pointer variables are used for dynamic allocation of memory in Pascal. We can grab the memory we need, we use the space as we need and dispose it off after our use. Since we placed the memory addresses in pointers, pointers are our contacts to the memory spaces.