Professional Documents
Culture Documents
Ludhiana, Punjab
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};
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();
}
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;
// 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);
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;