You are on page 1of 22

Guru Nanak Dev Engineering College,

Ludhiana, Punjab

Name- Amandeep Kaur


Branch – CSE A (A1)
CRN-2115010
URN-2104064
Subject Code: LPCCS-109
Subject- Computer Graphics Laboratory
Submitted To- Er. Maninder Kaur

INDEX
S.no. Title Pg No. Date Signature
1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

PRACTICAL- 1
1.1 Write a program to plot a pixel on a screen.
Program:
#include <graphics.h>
#include <iostream>
int main() {
int gd = DETECT, gm, color;
initgraph(&gd, &gm, "");
putpixel(85, 35, GREEN);
putpixel(30, 40, RED);
putpixel(115, 50, YELLOW);
putpixel(135, 50, CYAN);
putpixel(45, 60, BLUE);
putpixel(20, 100, WHITE);
putpixel(200, 100, LIGHTBLUE);
putpixel(150, 100, LIGHTGREEN);
putpixel(200, 50, YELLOW);
putpixel(120, 70, RED);
getch();
closegraph();
return 0;}

Output:
1.2 Write a program to draw a line on a screen.
Program:

#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// line for x1, y1, x2, y2
line(150, 150, 450, 150);
// line for x1, y1, x2, y2
line(150, 200, 450, 200);
// line for x1, y1, x2, y2
line(150, 250, 450, 250);
getch();
closegraph();
}

Output:
1.3 Write a program to draw a circle on a screen.
Program:

#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
circle(250, 200, 50);
getch();
closegraph();
return 0;
}

Output:
1.4 Write a program to draw any 2- dimensional figure on a screen.
Program:
#include <graphics.h>
void drawRectangle(int x1, int y1, int x2, int y2) {
rectangle(x1, y1, x2, y2);
}
void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
drawRectangle(100, 200, 400, 400); // Base
drawRectangle(150, 100, 350, 200); // Roof
drawTriangle(150, 100, 250, 50, 350, 100); // Roof triangle
drawRectangle(200, 250, 300, 400); // Door
getch();
closegraph();
return 0;

} Output:
PRACTICAL- 2
Write a program to Draw a color cube and spin it using transformation
matrices.
Program:
#include<stdlib.h>
#include<GL/glut.h>

GLfloat vertices[]={-1.0,-1.0,-1.0,1.0,-1.0,-1.0,1.0,
1.0,-1.0,-1.0,1.0,-1.0,-1.0,-1.0,1.0,
1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,1.0,1.0};

GLfloat colors[] = {0.0,0.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0,


0.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,
1.0,1.0,1.0,1.0,0.0,1.0,1.0};

GLubyte cubeIndices[]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4, 5,6,7,0,1,5,4};


static GLfloat theta[]={0.0,0.0,0.0};
static GLint axis=2;

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE, cubeIndices);
glFlush();
glutSwapBuffers();
}

void spinCube()
{
theta[axis]+=2.0;
if(theta[axis]>360.0) theta[axis]-=360.0;
glutPostRedisplay();
}

void mouse(int btn, int state, int x, int y)


{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
axis=0;
if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)
axis=1;
if(btn==GLUT_RIGHT_BUTTON&& state==GLUT_DOWN)
axis=2;
}

void myReshape(int w, int h)


{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w, 2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h, 2.0*(GLfloat)w/(GLfloat)h, -2.0,2.0,-
10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}

void main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("Spin a color cube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
glColor3f(1.0,1.0,1.0);
glutMainLoop();
}

Output:
PRACTICAL- 3
Implement the DDA algorithm for drawing line (programmer is expected to
shift the origin to the center of the screen and divide the screen into
required quadrants)
Program:
#include<iostream>
#include<graphics.h>
#include <cstdlib>
using namespace std;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
int screenWidth = getmaxx() + 1; // +1 because coordinates are 0-indexed
int screenHeight = getmaxy() + 1;
// Calculate center coordinates
int centerX = screenWidth / 2;
int centerY = screenHeight / 2;
int x1, y1, x2, y2;
cout << "Enter x1: ";
cin >> x1;
cout << "Enter y1: ";
cin >> y1;
cout << "Enter x2: ";
cin >> x2;
cout << "Enter y2: ";
cin >> y2;
// Validate user input
if (x1 < 0 || x1 >= screenWidth || y1 < 0 || y1 >= screenHeight ||
x2 < 0 || x2 >= screenWidth || y2 < 0 || y2 >= screenHeight) {
cout << "Invalid coordinates! Coordinates should be within the screen bounds.\n";
closegraph();
return 1;
}
int dx = x2 - x1;
int dy = y2 - y1;
int steps = max(abs(dx), abs(dy));
float xinc = dx / (float)steps;
float yinc = dy / (float)steps;
int x = x1 + centerX;
int y = y1 + centerY;
putpixel(x, y, WHITE);
for (int k = 1; k <= steps; k++) {
x = x + xinc;
y = y + yinc;
putpixel(x, y, WHITE);
}
getch();
closegraph();
return 0;
}

Output:
PRACTICAL- 4
Write a program to input the line coordinates from the user to generate a
line using Bresenham’s Algorithm.
Program:
#include<iostream>
#include<graphics.h>
#include <cstdlib>
using namespace std;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
int x1, y1, x2, y2;
cout << "Enter x1: ";
cin >> x1;
cout << "Enter y1: ";
cin >> y1;
cout << "Enter x2: ";
cin >> x2;
cout << "Enter y2: ";
cin >> y2;
int dx = x2 - x1;
int dy = y2 - y1;
int p= 2*dy-dx;
int x=x1;
int y=y1;
putpixel(x,y,WHITE);
while(x<=x2)
{
if(p<0)
{
p=p+2*dy;
}
else
{
p=p+2*(dy-dx);
y=y+1;
}
x=x+1;
putpixel(x,y,WHITE);
}
getch();
closegraph();
return 0;
}

Output:
PRACTICAL- 5
Write a program to generate a complete moving wheel using Midpoint
circle drawing algorithm and DDA line drawing algorithm
Program:
#include<iostream>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<conio.h>
using namespace std;
double round(double x);
void dda(float x1, float y1, float x2, float y2)
{
float m= (y2-y1)/(x2-x1);
int y=y1;
int x=0;
putpixel(x1,y1,10);
if(m<1)
{
while(x1<=x2)
{
x1++;
y1=y1+m;
y=round(y1);
putpixel(x1,y,10);
}
}
else if(m==1)
{
while(y1!=y2)
{
y1++;
x1++;
putpixel(x1,y1,10);
}
}
else if(m>1)
{
while(y1<=y2)
{
x1=x1+(1/m);
x=round(x);
y1++;
putpixel(x,y1,10);
}
}
}
int main()
{
float r, xc, yc;
cout << "enter center point: ";
cin >> xc >> yc;
cout << "enter radius";
cin >> r;
int j=0;
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
while(xc<=500)
{
cleardevice();
float p=1-r;
float x=0,y=r;
putpixel(xc, y+yc,10);
putpixel(xc+y,yc,10);

putpixel(xc,-y+yc,10);
putpixel(xc-y,yc,10);
float y1=0;
while(x<=y)
{
if(p<0)
{
y1=y;
putpixel(++x+xc, y1+yc, 10);
}
else
{
y1=y-1;
putpixel(++x+xc,y1+yc,10);
}
p=p+2*x+((y1*y1)-(y*y))-(y1-y)+1;
y=y1;
putpixel(y+xc,x+yc,10);
putpixel(xc-y,x+yc,10);
putpixel(x+xc,yc-y,10);
putpixel(xc-y,yc-x,10);
putpixel(xc-x,yc-y,10);
putpixel(xc+y,yc-x,10);
putpixel(xc-x,yc+y,10);
}
if(j%2==0)
{
dda(xc-r,yc,xc+r,yc);
dda(xc,yc-r,xc,yc+r);
dda(xc-x,yc-y,xc+x,yc+y);
dda((xc-x),(yc+y),(xc+x),(yc-y));
delay(500);
}
else
{
dda((xc+xc-r)/2,(yc+r+yc+y)/2,(xc+xc+x)/2,(yc-r+yc-y)/2);
dda((xc-r+xc-x)/2,(yc+yc+y)/2,(xc+r+xc+x)/2,(yc+yc-y)/2);
dda((xc-r+xc-x)/2,(yc+yc-y)/2,(xc+r+xc+x)/2,(yc+yc+y)/2);
dda((xc-x+xc)/2,(yc-y+yc-r)/2,(xc+xc+x)/2,(yc+r+yc+y)/2);
delay(500);
}
j++;
xc=xc+5;

}
getch();
closegraph();
return 0;
}
double round(double x)
{
return floor(x+0.5);
}
Output:
PRACTICAL- 6
Write a program to draw an ellipse using the Midpoint ellipse generation
algorithm for both the regions
Program:
#include <iostream>
#include <graphics.h>
using namespace std;

// Function to draw ellipse using Midpoint Ellipse Algorithm


void drawEllipse(int xc, int yc, int rx, int ry) {
int gm, gd = DETECT;
initgraph(&gd, &gm, "");

float dx, dy, d1, d2, x, y,p;


x = 0;
y = ry;

// Initial decision parameter of region 1


p = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;

// For region 1
while (dx < dy) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);

// Checking and updating value of decision parameter based on algorithm


if (p < 0) {
x++;
dx = dx + (2 * ry * ry);
p = p + dx + (ry * ry);
} else {
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
p = p + dx - dy + (ry * ry);
}
}

// Decision parameter of region 2


d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry);

// Plotting points of region 2


while (y >= 0) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);

// Checking and updating value of decision parameter based on algorithm


if (d2 > 0) {
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
} else {
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}

delay(5000);
getch();
closegraph();
}

int main() {
int xc, yc, rx, ry;
cout << "Enter center coordinates (xc, yc): ";
cin >> xc >> yc;
cout << "Enter radius along x-axis (rx): ";
cin >> rx;
cout << "Enter radius along y-axis (ry): ";
cin >> ry;

drawEllipse(xc, yc, rx, ry);


return 0;
}
Output:

You might also like