180 likes | 350 Views
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
E N D
Computer GraphicsLine Drawing FunctionsPoint 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
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
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;
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(); }
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(); }
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(); }
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);
Drawing Lines .. 2 int main(void) { int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, "\\tc\\bgi"); Moveto(cx,cy); Linerel(200,0); getch(); closegraph(); }
Drawing Lines .. 3 • Lineto(int x, int y) • Draws a line from the current position to the given point.
Drawing Lines .. 3 int main(void) { int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, "\\tc\\bgi"); Moveto(cx,cy); Lineto(100,200); getch(); closegraph(); }
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); } }
Main function void main() { int driver=DETECT,mode; initgraph(&driver,&mode,"C:\\tc\\bgi"); lineDDA(100,100,401,401); getch(); closegraph(); }
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;
while(x<=x1) { X++; if(d<=0) { d+=c1; } else { d+=incrne; y++; } putpixel(x,y,RED); } } dx=x1-x0; dy=y1-y0; d=2*dy-dx; c1=2*dy; c2=2*(dy-dx); if(x1>x0) { x=x0; y=y0; putpixel(x,y,RED);
else { x=x0; y=y0; putpixel(x,y,YELLOW); while(x>=x1) { if(d<=0) { d-=c1; x--; } else { d-=c2; x--; y--; } putpixel(x,y,RED); } } getch(); closegraph(); }