SOLVED CS602 Assignment 2 Solution and Discussion

Re: CS602 Assignment 2 Solution and Discussion Spring 2020
Assignment No. 02
Semester: Fall 2020
CS602: Computer Graphics
Total Marks: 20Due Date: Dec 17, 2020
Objective
The objective of this assignment is to;
Learn and practice basic concepts of computer graphics like pixels, lines, text etc using DevC++. (You can download DevC++ setup from LMS)
Learn to clip lines and rigid bodies by using graphics library functions.
Learn the concepts of Sutherland and Hodgman’s polygonclipping algorithm.
Instructions:
You have to do the following steps first in order to perform the task. Install DevC++ from your LMS.
 Read out the file “Add graphics in Dev cpp” thoroughly and follow the instructions in the file properly provided to you on your LMS.
Please read the following instructions carefully before submitting assignment:  You should consult the recommended books, PowerPoint slides, Handouts and video lectures to clarify your concepts.
 You can take any help from your provided labs.
 It should be clear that your assignment will not get any credit if:
• The assignment is submitted after due date.
• The assignment is copied from Internet or from any other student.
• The submitted assignment does not open or file is corrupt.
Submission
You are required to submit your solution through LMS in doc file. You are also required to enter the screenshot of the output of your program.
Note: No assignment will be accepted after the due date through email in any case (load shedding, server down, internet malfunctioning etc.).
It is recommended to upload solution file at least two days before its closing date.
For any query about the assignment, contact at [email protected]TASK Marks=20
This task is based on the concepts of clipping algorithms, you are required to implement the Sutherland and Hodgman’s polygonclipping algorithm in Dev C++. After executing the program, the program should ask the user to enter the number of vertices of polygon. After entering number of vertices, the program will ask the user to enter coordinates of vertices as shown in the image below:Now the user needs to just press a button, a polygon will be drawn with white color the second press of a button to clip the polygon as shown in the image below:

Solution:
Program: #include<stdio.h> #include<graphics.h> #include<conio.h> #include<stdlib.h> int main() { int gd,gm,n,*x,i,k=0; //window coordinates int wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340; int w[]={220,140,420,140,420,340,220,340,220,140};//array for drawing window detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics printf("Window:"); setcolor(RED); //red colored window drawpoly(5,w); //window drawn printf("Enter the no. of vertices of polygon: "); scanf("%d",&n); x = malloc(n*2+1); printf("Enter the coordinates of points:\n"); k=0; for(i=0;i<n*2;i+=2) //reading vertices of polygon { printf("(x%d,y%d): ",k,k); scanf("%d,%d",&x[i],&x[i+1]); k++; } x[n*2]=x[0]; //assigning the coordinates of first vertex to last additional vertex for drawpoly method. x[n*2+1]=x[1]; setcolor(WHITE); drawpoly(n+1,x); printf("\nPress a button to clip a polygon.."); getch(); setcolor(RED); drawpoly(5,w); setfillstyle(SOLID_FILL,BLACK); floodfill(2,2,RED); gotoxy(1,1); //bringing cursor at starting position printf("\nThis is the clipped polygon.."); getch(); cleardevice(); closegraph(); return 0; }

Solution:
Program: #include<stdio.h> #include<graphics.h> #include<conio.h> #include<stdlib.h> int main() { int gd,gm,n,*x,i,k=0; //window coordinates int wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340; int w[]={220,140,420,140,420,340,220,340,220,140};//array for drawing window detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics printf("Window:"); setcolor(RED); //red colored window drawpoly(5,w); //window drawn printf("Enter the no. of vertices of polygon: "); scanf("%d",&n); x = malloc(n*2+1); printf("Enter the coordinates of points:\n"); k=0; for(i=0;i<n*2;i+=2) //reading vertices of polygon { printf("(x%d,y%d): ",k,k); scanf("%d,%d",&x[i],&x[i+1]); k++; } x[n*2]=x[0]; //assigning the coordinates of first vertex to last additional vertex for drawpoly method. x[n*2+1]=x[1]; setcolor(WHITE); drawpoly(n+1,x); printf("\nPress a button to clip a polygon.."); getch(); setcolor(RED); drawpoly(5,w); setfillstyle(SOLID_FILL,BLACK); floodfill(2,2,RED); gotoxy(1,1); //bringing cursor at starting position printf("\nThis is the clipped polygon.."); getch(); cleardevice(); closegraph(); return 0; }