Sie sind auf Seite 1von 20

Computer Graphics

Line Drawing Functions


Point structure

Asima Latif
19-Nov-2009
Main Mile stones that we will cover in this course

 Basic Concepts
 Basic Shapes, Line Drawing Algorithms
 Projection / Display of Graphics
 Transformation
 Parametric Equations
 Graphics Pipeline OpenGL
 Two dimensional Graphics
 3D Graphics
12/08/21 2
Book / Other Material
 Computer Graphics FS Hill or the hevern one

 Turbo C++ remember that there is a BGI


folder

 Visual C++ , OpenGL GLUT Library

 Java3D optional

12/08/21 3
Origin – Center of the screen
 To get the center of the screen

 We have two functions


 Getmaxx()
 Getmaxy()

 Center of Screen
 Int cx = getmaxx()/2;
 Int cy = getmaxy()/2;
12/08/21 4
Complete Program with cx, cy
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "\\tc\\bgi");
int radius=100;
Int cx=getmaxx()/2;
Int cy=getmaxy()/2;
circle(cx, cy, radius);
getch();
closegraph();
}
12/08/21 5
Drawing Lines .. 1
Line(int x1,int y1,int x2, int y2);
 Draws a line between (x1,y1) AND (x2,y2)
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "\\tc\\bgi");
Line(cx,cy,cx+200,cy);
getch();
closegraph();
}
12/08/21 6
Drawing Lines .. 1
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "\\tc\\bgi");
Line(cx,cy,cx+200,cy);
Line(cx,cy,cx,cy-200);
getch();
closegraph();
}

12/08/21 7
Drawing Line …2
 Linerel(int dx, int dy)
 Draws a line from the current position to the
distance dx, and dy

 Curret Position CP is achived through using


the function moveto(int x, int y);

12/08/21 8
Drawing Lines .. 2
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "\\tc\\bgi");
Moveto(cx,cy);
Linerel(200,0);
getch();
closegraph();
}

12/08/21 9
Drawing Lines .. 3
 Lineto(int x, int y)

 Draws a line from the current position to the


given point.

12/08/21 10
Drawing Lines .. 3
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "\\tc\\bgi");
Moveto(cx,cy);
Lineto(100,200);
getch();
closegraph();
}

12/08/21 11
Line Drawing Algorithm
Digital Differential Analyzer
DDA
void lineDDA(int xa,int ya,int xb,int yb)
{
int dx,dy,steps;
dx=xb-xa;
dy=yb-ya;
float xincrement,yincrement,x,y;
if (abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/steps;
yincrement=dy/steps;
x=xa,y=ya;
putpixel(round(x),round(y),2);
for (int k=1;k<=steps;k++)
{
x=x+xincrement;
y=y+yincrement;
putpixel(round(x),round(y),2);
} }
12/08/21 13
Main function
void main()
{
int driver=DETECT,mode;
initgraph(&driver,&mode,"C:\\tc\\bg
i");
lineDDA(100,100,401,401);
getch();
closegraph();
}

12/08/21 14
LINE MIDPOINT
ALGORITHM(BRESHMAN ALGORITHM
 void main()
 {
 int gd=DETECT,gm,x0,y0,x1,y1,dx,dy,d,x,y,c1,c2;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 clrscr();
 cout<<"\t\tMID POINT ALGORITHM FOR LINE\n";
 cout<<"\n enter initial coordinates\t";
 cin>>x0>>y0;
 cout<<"\nenter final coordinates\t\t";
 cin>>x1>>y1;
12/08/21 15
while(x<=x1)
dx=x1-x0; {
dy=y1-y0; X++;
if(d<=0)
d=2*dy-dx;
{
c1=2*dy; d+=c1;
c2=2*(dy-dx); }
if(x1>x0) else
{ {
d+=c2;
x=x0; y=y0;
y++;
putpixel(x,y,RED); }
putpixel(x,y,RED);
} 16

}
else else
{ {
x=x0; y=y0; d-=c2;
putpixel(x,y,YELLOW); x--;
while(x>=x1) y--;
{
}
putpixel(x,y,RED);
if(d<=0)
}
{
}
d-=c1;
getch();
x--;
closegraph();
} }
  17
Translation
void main()
cin>>j;
{
cout<<"\n Now enter co-
int
ordinates for each point\n\t";
gd=DETECT,gm,x[10],y[1
0],a,b,i,j,dx,dy; for(i=1;i<=j;i++)
clrscr(); cin>>x[i]>>y[i];
initgraph(&gd,&gm,"c:\\tc\\bg setcolor(BLUE);
i"); for(i=1;i<=j-1;i++)
cout<<" To draw polygon { line(x[i],
enter the number of sides for
polygon\t";
y[i],
x[i+1],
y[i+1]);
}
Translation…
line(x[i],y[i],x[0],y[0]); { line(x[i]+dx,
cout<<"\n Enter the y[i]+dy,
co-ordinates for x[i+1]+dx,
translation\t"; y[i+1]+dy);
cin>>dx>>dy; }
cout<<"\n After line(x[i]+dx,
transalation the y[i]+dy,
polygon is"; x[0]+dx,
for(i=1;i<=j-1;i++) y[0]+dy);
getch();
closegraph();
}
That’s all for Today

Das könnte Ihnen auch gefallen