0% found this document useful (0 votes)
87 views11 pages

MCSL-054 (II) Solved Assignment

Uploaded by

Rajni Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views11 pages

MCSL-054 (II) Solved Assignment

Uploaded by

Rajni Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Solved Assignment By

Ignougroup.com

MCSL053 – (MCS-53)
(Computer Graphics and Multimedia)

Q1. Write a program in C/C++ using OpenGL to draw a Triangle of


orange color and inside that draw a Circle of green color.
Ans:

#include <windows.h>

#include <gl/glut.h>

#include <math.h>

const float PI=3.14;

void drawCircle(){

glBegin(GL_LINE_LOOP);

glColor3f(1.0,0.0,0.0);

for(int i =0; i <= 300; i++){

double angle = 2 * PI * i / 300;

double x = 5*cos(angle);

double y = 5*sin(angle);

glVertex2d(x,y);

glEnd();

void drawRect(){

glColor3f(0.0,0.0,1.0);

[email protected]
Solved Assignment By
Ignougroup.com

glRectf(-5.0,5.0,5.0,-5.0);

void init(void){

glClearColor(0.0,1.0,0.0,0.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-10.0,10.0,-10.0,10.0,-10.0,10.0);

void display(void)

glClear(GL_COLOR_BUFFER_BIT);

drawRect();

drawCircle();

glutSwapBuffers();

int main(int argc, char** argv){

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

glutInitWindowSize(320,320);

glutInitWindowPosition(50,50);

glutCreateWindow("2D Shapes");

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;

[email protected]
Solved Assignment By
Ignougroup.com

Q2. Write a program in C/C++ using OpenGL to draw a hard wire


house as shown in figure given below using openGL.
Ans: #include<windows.h>

#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800.0,0.0,600.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,1.0f);
glVertex2i(100,100);
glVertex2i(200,100);
glVertex2i(200,200);
glVertex2i(100,200);
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex2i(100,200);
glVertex2i(150,300);
glVertex2i(200,200);
glEnd();
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGl Window");
myInit();

[email protected]
Solved Assignment By
Ignougroup.com

glutDisplayFunc(myDisplay);
glutMainLoop();
}

Q3. Write a program in C or C++ to implement Scan-Line Polygon


Filling Algorithm.
Ans:

#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct Node
{
int x;
int y;
struct Node* next;
};

void fill (int pt[][2], int clr);


void floodfill4 (int x, int y, int oldclr, int newclr);
void insert (int x, int y, struct Node** last);

void main()
{
int i, j;
int pt[3][2];
int clr;

printf ("This program demonstrates filling a polygon.\n");


printf ("Enter the x- and y-coordinates for three points:\n");
for (i=0; i<3; i++)
for (j=0; j<2; j++)
scanf ("%d", &pt[i][j]);

printf ("Enter the fill-colour: (Any number from 1 to 14) ");


scanf ("%d", &clr);
fill (pt, clr);
}

void fill (int pt[][2], int clr)


{
int gd = DETECT, gm;

[email protected]
Solved Assignment By
Ignougroup.com

int seedx, seedy;

initgraph (&gd, &gm, "..\\bgi");

setcolor (WHITE);
line (pt[0][0], pt[0][1], pt[1][0], pt[1][1]);
line (pt[1][0], pt[1][1], pt[2][0], pt[2][1]);
line (pt[2][0], pt[2][1], pt[0][0], pt[0][1]);
getch();

seedx = (pt[0][0] + pt[1][0] + pt[2][0]) / 3;


seedy = (pt[0][1] + pt[1][1] + pt[2][1]) / 3;

floodfill4 (seedx, seedy, BLACK, clr);


getch();

closegraph();
return;
}

void floodfill4 (int x, int y, int oldclr, int newclr)


{
struct Node* first, *last, *tmp;

first = (struct Node*) malloc (sizeof (struct Node));


if (first == NULL)
{
closegraph();
fprintf (stderr, "floodfill4: Out of memory.\n");
exit (2);
}
if (oldclr == newclr)
{
free (first);
return;
}

first->x = x;
first->y = y;
first->next = NULL;
last = first;

while (first != NULL)


{
putpixel (x, y, newclr);

[email protected]
Solved Assignment By
Ignougroup.com

if (getpixel (x, y-1) == oldclr)


{
putpixel (x, y-1, newclr);
insert (x, y-1, &last);
}

if (getpixel (x, y+1) == oldclr)


{
putpixel (x, y+1, newclr);
insert (x, y+1, &last);
}

if (getpixel (x-1, y) == oldclr)


{
putpixel (x-1, y, newclr);
insert (x-1, y, &last);
}

if (getpixel (x+1, y) == oldclr)


{
putpixel (x+1, y, newclr);
insert (x+1, y, &last);
}

tmp = first;
first = first->next;
x = first->x;
y = first->y;
free (tmp);
}
}

void insert (int x, int y, struct Node** last)


{
struct Node* p;
p = (struct Node*) malloc (sizeof (struct Node));
if (p == NULL)
{
closegraph();
fprintf (stderr, "\n insert: Out of memory.\n");
exit (2);
}

p->x = x;
p->y = y;

[email protected]
Solved Assignment By
Ignougroup.com

p->next = NULL;
(*last)->next = p;
*last = (*last)->next;
}

Q4. Write a program in C/C++ to implement Cohen-Sutherland line


clipping algorithm. In this implementation consider two cases of a
line: totally visible, totally invisible, against the rectangular clipping
window.
Ans: #include<stdio.h>

#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>

typedef struct coordinate


{
int x,y;
char code[4];
}PT;

void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);

void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;

printf("\nEnter x1 and y1\n");


scanf("%d %d",&p1.x,&p1.y);
printf("\nEnter x2 and y2\n");
scanf("%d %d",&p2.x,&p2.y);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);

drawline(p1,p2);

[email protected]
Solved Assignment By
Ignougroup.com

delay(500);
cleardevice();

delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);

switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}

delay(5000);
closegraph();
}

void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}

void drawline(PT p1,PT p2)


{
line(p1.x,p1.y,p2.x,p2.y);
}

PT setcode(PT p) //for setting the 4 bit code


{
PT ptemp;

[email protected]
Solved Assignment By
Ignougroup.com

if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';

if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';

if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';

if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';

ptemp.x=p.x;
ptemp.y=p.y;

return(ptemp);
}

int visibility(PT p1,PT p2)


{
int i,flag=0;

for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}

if(flag==0)
return(0);

for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}

[email protected]
Solved Assignment By
Ignougroup.com

if(flag==0)
return(1);

return(2);
}

PT resetendpt(PT p1,PT p2)


{
PT temp;
int x,y,i;
float m,k;

if(p1.code[3]=='1')
x=150;

if(p1.code[2]=='1')
x=450;

if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;

for(i=0;i<4;i++)
temp.code[i]=p1.code[i];

if(temp.y<=350 && temp.y>=100)


return (temp);
}

if(p1.code[0]=='1')
y=100;

if(p1.code[1]=='1')
y=350;

if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;

for(i=0;i<4;i++)

[email protected]
Solved Assignment By
Ignougroup.com

temp.code[i]=p1.code[i];

return(temp);
}
else
return(p1);
}

[email protected]

You might also like