Sunteți pe pagina 1din 4

Ministerul Educaiei al Republicii Moldova

Universitatea Tehnic a Moldovei


Facultatea Calculatoare, Informatic i Microelectronic
Catedra Automatic i Tehnologii Informaionale

Lucrare de laborator Nr. 3


la disciplina

Grafica pe calculator
Tema: Desenarea graficelor funciilor utiliznd
algoritmul fereastr/poart

Elaborat de:

St. gr. AI-121,


ve Sergiu

Lect.sup.,

Controlat de:

Plotnic Constantin

Chiinu 2014

Scopul lucrrii:
Obinerea cunotinelor practice n realizarea transformrilor geometrice 2D i 3D a imaginilor
Sarcina lucrrii:
1. De a elabora un program pentru sintezarea i afiarea graficelor funciilor conform
variantelor (tabela 1)
2. S se foloseasc transformarea fereastr-poart.
Varianta: Y = arccos(x)
Codul surs
#include <iostream>
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<math.h>
#include<dos.h>
#include<complex.h>
using namespace std;
float XFm,YFm,XFM,YFM;
int XPm,YPm,XPM,YPM;
float Sx,Sy,Tx,Ty;

//--------------------------Initial functions---------------------------------//
float f1(float x){
return acos(x);
}
//----------------------Window initialization---------------------------------//
void WindowInit(float x1,float y1, float x2, float y2){
XFm=x1;
YFm=y1;
XFM=x2;
YFM=y2;
}
//------------------------Transform window/port-------------------------------//
void TransformWG(){
if(XFM>XFm&&YFM>YFm){
Sx=(XPM-XPm)/(XFM-XFm);
Sy=(YPM-YPm)/(YFM-YFm);
Tx=-Sx*XFm+(XPM-XPm-Sx*(XFM-XFm))/2;
Ty=YPm-Sy*YFm+(YPM-YPm-Sy*(YFM-YFm))/2;
}
else Sx=Sy=Tx=Ty=0;
}
//--------------------------Port initialization-------------------------------//
void Port(int x1,int y1,int x2,int y2)
{ XPm=x1;
YPm=y1;
XPM=x2;

YPM=y2;
TransformWG();
setviewport(x1,y1,x2,y2,1);
}
//--------------------------Transform of coeficients--------------------------//
int Xdisplay(float xf)
{return (int)(xf*Sx+Tx);}
int Ydisplay(float yf)
{return (int)(YPM-(yf*Sy+Ty));}
int XPmin,YPmin, XPmax,YPmax;
//-----------------------Drawing the system of coordinates--------------------//
void DrawSystem(float Xmin,float Xmax,float Ymin, float Ymax){
setlinestyle(DASHED_LINE,1,1);
moveto(Xdisplay(Xmin),Ydisplay(0));
lineto(Xdisplay(Xmax),Ydisplay(0));
moveto(Xdisplay(0),Ydisplay(Ymin));
lineto(Xdisplay(0),Ydisplay(Ymax));
outtextxy(getmaxx()/2-20, 0, "y");
outtextxy(getmaxx()-30, getmaxy()/2-30, "x");
}
//-------------------------Full Drawing---------------------------------------//
void Graphic(float Xmin,float Xmax,float Ymin, float Ymax,float pas,float
(*f)(float),int XPmin,int YPmin,int XPmax,int YPmax){
float x,y;
WindowInit(Xmin,Ymin,Xmax,Ymax);
Port(XPmin,YPmin,XPmax,YPmax);
DrawSystem(Xmin,Xmax,Ymin,Ymax);
setlinestyle(SOLID_LINE,1,1);

for(x=Xmin+pas;x<Xmax;x+=pas)
putpixel(Xdisplay(x),Ydisplay((*f)(x)),WHITE);
}
int main()
{ initwindow(700,500,"Laboratorul nr.3");
setcolor(8);
//----------------------------1st function(Y)---------------------------------//
XPmin=10;
YPmin=10;
XPmax=getmaxx()-10;
YPmax=getmaxy()-10;
Graphic(-5,5,-5,5,0.001,f1,XPmin,YPmin,XPmax,YPmax);
//----------------------------------------------------------------------------//
XPmin=XPmin;
XPmax=XPmax;
Port(XPmin,YPmin,XPmax,YPmax);
setcolor(15);
outtextxy(XPmin/30+10,20,"Graficul functiei Y=arccos(x)");
getch();
return 0;
}

Rezultatul execuiei

Concluzie
n urma efecturii acestei lucrri de laborator am aflat cum pot fi desenate graficele funciilor n
C++. Pentru aceasta exist un algoritm denumit Fereastr/Poart. Cu ajutorul lui poate fi desenat
orice grafic al funciilor.