/* Write C++ program to draw man walking in the rain with an umbrella. */
#include <iostream>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
using namespace std;
int main()
{
int gd=DETECT,gm;
int rhx,rhy,j,i;
clrscr();
initgraph(&gd,&gm,NULL);
for(i=0;i<500;i+=5)
{
line(20,380,580,380); //platform
if(i%2==0)
{
line(25+i,380,35+i,340); //leftleg
line(45+i,380,35+i,340);//right leg
line(35+i,310,25+i,330);//left hand
delay(20);
}
else
{
line(35+i,380,35+i,340);
line(35+i,310,40+i,330);
delay(20);
}
line(35+i,340,35+i,310); //body
circle(35+i,300,10); //head
line(35+i,310,50+i,330); // hand
line(50+i,330,50+i,280); //umbrella stick
line(15+i,280,85+i,280); //umbrella right
arc(50+i,280,0,180,35); //umbrella body
arc(55+i,330,180,360,5);//umbrella handle
rhx=getmaxx();
rhy=getmaxy();
for(j=0;j<100;j++)
{
outtextxy(random(rhx),random(rhy-50),"|");
setcolor(WHITE);
}
delay(150);
cleardevice();
return 0;
}
}
Second Year Engineering - Computer Graphics and OOP Lab
Monday, January 11, 2021
Write C++ program to draw man walking in the rain with an umbrella.
Code For Sunrise and Sunset: Using Graphics Library
//Code For Sunrise and Sunset:
#include<iostream>
#include<stdlib.h>
#ifdef __APPLE__
#include<openGL/openGL.h>
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
using namespace std;
float ballX = -0.8f;
float ballY = -0.3f;
float ballZ = -1.2f;
float colR=3.0;
float colG=1.5;
float colB=1.0;
float bgColR=0.0;
float bgColG=0.0;
float bgColB=0.0;
static int flag=1;
void drawBall(void) {
glColor3f(colR,colG,colB); //set ball colour
glTranslatef(ballX,ballY,ballZ); //moving it toward the screen a
//bit on creation
glutSolidSphere (0.05, 30, 30); //create ball.
}
void drawAv(void) {
glBegin(GL_POLYGON);
glColor3f(1.0,1.0,1.0);
glVertex3f(-0.9,-0.7,-1.0);
glVertex3f(-0.5,-0.1,-1.0);
glVertex3f(-0.2,-1.0,-1.0);
glVertex3f(0.5,0.0,-1.0);
glVertex3f(0.6,-0.2,-1.0);
glVertex3f(0.9,-0.7,-1.0);
glEnd();
}
void drawClouds(){}
void keyPress(int key, int x, int y)
{
if(key==GLUT_KEY_RIGHT)
ballX -= 0.05f;
if(key==GLUT_KEY_LEFT)
ballX += 0.05f;
glutPostRedisplay();
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_NORMALIZE); //Automatically normalize normals
//glShadeModel(GL_SMOOTH); //Enable smooth shading
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
200.0); //The far z clipping coordinate
}
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor(bgColR,bgColG,bgColB,0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Add ambient light
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
//drawing the SUN
glPushMatrix();
drawBall();
glPopMatrix();
//drawing the Mount Avarest
glPushMatrix();
drawAv();
glPopMatrix();
//drawing the Clouds
glPushMatrix();
drawClouds();
glPopMatrix();
glutSwapBuffers();
}
//float _angle = 30.0f;
void update(int value) {
if(ballX>0.9f)
{
ballX = -0.8f;
ballY = -0.3f;
flag=1;
colR=2.0;
colG=1.50;
colB=1.0;
bgColB=0.0;
}
if(flag)
{
ballX += 0.001f;
ballY +=0.0007f;
colR-=0.001;
//colG+=0.002;
colB+=0.005;
bgColB+=0.001;
if(ballX>0.01)
{
flag=0;
}
}
if (!flag)
{
ballX += 0.001f;
ballY -=0.0007f;
colR+=0.001;
colB-=0.01;
bgColB-=0.001;
if(ballX<-0.3)
{
flag=1;
}
}
glutPostRedisplay(); //Tell GLUT that the display has changed
//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(25, update, 0);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("Sun");
initRendering();
glutDisplayFunc(drawScene);
glutFullScreen();
glutSpecialFunc(keyPress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return(0);
}
Cpp program to generate fractal pattern using Koch Curve
//Cpp program to generate fractal pattern using Koch Curve
#include<graphics.h>
#include<iostream>
#include<math.h>
using namespace std;
void koch(int x1, int y1, int x2, int y2, int it)
{
float angle = 60*M_PI/180;
int x3 = (2*x1+x2)/3;
int y3 = (2*y1+y2)/3;
int x4 = (x1+2*x2)/3;
int y4 = (y1+2*y2)/3;
int x = x3 + (x4-x3)*cos(angle)+(y4-y3)*sin(angle);
int y = y3 - (x4-x3)*sin(angle)+(y4-y3)*cos(angle);
if(it > 0)
{
koch(x1, y1, x3, y3, it-1);
koch(x3, y3, x, y, it-1);
koch(x, y, x4, y4, it-1);
koch(x4, y4, x2, y2, it-1);
}
else
{
line(x1, y1, x3, y3);
line(x3, y3, x, y);
line(x, y, x4, y4);
line(x4, y4, x2, y2);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,NULL);
int x1 = 100, y1 = 100, x2 = 400, y2 = 400;
koch(x1, y1, x2, y2, 4);
delay(5000);
closegraph();
return 0;
}
a) Write C++ program to draw 2-D object and perform following basic transformations, Scaling b) Translation c) Rotation
/* a) Write C++ program to draw 2-D object and perform following basic transformations, Scaling
b) Translation c) Rotation. */
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
void disp(int n,float c[][3])
{
float maxx,maxy;
int i;
maxx=getmaxx(); //to get maximum x-coordinate value
maxy=getmaxy(); //to get maximum y-coordinate value
maxx=maxx/2;
maxy=maxy/2;
i=0;
//draw polygon
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
//draw xy-axis
setcolor(GREEN);
line(0,maxy,(maxx*2),maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}
void mul(int n,float b[][3],float c[][3],float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=0; //initialize all co-ordinate as 0
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
a[i][j]=a[i][j]+(c[i][k]*b[k][j]);
}
}
}
}
void translation(int n,float c[][3],float tx,float ty)
{
float b[10][3],a[10][3];
int i=0,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
}
}
b[0][0]=1;
b[1][1]=1;
b[2][0]=tx;
b[2][1]=ty;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
getch();
}
void scaling(int n,float c[][3],float sx,float sy)
{
float b[10][3],a[10][3];
int i=0,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
}
}
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
getch();
}
void rotation(int n,float c[][3],float ra)
{
int i=0,j;
float b[10][3],xp,yp,a[10][3];
xp=c[0][0];
yp=c[0][1];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
}
}
b[0][0]=b[1][1]=cos(ra*3.14/180);
b[0][1]=sin(ra*3.14/180);
b[1][0]=-sin(ra*3.14/180);
b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp;
b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
getch();
}
int main()
{
int i,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
cout<<"\n Enter the number of vertices:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n Enter the co-ordinates of the vertex"<<i+1<<":";
cin>>c[i][0]>>c[i][1];
c[i][2]=1;
}
do
{
//cleardevice();
cout<<"\n\t******Menu for transformation*******";
cout<<"\n\t 1)Translation";
cout<<"\n\t 2)Scaling";
cout<<"\n\t 3)Rotation";
cout<<"\n\t 4)Exit";
cout<<"\n\t Enter your choice:";
cin>>cho;
switch(cho)
{
case 1:
cout<<"Enter translation factor for x and y axis:";
cin>>tx>>ty;
initgraph(&gd,&gm,NULL);
//clrscr();
//ssscleardevice();
setcolor(BLUE);
disp(n,c);
translation(n,c,tx,ty);
getch();
break;
case 2:
cout<<"Enter scaling factor for x and y axis:";
cin>>sx>>sy;
initgraph(&gd,&gm,NULL);
//clrscr();
//cleardevice();
setcolor(BLUE);
disp(n,c);
scaling(n,c,sx,sy);
getch();
break;
case 3:
cout<<"Enter rotation factor :";
cin>>ra;
initgraph(&gd,&gm,NULL);
//clrscr();
//cleardevice();
setcolor(BLUE);
disp(n,c);
rotation(n,c,ra);
getch();
break;
case 4:
exit(0);
break;
default:
cout<<"\n Invalid choice";
break;
}
}while(cho!=4);
getch();
closegraph();
return 0;
}
Write C++ program to draw the pattern. Use DDA line and Bresenham‘s circle drawing algorithm.
// Write C++ program to draw the pattern. Use DDA line and Bresenham‘s circle drawing algorithm.
# include <graphics.h> //graphics.h library is used to include graphical operations in a program.
# include <math.h>
# include <iostream>
using namespace std ;
void DDALine(int x1,int y1,int x2,int y2,int Color); //declare function
int main()
{
int x1,y1,x2,y2,r,r1,Color;
int gd,gm; gd=DETECT; //Initialize the variables for the graphics driver and graphics mode
// gm is Graphics mode which is a computer display mode that generates image using pixels.
// DETECT is a macro defined in "graphics.h" header file
initgraph(&gd,&gm,NULL); // initgraph initializes the graphics system by loading a graphics driver from disk
cleardevice(); // The header file graphics.h contains cleardevice() function which clears the screen in graphics mode and sets the current position to (0,0)
//call function
DDALine(100,113,50,200,4); //x1,y1,x2,y2,value of color
DDALine(50,200,150,200,4);
DDALine(150,200,100,113,4);
r=50/sqrt(3); //formula to find out radius of small circle
x1=(100+50+150)/3;
y1=(113+200+200)/3;
circle(x1,y1,r); //draw small circle
r1=100/sqrt(3); //formula to find out radius of outer circle
circle(x1,y1,r1); //draw outer circle
delay(10000); //delay() function is used to hold the program's execution for given number of milliseconds
return 0;
}
// define function
void DDALine(int x1,int y1,int x2,int y2,int Color)
{
float dX,dY,Steps;
float xInc,yInc,i,x,y;
dX = x2 - x1;
dY = y2 - y1;
if (abs(dX) > abs(dY))
{
Steps = abs(dX);
}
else
{
Steps = abs(dY);
}
xInc = dX/Steps;
yInc = dY/Steps;
x = x1;
y = y1;
for (i=1;i<=Steps; i++)
{
putpixel(x,y,Color);//The header file graphics. h contains putpixel() function which plots a pixel at location (x, y)of specified color
x = x+xInc;
y=y+yInc;
}
}
Program to implement Cohen-Sutherland Line Clipping Algorithm in C++
/* Program to implement Cohen-Sutherland Line Clipping Algorithm in C++ */
#include<iostream>
#include<graphics.h> //graphics.h library is used to include graphical operations in a program.
using namespace std;
static int LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,xmin,ymin,xmax,ymax;
// getcode fun to find out code of end points of line
int getcode(int x,int y) //x,y cordinates of first endpoint of line
{
int code = 0;
//Perform Bitwise OR to get outcode
if(y > ymax) code |=TOP;
if(y < ymin) code |=BOTTOM;
if(x < xmin) code |=LEFT;
if(x > xmax) code |=RIGHT;
return code;
}
int main()
{
int gd = DETECT,gm;//Initialize the variables for the graphics driver and graphics mode
// gm is Graphics mode which is a computer display mode that generates image using pixels.
// DETECT is a macro defined in "graphics.h" header file
cout<<"Enter the window's minimum and maximum values: ";
cin>>xmin>>ymin >>xmax>>ymax;
int x1,y1,x2,y2;
cout<<"Enter the endpoints of the line: ";
cin>>x1>>y1>>x2>>y2;
initgraph(&gd,&gm,NULL); //initgraph initializes the graphics system by loading a graphics driver from disk
rectangle(xmin,ymin,xmax,ymax); // draw rectanguler window
setcolor(BLUE);
line(x1,y1,x2,y2); //Draw line to be clipped
int outcode1=getcode(x1,y1), outcode2=getcode(x2,y2);
int accept = 0; //decides if line is to be drawn
while(1){
float m =(float)(y2-y1)/(x2-x1);
//Both points inside. Accept line
if(outcode1==0 && outcode2==0){ //The logical AND operator (&&) returns true if both operands are true and returns false otherwise.
accept = 1;
break;
}
//bitwise AND of both codes != 0.Line is outside. Reject line
else if((outcode1 & outcode2)!=0){
break;
}else{
int x,y,temp;
//Decide if point1 is inside, if not, calculate intersection
if(outcode1==0)
temp = outcode2;
else
temp = outcode1;
//Line clips top edge
if(temp & TOP){ //if bitwise of temp and top is true
x = x1+ (ymax-y1)/m; //using this formula perform intersection with top boundry
y = ymax;
}
else if(temp & BOTTOM){ //Line clips bottom edge
x = x1+ (ymin -y1)/m;
y = ymin ;
}else if(temp & LEFT){ //Line clips left edge
x = xmin;
y = y1+ m*(xmin-x1);
}else if(temp & RIGHT){ //Line clips right edge
x = xmax;
y = y1+ m*(xmax-x1);
}
//Check which point we had selected earlier as temp, and replace its co-ordinates
if(temp == outcode1){
x1 = x;
y1 = y;
outcode1 = getcode(x1,y1);
}else{
x2 = x;
y2 = y;
outcode2 = getcode(x2,y2);
}
}
}
cout<<"After clipping:";
if(accept)
cleardevice();
setcolor(WHITE);
rectangle(xmin,ymin,xmax,ymax);
setcolor(RED);
line(x1,y1,x2,y2);
getch();
closegraph();
return 0;
}
Write C++ program to draw a concave polygon and fill it with desired color using scan fill algorithm.
// Write C++ program to draw a concave polygon and fill it with desired color using scan fill algorithm.
#include<iostream>
#include<graphics.h> //graphics.h library is used to include graphical operations in a program.
#include<math.h>
using namespace std;
class scan
{
public:
int x[20],y[20],k;
float slope[20],x_int[20];
void polygon(int n);
};
void scan::polygon(int n)
{
int i;
float dx,dy;
x[n]=x[0];
y[n]=y[0];
for(int i=0;i<n;i++) //draw all lines (edges of polygon)
{
line(x[i],y[i],x[i+1],y[i+1]); // line cordinates x1,y1,x2,y2
}
for(i=0;i<n;i++) // finding slope of all lines
{
dy=y[i+1]-y[i]; // dy=y2-y1
dx=x[i+1]-x[i]; // dx=x2-x1
if(dy==0)
slope[i]=1;
else if(dx==0)
slope[i]=0;
else
slope[i]=dx/dy;
}
// finding intersection points
for(int p=0;p<480;p++) // consider 480 horizontal lines on screen
{
k=0;
for(i=0;i<n;i++)
{
if(( (y[i]<=p) && (y[i+1]>p)) || ((y[i]>p) && (y[i+1]<=p) ))
{
x_int[k]=x[i]+slope[i]*(p-y[i]); // find out intersection points using formula
k++;
}
}
for(int j=0;j<k-1;j++) // perform sorting of intersection points on x direction
{
for(int i=0;i<k-1;i++)
{
if(x_int[i]>x_int[i+1])
{
int temp = x_int[i];
x_int[i] = x_int[i+1];
x_int[i+1] = temp;
}
}
}
for(int i=0;i<k;i=i+2) //fill points of line that are interior to polygon
{
setcolor(YELLOW);
line(x_int[i], p ,x_int[i+1], p); // x1,y1,x2,y2
delay(10);
}
}
}
int main()
{
int n,i;
scan p;
cout<<"Enter edge : \t";
cin>>n;
cout<<"\n\nEnter Coordinates : \t";
for(i=0;i<n;i++)
{
cin>>p.x[i]>>p.y[i];
}
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,NULL);
p.polygon(n);
getch();
closegraph();
return 0;
}
/* Enter edge : 4
Enter Coordinates :
50 100
100 50
300 200
150 120 Enter edge : 4
Enter Coordinates : 200 300
300 400
200 400
300 300
Enter edge : 4
200 300
300 200
300 100
150 120
*/