Vous êtes sur la page 1sur 11

IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES AIM: To write a C program to set attributes to line, circle and ellipse

ALGORITHM: Step 1: Include the necessary header files and the variables needed for the program. Step 2: Select the driver that supports multiple drawing colors. Step 3: Get the users choice for the object to be drawn. Step 4: For line, Step 4.1: Getmaxcolor() and setcolor() functions are used to modify the color attribute. Step 4.2: using the string array of different linr styles, depict the various types of lines. Step 5: For circle, Step 5.1: Use the setcolor() function to vary the color of the circle. Step 6: For ellipse, Step 6.1: Use the setcolor() function to vary the boundary color. Step 6.2: Use the Setfillstyle() and fillellipse() functions to change the fillstyle of the ellipse. Step 7: End the program.

DESCRIPTION: Output primitives have geometric and non-geometric attributes. Geometric attributes, such as the character height, affect the size and shape of a primitive, whereas non-geometric attributes are qualities such as colour, line style, etc. The output primitives Such as Line, Circle and Ellipse are associated with set of attributes such as Line (color and Line Style), Cicrle (Color) and Ellipse (Color and Patterns).

CODING:

#include<graphics.h> /* include the necessary header files*/ #include<stdlib.h> #include<stdio.h> #include<conio.h> int main(void) { /* select a driver and mode that supports */ /* multiple drawing colors.*/ int gdriver=EGA,DETECT,gmode=EGAHI,errorcode; int color,maxcolor,x,y,s,ch,ch1,ch2,i; int midx,midy; int radius=100; int xradius=100,yradius=50; char msg[80]; char *lname[]={"Solid Line", "Dotted Line", "Center Line", "Dashed Line", "Usebit Line"}; /* initialize graphics and local variables */ initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); /* read result of initialization */ errorcode=graphresult(); if (errorcode!=grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch();

exit(1); /* terminate with an error code */ } do{ printf("\n1.Line\n2.Circle\n3.Ellipse\n"); /* get the user choice*/ printf("\nEnter Your choice\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Attribute: 1.Color 2.Style:\n"); scanf("%d",&ch1); switch(ch1) { case 1: maxcolor=getmaxcolor(); /* use predefined methods to change the color*/ x=getmaxx()/2; y=getmaxy()/2; for(color=1;color<=maxcolor;color++) { cleardevice(); setcolor(color); line(100,100,100,300); sprintf(msg,"Color:%d",color); outtextxy(x,y,msg); getch(); } closegraph(); break; case 2: initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

for(s=0;s<5;s++) { setlinestyle(s,1,1); /* pre defined method for linestyle*/ line(20,20+s*50,120,120+s*50); outtextxy(125,120+s*50,lname[s]); } getch(); closegraph(); break; } break; case 2: initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); midx=getmaxx()/2; midy=getmaxy()/2; maxcolor=getmaxcolor(); /* draw circles of different colors*/ for(color=1;color<=maxcolor;color++) { cleardevice(); setcolor(color); /* draw the circle */ circle(midx,midy,radius); /* clean up */ getch(); } closegraph(); break; case 3: printf("\n1.pattern 2.colour\n"); /* get choice for color or style of eclipse*/

printf("\nEnter your choice:\n"); scanf("%d",&ch2); switch(ch2) { case 1: /* initialize graphics and local variables */ initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); midx=getmaxx()/2; midy=getmaxy()/2; /* loop through the fill patterns */ for(i=EMPTY_FILL;i<USER_FILL;i++) { /* set fill pattern */ setfillstyle(i,getmaxcolor()); /* draw a filled ellipse */ fillellipse(midx, midy, xradius, yradius); getch(); } closegraph(); break; case 2: initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); maxcolor=getmaxcolor(); for(color=1;color<=maxcolor;color++) { cleardevice(); setcolor(color); ellipse(100,200,0,360,xradius,yradius); getch(); } /* clean up */

closegraph(); break; } default: //exit(0); break; } } while(ch==3); return 0; } IMPLEMENTATION OF SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM AIM: To write a C program to implement Cohen Sutherland line clipping algorithm. ALGORITHM: Step 1: Declare the necessary variables and header files for the program. Step 2: Create an enumeration to assign the region codes for each side of the clipping window. Step 3: In the clip() function, check the value of the outcode and assign the value of accept and done. Step 4: For every point on the polygon, check its position relative to that of clipping window. Step 5: Modify the value of x and y as, Step 5.1: Assign x = x0 + (x1 x0) * (ymax y0) / (y1 y0) Step 5.2: Assign y = ymax. Step 6: In the compoutcode() function, assign the position for each point as TOP, BOTTOM, RIGHT and LEFT. Step 7: In the main function, get the sides of the polygon. Step 8: Store the coordinates of the vertices in an array and call the drawpoly() function. Step 9: Call the user defined clipping function and display the output.

Step 10: End the program.

DESCRIPTION: The Sutherland Hodgeman Algorithm is used for clipping polygons. It works by extending each line of the convex clip polygon in turn and selecting only vertices from the subject polygon that is on the visible side. This algorithm performs a clipping of a polygon against each window edge in turn. It accepts an ordered sequence of vertices v1, v2, v3... vn and puts out a set of vertices defining the clipped polygon.

CODING:

#include<stdio.h> #include<graphics.h> #include<conio.h> #include<math.h> #include<process.h> #define TRUE 1 #define FALSE 0

/* include the necessary header files*/

typedef unsigned int outcode; outcode CompOutCode(float x,float y); /* create an user defined function for the output*/ enum { TOP=0x1,

BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 }; float xmin,xmax,ymin,ymax; void clip(float x0,float y0,float x1,float y1) /* define the clipping function*/ { outcode outcode0,outcode1,outcodeOut; int accept=FALSE,done=FALSE; outcode0=CompOutCode(x0,y0); /* call the user defined function*/ outcode1=CompOutCode(x1,y1); do /* assign the values for the condition variables*/ { if(!(outcode0|outcode1)) { accept=TRUE; done=TRUE; } else if(outcode0&outcode1) done=TRUE; else { float x,y; outcodeOut=outcode0?outcode0:outcode1; /* use the tertiary operator to assign values*/ if(outcodeOut&TOP) /* reassign the value of x and y */ { x=x0+(x1-x0)*(ymax-y0)/(y1-y0); y=ymax; }

else if(outcodeOut&BOTTOM) { x=x0+(x1-x0)*(ymin-y0)/(y1-y0); y=ymin; } else if(outcodeOut&RIGHT) { y=y0+(y1-y0)*(xmax-x0)/(x1-x0); x=xmax; } else { y=y0+(y1-y0)*(xmin-x0)/(x1-x0); x=xmin; } if(outcodeOut==outcode0) { x0=x; y0=y; outcode0=CompOutCode(x0,y0); } else { x1=x; y1=y; outcode1=CompOutCode(x1,y1); } } }

while(done==FALSE); if(accept) line(x0,y0,x1,y1); outtextxy(150,20,"POLYGON AFTER CLIPPING"); rectangle(xmin,ymin,xmax,ymax); /* draw the clipping window*/ } outcode CompOutCode(float x,float y) /* define the output function*/ { outcode code=0; if(y>ymax) code|=TOP; else if(y<ymin) code|=BOTTOM; if(x>xmax) code|=RIGHT; else if(x<xmin) code|=LEFT; return code; } void main() { float x1,y1,x2,y2; /* request auto detection */ int gdriver=DETECT,gmode,n,poly[14],i; clrscr( ); printf("Enter the no of sides of polygon:"); /* get the sides of the polygon*/ scanf("%d",&n); printf("\nEnter the coordinates of polygon\n"); for(i=0;i<2*n;i++)

{ scanf("%d",&poly[i]); } poly[2*n]=poly[0]; poly[2*n+1]=poly[1]; printf("Enter the rectangular coordinates of clipping window\n"); scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax); /* get the coordinates of the clipping window*/ /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); outtextxy(150,20,"POLYGON BEFORE CLIPPING"); drawpoly(n+1,poly); rectangle(xmin,ymin,xmax,ymax); getch(); cleardevice(); for(i=0;i<n;i++) clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); /* call the clipping function*/ getch(); restorecrtmode(); }

Vous aimerez peut-être aussi