2bytesprog2 course_2014_c9_graph

25
Prog_2 course- 2014 2 bytes team Kinan keshkeh IT Engineering-Damascus University 3 rd year

Upload: kinan-ke

Post on 04-Aug-2015

29 views

Category:

Software


0 download

TRANSCRIPT

Prog_2 course- 2014

2 bytes team

Kinan keshkeh

IT Engineering-Damascus University

3rd year

GRAPH

Introduction

In all programs we were using TXTmode (all outputs

were in Text).

In PASCAL there is a Graph Unit, which allow us

to change into Graphicmode and draw diagrams.

Change into Graph mode

Uses Graph; procedure my_initgraph;

var

gd, gm, error,detect:integer;

begin

gd:=detect;

initgraph(gd,gm,'c:\evgrga.bgi');

error:=graphresult;

if error<>grok then

begin

writeln('graphic error',GraphErrorMsg(error));

halt(1);

end;

end;

1- Uses Graph : to include Graph library.

2- gd:=detect : detect ,to determine the kind of

display card .

3- initgraph(gd,gm,'c:\evgrga.bgi')

that in (2) display system : ex: gm=VGAHI , so the screen

will be 2640*480*16 (16 number of colors ).

4- error:=graphresult : to insure if initgraph success in

changing to Graph mode, it return a value if success or error.

5-GraphErrorMsg(error) : display an error message , if an

error happens when changing into graphic mode.

6-Halt: to exit the procedure.

Note: If we have this folder to save code file in it : C:\MyFolder\pro.pas

Go to (Turbo Pascal) folder , open ‘BGI’ folder

1)Copy this file EGAVGA.BGI and paste it in ‘C:\MyFolder\’

2)Copy this fileGRAPH.TPU’ unit from ‘Units’ folder (in Turbo

Pascal folder).and paste it in the path ‘C:\MyFolder\’ too.

3)From pascal program , choose File -> Change dir ->

C:\MyFolder\ .

* That to initialize Graph environment in pascal !

How to paint a Normal function:

1) The function : Y= f(x) ex: (y=2x) .

2) X domain: ex: x in [ x0=-100, xn=100] .

4) Painting step dx : dx=(xn-x0)/n .

3) N number of points.

5) The Algorithm :

x1=x0; y1=F(x0);

For(i:=1) to (N) do

begin

x2 = x0+i*dx;

y2 = F(x2);

line(x1,y1,x2,y2);

x1:=x2; y1:=y2

end;

Example :

paint ( Y=f(x) = x ) : 1- (f(x) = x ) 2- let x in [0,50] 3- n=100

4-dx=(50-0)/100 = 0.5 ;

5- Algorithm:

x1=x0; y1=x0;

For(i:=1) to (n) do

begin

x2 = x0+i*dx;

y2 = x2;

line(x1,y1,x2,y2);

x1:=x2; y1:=y2

end;

x1

y1

x2

y2

x2

y2

How to paint a Polar function:

1) The function :R= F(u) ex: (y=cos(x) ) .

2) U domain: ex: U in [ u0=-100, un=100] .

4) Painting step du : du=(Un-U0)/n .

3) N number of points.

5) The Algorithm :

x1=F(u0)*cos(u0); y1=F(u0)*sin(u0);

For(i:=1) to (N) do

begin

U= u0+ i*du;

x2=F(u)*cos(u);

y2=F(u)*sin(u);

line(x1,y1,x2,y2);

x1:=x2; y1:=y2

end;

x

y

U

the Coordinates

(wXb,wYb)

paper bottom

(wXt,wYt)

top

(vXb,vYb)

screen bottom

(vXt,vYt)

top

Coordinates appropriate

- If you have On paper x in [0..460]

..it would be On Screen x in [0..230]

- So each two points on screen , one point on paper .

(6,0) paper -> (3,0) screen

- So the Ratio:

Sx = | vXb – vXt | / | wXt – wXb |

Sy = | vYb – vYt | / | wYt – wYb |

0

460

230

0

vXt vXb

wXb wXt

3

6

1)Increasing and decreasing coordinates:

EX:

If the paper Coordinates are 640*320 , and the screen

Coordinates are 1280*640 .

What are the coordinates on screen of the point (4,6)

on paper ?

answer :

Sx=(1280-0) / (640-0) = 2

Sy=(640-0) / (320-0) = 2

the point on paper(4,6) -> (8,12) on screen

Xv= (x-wXb)*Sx + vXt

2)Displacement coordinates:

0 200 vXt vXb

-50 50 wXb wXt

Ex: the point (50,0) -> Xv=(50+50)*2 + 0 = 200

(x on screen).

paper

Screen

Yv= (y-wYb)*Sy + (GetMaxY – vYb)

0

0

paper

wYb 100

100 vYt wYt

vYb

Screen

GetMaxY = 100

Ex: the point (0,100) -> Yv=(100-0)*1 +(100-100)

= 100 (x on screen).

Some important functions and procedures :

Line(x1,y1,x2,y2) : draw a line between the two points.

Circle(x,y,radius) : draw a circle .

Rectangle(x1,y1,x2,y2): draw a rectangle between the two points .

Setcolor(color): change the color where the color is a number from

[0..15] and each number is a color.

Setviewport(x1,y1,x2,y2,clipOn/clipOff): determine a specific display

window

clipOn: inside it we can see, clipOff: outside it we can see.

TXTout(“Hello world”) or TXToutXY(50,20,“Hello world”) : print a

text on screen with Graphicmode (at specific coordinate),

whereas at TXTmode we use writeln,write .

Lets make it

real :D !!

What u do in Exams

Normal functions: F(x)=1+x2

:الخط البياني للتابعارسم :س2+x1F(x)=

]-100+,100[ضمن المجال xحيث

2000N= Program draw;

const n=2000; x0=-100; xm=100;

dx=0.1 ; { (xm-x0)/n= [100-(-100)]/2000=0.1}

uses graph;

var

x1,x2,y1,y2:real;

nx1,nx2,ny1,ny2:real;

maxx , maxy , i : integer;

alpha , beta :real;

procedure my_initgraph; {initialize screen for drawing}

var

gd,gm,error,detect :integer;

begin

gd:=detect;

initgraph(gd,gm,'c:\evgrga.bgi');

error:=graphresult;

if error<>grok then

begin

writeln('graphic error',GraphErrorMsg(error) );

halt(1);

end;

function f(x:real):real

begin

f:=x*x+1;

end;

Begin

my_initgraph;

x1:=x0; y1:=f(x0);

maxx:=getmaxx;

maxy:=getmaxy;

alpha:=maxx/(xm-x0);

beta:=maxy/abs( f(xm) - f(x0) );;

line(0,maxy div 2,maxx,maxy div 2);

line(maxx div 2,0,maxx div 2,maxy);

for i:=1 to n do

begin

x2:=x0+i*dx;

y2:=f(x2);

nx1:=round(x1*alpha)+maxx div 2;

nx2:=round(x2*alpha)+maxx div 2;

ny1:= -round(y1*beta)+maxy div 2;

ny2:= -round(y2*beta)+maxy div 2;

line(nx1,ny1,nx2,ny2);

x1:=x2; y1:=y2;

end;

close graph;

End;

appropriate the coordinate

Displacement

Draw the axis

Polar functions: F(u)=1+sin(u)

منحني التابعارسم :س

+sin(u)1F(u)=

0.02du=, 0 =0U

Program draw;

const n=2500; u0=0; du=0.02;

uses graph;

var

x1,x2,y1,y2:real;

nx1,nx2,ny1,ny2:real;

maxx , maxy,i :integer;

alpha ,beta :real;

procedure my_initgraph; {initialize screen for drawing}

var

gd,gm,error,detect :integer;

begin

gd:=detect;

initgraph(gd,gm,'c:\evgrga.bgi');

error:=graphresult;

if error<>grok then

begin

writeln('graphic error',GraphErrorMsg(error) );

halt(1);

end;

function f(u:real):real

begin

f:=1+sin(u);

end;

Begin

my_initgraph;

x1:=f(u0)*cos(u0); y1:=f(u0)*sin(u0);

maxx:=getmaxx;

maxy:=getmaxy;

alpha:=maxx/ ( n*du)

beta:= maxy/abs( f(u0+n*du) - f(u0) ); line(0,maxy div 2,maxx,maxy div 2);

line(maxx div 2,0,maxx div 2,maxy);

for i:=1 to n do

begin

u:=u0+i*du;

x2:=f(u)*cos(u);

y2:=f(u)*sin(u); nx1:=round(x1*alpha)+maxx div 2;

nx2:=round(x2*alpha)+maxx div 2;

ny1:= -round(y1*beta)+maxy div 2;

ny2:= -round(y2*beta)+maxy div 2;

line(nx1,ny1,nx2,ny2);

x1:=x2; y1:=y2;

end;

close graph;

End;

Draw the axis

The End of the course !!

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team