0% found this document useful (0 votes)
3 views31 pages

Practical No 4

Uploaded by

Suraj Mhaske
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)
3 views31 pages

Practical No 4

Uploaded by

Suraj Mhaske
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/ 31

Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

Practical No 4
Flood.cpp
#include <math.h>
#include <GL/glut.h>
#include<iostream>
using namespace std;
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 640, 0, 480);
}

void floodFill(int x, int y, float* newColor) {


float color[3];
glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,color);
if(color[0] != newColor[0] || color[1] != newColor[1] ||
color[2]!= newColor[2])
{
glColor3f(newColor[0],newColor[1],newColor[2]);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
floodFill(x+1, y,newColor);
floodFill(x, y+1, newColor);
floodFill(x-1, y, newColor);
floodFill(x, y-1, newColor);
}
return;
}

void onMouseClick(int button, int state, int x, int y)


{
float newColor[] = {0, 1, 0};
cout<<"x is :"<<x<<" y is : "<<y<<endl;
floodFill(320,230,newColor);
}

void draw_circle() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_LINE_LOOP);
glVertex2i(300,100);
glVertex2i(300,300);
glVertex2i(450,100);
glEnd();
glFlush();
}

void display(void) {
draw_circle();
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200,200);
glutCreateWindow("Open GL");
init();
glutDisplayFunc(display);
glutMouseFunc(onMouseClick);
glutMainLoop();
return 0;
}

Output :
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

BoundrayFill.cpp
#include <iostream>
#include <math.h>
#include <time.h>
#include <GL/glut.h>

using namespace std;

void init(){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,640,0,480);
}

void bound_it(int x, int y, float* fillColor, float* bc){


float color[3];
glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,color);
if((color[0]!=bc[0] || color[1]!=bc[1] || color[2]!=bc[2])&&(
color[0]!=fillColor[0] || color[1]!=fillColor[1] ||
color[2]!=fillColor[2])){
glColor3f(fillColor[0],fillColor[1],fillColor[2]);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
bound_it(x+2,y,fillColor,bc);
bound_it(x-2,y,fillColor,bc);
bound_it(x,y+2,fillColor,bc);
bound_it(x,y-2,fillColor,bc);
}
}

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


float bCol[] = {0,0,1};
float color[] = {0,0,1};
bound_it(x,y,color,bCol);
}

void world(){
glLineWidth(3);
glPointSize(2);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1);
glBegin(GL_LINE_LOOP);
glVertex2i(150,100);
glVertex2i(300,300);
glVertex2i(450,100);
glEnd();
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

glutInitWindowPosition(200,200);
glutCreateWindow("Many Amaze Very GL WOW");
init();
glutDisplayFunc(world);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

Output :
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D


Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

Practical No 5
Polygoan.cpp
#include <iostream>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
using namespace std;
int wxmin = 200, wxmax = 500, wymax = 350, wymin = 100;
int points[10][2];
int edge;
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 640, 0, 480);
glClear(GL_COLOR_BUFFER_BIT);
}
void Draw()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.2, 0.2, 1);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++)
{
glVertex2i(points[i][0], points[i][1]);
}
glEnd();
glFlush();
glColor3f(0, 1, 0);
glBegin(GL_LINE_LOOP);
glVertex2i(200, 100);
glVertex2i(500, 100);
glVertex2i(500, 350);
glVertex2i(200, 350);
glEnd();
glFlush();}
int BottomCliping(int e)
{
float m = 0;
int x = 0, k = 0;
int t[10][2];
for (int i = 0; i < e; i++)
{
if (points[i][1] < wymin)
{
if (points[i + 1][1] < wymin)
{
}
else if (points[i + 1][1] > wymin)
{
float x1, x2;
float y1, y2;
x1 = points[i][0];
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

y1 = points[i][1];
x2 = points[i + 1][0];
y2 = points[i + 1][1];
x = ((1 / ((y2 - y1) / (x2 - x1))) * (wymin - y1)) + x1;
t[k][0] = x;
t[k][1] = wymin;
k++;
}
}
else if (points[i][1] > wymin)
{
if (points[i + 1][1] > wymin)
{
t[k][0] = points[i][0];
t[k][1] = points[i][1];
k++;
}
else if (points[i + 1][1] < wymin)
{float x1, x2;
float y1, y2;
x1 = points[i][0];
y1 = points[i][1];
x2 = points[i + 1][0];
y2 = points[i + 1][1];
x = ((1 / ((y2 - y1) / (x2 - x1))) * (wymin - y1)) + x1;
t[k][0] = x1;
t[k][1] = y1;
k++;
t[k][0] = x;
t[k][1] = wymin;
k++;
}
}
}
cout << "k = " << k;
for (int i = 0; i < 10; i++)
{
points[i][0] = 0;
points[i][1] = 0;
}
for (int i = 0; i < k; i++)
{
cout << "\n"
<< t[i][0] << " " << t[i][1];
points[i][0] = t[i][0];
points[i][1] = t[i][1];
}
points[k][0] = points[0][0];
points[k][1] = points[0][1];
return k;
}
int TopCliping(int e)
{float m = 0;
int x = 0, k = 0;
int t[10][2];
for (int i = 0; i < e; i++)
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

{
if (points[i][1] > wymax)
{
if (points[i + 1][1] > wymax)
{
}
else if (points[i + 1][1] < wymax)
{
float x1, x2;
float y1, y2;
x1 = points[i][0];
y1 = points[i][1];
x2 = points[i + 1][0];
y2 = points[i + 1][1];
x = ((1 / ((y2 - y1) / (x2 - x1))) * (wymax - y1)) + x1;
t[k][0] = x;
t[k][1] = wymax;
k++;
}
}
else if (points[i][1] < wymax)
{
if (points[i + 1][1] < wymax)
{
t[k][0] = points[i][0];
t[k][1] = points[i][1];
k++;
}
else if (points[i + 1][1] > wymax)
{
float x1, x2;
float y1, y2;x1 = points[i][0];
y1 = points[i][1];
x2 = points[i + 1][0];
y2 = points[i + 1][1];
x = ((1 / ((y2 - y1) / (x2 - x1))) * (wymax - y1)) + x1;
t[k][0] = x1;
t[k][1] = y1;
k++;
t[k][0] = x;
t[k][1] = wymax;
k++;
}
}
}
cout << "k = " << k;
for (int i = 0; i < 10; i++)
{
points[i][0] = 0;
points[i][1] = 0;
}
for (int i = 0; i < k; i++)
{
cout << "\n"
<< t[i][0] << " " << t[i][1];
points[i][0] = t[i][0];
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

points[i][1] = t[i][1];
}
points[k][0] = points[0][0];
points[k][1] = points[0][1];
return k;
}
int leftCliping(int e)
{
float m = 0;
int y = 0, k = 0;
int t[10][2];for (int i = 0; i < e; i++)
{
if (points[i][0] < wxmin)
{
if (points[i + 1][0] < wxmin)
{
cout << "\n Test 1";
}
else if (points[i + 1][0] > wxmin)
{
cout << "\n Test 2";
float x1, x2;
float y1, y2;
x1 = points[i][0];
y1 = points[i][1];
x2 = points[i + 1][0];
y2 = points[i + 1][1];
y = (((y2 - y1) / (x2 - x1)) * (wxmin - x1)) + y1;
t[k][0] = wxmin;
t[k][1] = y;
k++;
}
}
else if (points[i][0] > wxmin)
{
if (points[i + 1][0] > wxmin)
{
t[k][0] = points[i][0];
t[k][1] = points[i][1];
k++;
}
else if (points[i + 1][0] < wxmin)
{
float x1, x2;
float y1, y2;
x1 = points[i][0];
y1 = points[i][1];x2 = points[i + 1][0];
y2 = points[i + 1][1];
y = ((y2 - y1) / (x2 - x1) * (wxmin - x1)) + y1;
t[k][0] = x1;
t[k][1] = y1;
k++;
t[k][0] = wxmin;
t[k][1] = y;
k++;
}
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

}
}
cout << "k = " << k;
for (int i = 0; i < 10; i++)
{
points[i][0] = 0;
points[i][1] = 0;
}
for (int i = 0; i < k; i++)
{
cout << "\n"
<< t[i][0] << " " << t[i][1];
points[i][0] = t[i][0];
points[i][1] = t[i][1];
}
points[k][0] = points[0][0];
points[k][1] = points[0][1];
return k;
}
int RightCliping(int e)
{
float m = 0;
int y = 0, k = 0;
int t[10][2];
for (int i = 0; i < e; i++)
{
if (points[i][0] > wxmax){
if (points[i + 1][0] > wxmax)
{
}
else if (points[i + 1][0] < wxmax)
{
float x1, x2;
float y1, y2;
x1 = points[i][0];
y1 = points[i][1];
x2 = points[i + 1][0];
y2 = points[i + 1][1];
y = (((y2 - y1) / (x2 - x1)) * (wxmax - x1)) + y1;
t[k][0] = wxmax;
t[k][1] = y;
k++;
}
}
else if (points[i][0] < wxmax)
{
if (points[i + 1][0] < wxmax)
{
t[k][0] = points[i][0];
t[k][1] = points[i][1];
k++;
}
else if (points[i + 1][0] > wxmax)
{
float x1, x2;
float y1, y2;
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

x1 = points[i][0];
y1 = points[i][1];
x2 = points[i + 1][0];
y2 = points[i + 1][1];
y = ((y2 - y1) / (x2 - x1) * (wxmax - x1)) + y1;
t[k][0] = x1;t[k][1] = y1;
k++;
t[k][0] = wxmax;
t[k][1] = y;
k++;
}
}
}
cout << "k = " << k;
for (int i = 0; i < 10; i++)
{
points[i][0] = 0;
points[i][1] = 0;
}
for (int i = 0; i < k; i++)
{
cout << "\n"
<< t[i][0] << " " << t[i][1];
points[i][0] = t[i][0];
points[i][1] = t[i][1];
}
points[k][0] = points[0][0];
points[k][1] = points[0][1];
return k;
}
void P_C(){
Draw();
}
void goMenu(int value){
switch (value){
case 1:
edge = leftCliping(edge);
Draw();
break;
case 2:
edge = RightCliping(edge);
Draw();break;
case 3:
edge = TopCliping(edge);
Draw();
break;
case 4:
edge = BottomCliping(edge);
Draw();
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv){
cout << "\n Enter No of edges of polygon ";
cin >> edge;
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

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


cout << "\n Enter point " << i << " x space y ";
cin >> points[i][0] >> points[i][1];
}
points[edge][0] = points[0][0];
points[edge][1] = points[0][1];
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("Polygon Clipping");
init();
glutCreateMenu(goMenu);
glutAddMenuEntry("Left", 1);
glutAddMenuEntry("Right", 2);
glutAddMenuEntry("Top", 3);
glutAddMenuEntry("Bottom", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(P_C);
glutMainLoop();
return 0;
}

Output :
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

Practical No 6
Transfortion.cpp
#include <iostream>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
#include <vector>
using namespace std;
int edge;
vector<int> xpoint;
vector<int> ypoint;
int ch;
double round(double d){
return floor(d + 0.5);
}
void init(){
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 640, 0, 480);
glClear(GL_COLOR_BUFFER_BIT);
}
void translation(){
int tx, ty;
cout << "\t Enter Tx, Ty \n";
cin >> tx >> ty;
// Translate the point
for (int i = 0; i < edge; i++){
xpoint[i] = xpoint[i] + tx;
ypoint[i] = ypoint[i] + ty;
}
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i], ypoint[i]);
}
glEnd();
glFlush();
}void rotaion(){
int cx, cy;
cout << "\n Enter Ar point x , y ";
cin >> cx >> cy;
cx = cx + 320;
cy = cy + 240;
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(cx, cy);
glEnd();
glFlush();
double the;
cout << "\n Enter thetha ";
cin >> the;
the = the * 3.14 / 180;
glColor3f(0, 0, 1.0);
glBegin(GL_POLYGON);
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

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


glVertex2i(round(((xpoint[i] - cx) * cos(the) - ((ypoint[i] - cy) *
sin(the))) +
cx), round(((xpoint[i] - cx) * sin(the) + ((ypoint[i] - cy) *
cos(the))) + cy));
}
glEnd();
glFlush();
}
void scale(){
glColor3f(1.0, 0, 0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i] - 320, ypoint[i] - 240);
}
glEnd();
glFlush();
cout << "\n\tIn Scaling whole screen is 1st Qudrant \n";
int sx, sy;
cout << "\t Enter sx, sy \n";
cin >> sx >> sy;// scale the point
for (int i = 0; i < edge; i++){
xpoint[i] = (xpoint[i] - 320) * sx;
ypoint[i] = (ypoint[i] - 240) * sy;
}
glColor3f(0, 0, 1.0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i], ypoint[i]);
}
glEnd();
glFlush();
}
void reflection(){
char reflection;
cout << "Enter Reflection Axis \n";
cin >> reflection;
if (reflection == 'x' || reflection == 'X'){
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i], (ypoint[i] * -1) + 480);
}
glEnd();
glFlush();
}
else if (reflection == 'y' || reflection == 'Y'){
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i((xpoint[i] * -1) + 640, (ypoint[i]));
}
glEnd();
glFlush();
}
}
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

void Draw(){if (ch == 2 || ch == 3 || ch == 4){


glColor3f(1.0, 0, 0);
glBegin(GL_LINES);
glVertex2i(0, 240);
glVertex2i(640, 240);
glEnd();
glColor3f(1.0, 0, 0);
glBegin(GL_LINES);
glVertex2i(320, 0);
glVertex2i(320, 480);
glEnd();
glFlush();
glColor3f(1.0, 0, 0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i], ypoint[i]);
}
glEnd();
glFlush();
}
if (ch == 1){
scale();
}
else if (ch == 2){
rotaion();
}
else if (ch == 3){
reflection();
}
else if (ch == 4){
translation();
}
}
int main(int argc, char **argv){
cout << "\n \t Enter 1) Scaling ";
cout << "\n \t Enter 2) Rotation about arbitrary point";cout << "\n \t
Enter 3) Reflection";
cout << "\n \t Enter 4) Translation \n \t";
cin >> ch;
if (ch == 1 || ch == 2 || ch == 3 || ch == 4){
cout << "Enter No of edges \n";
cin >> edge;
int xpointnew, ypointnew;
cout << " Enter" << edge << " point of polygon \n";
for (int i = 0; i < edge; i++){
cout << "Enter " << i << " Point ";
cin >> xpointnew >> ypointnew;
xpoint.push_back(xpointnew + 320);
ypoint.push_back(ypointnew + 240);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("2D");
init();
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
else
{
cout << "\n \t Check Input run again";
return 0;
}
}

Output :
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D


Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D


Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

Practical No 7
Koch_curve.cpp
#include <iostream>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
using namespace std;
double x,y,len,angle;
int it;
void init(){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,640,0,480);
glClear(GL_COLOR_BUFFER_BIT);
}
void line1(int x1, int y11, int x2,int y2){
glColor3f(0,1,0);
glBegin(GL_LINES);
glVertex2i(x1,y11);
glVertex2i(x2,y2);
glEnd();
glFlush();
}
void k_curve(double x, double y, double len, double angle, int it){
if(it>0){
len /=3;
k_curve(x,y,len,angle,(it-1));
x += (len * cosl(angle * (M_PI)/180));
y += (len * sinl(angle * (M_PI)/180));
k_curve(x,y, len, angle+60,(it-1));
x += (len * cosl((angle + 60) * (M_PI)/180));
y += (len * sinl((angle + 60) * (M_PI)/180));
k_curve(x,y, len, angle-60,(it-1));
x += (len * cosl((angle - 60) * (M_PI)/180));
y += (len * sinl((angle - 60) * (M_PI)/180));
k_curve(x,y,len,angle,(it-1));
}
else{
line1(x,y,(int)(x + len * cosl(angle * (M_PI)/180) + 0.5),(int)(y + len
*
sinl(angle * (M_PI)/180) + 0.5));
}
}
void Algorithm(){
k_curve(x,y,len,angle,it);
}
int main(int argc, char** argv){
cout<<"\n Enter Starting Point x space y ";
cin>>x>>y;
cout <<"\n Lenght of line and space angle of line";
cin>>len>>angle;
cout<<"\n No. of ittration ";
cin>>it;
glutInit(&argc, argv);
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(200,200);
glutCreateWindow("Koch");
init();
glutDisplayFunc(Algorithm);
glutMainLoop();
return 0;
}

Output :
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

lineclippingSuder_land.cpp
#include<iostream>
#include<GL/glut.h>
#include<math.h>
using namespace std;

int xl=50,xh=200,yl=50,yh=200;
int flag=0;
float u1,v1,u2,v2;

//defining the structure code to get - opcodes.


struct code
{
int t,b,r,l;
};

void init()
{
//set the background color.
glClearColor(1,1,1,0);

//activate the color_buffer_bit and assign the background color


mentioned in glClearColor.
glClear(GL_COLOR_BUFFER_BIT);

//set the pixel color as black.


glColor3f(0,0,0);
}

//get the opcodes (tbrl).


code get_code(int u,int v)
{
code c={0,0,0,0};

if(u<xl)
c.l=1;

if(u>xh)
c.r=1;

if(v<yl)
c.b=1;

if(v>yh)
c.t=1;

return c;
}
/*
// #BRESENHEM LINE DRAWING ALOGORITHM
void line(int u1,int v1,int u2,int v2)
{
int dx,dy,p,xi=1,yi=1;
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

dx=u2-u1;
dy=v2-v1;

if(dx<0)
{
dx=-dx;
xi=-1;
}

if(dy<0)
{
dy=-dy;
yi=-1;
}

glBegin(GL_POINTS);
glVertex2i(u1,v1);
if(dx>dy)
{
p=(2*dy)-dx;
while(u1!=u2)
{
if(p<=0)
{
p+=2*dy;
}
else
{
p+=2*(dy-dx);
v1+=yi;
}

u1+=xi;
glVertex2i(u1,v1);
}
}

else
{
p=(2*dx)-dy;
while(v1!=v2)
{
if(p<=0)
{
p+=2*dx;
}
else
{
p+=2*(dx-dy);
u1+=xi;
}

v1+=yi;
glVertex2i(u1,v1);
}
}
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

glEnd();
glFlush();
}
*/
// #DDA-LINE DRAWING ALGORITHM
void line(float u1,float v1,float u2,float v2)
{

float dx,dy,x=u1,y=v1,xi,yi;
int steps,i;

dx=u2-u1;
dy=v2-v1;

steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

xi=dx/(float)steps;
yi=dy/(float)steps;

glBegin(GL_POINTS);
glVertex2f(x,y);

for(i=0;i<steps;i++)
{
x+=xi;
y+=yi;

glVertex2f(x,y);
}

glEnd();
glFlush();

//draw the window


void draw_window()
{
line(50,50,200,50);
line(50,50,50,200);
line(200,50,200,200);
line(50,200,200,200);
}
//mouse function to draw the line.
void mymouse(int button,int state,int x,int y)
{
glColor3f(0,0,0);
if(state==GLUT_DOWN && flag==0)
{
u1=x;
v1=480-y;
flag=1;

}
else if(state==GLUT_DOWN && flag==1)
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

{
u2=x;
v2=480-y;
flag=2;
line(u1,v1,u2,v2);
}
}
//cohen-sutherland algorithm to clip the line
void cohen()
{
code c1,c2,c;
float m;
int xi,yi,flag;

//get the slope of line


m=(v2-v1)/(u2-u1);

//get the opcodes of both the co-ordinates in c1 and c2.


c1=get_code(u1,v1);
c2=get_code(u2,v2);

while(1)
{
//if line inside the window , draw the line as it is.
if( c1.t==0 && c2.t==0 && c1.b==0 && c2.b==0 && c1.r==0
&& c2.r==0 && c1.l==0 && c2.l==0 )
break;

//if the ANDING of opcodes is non-zero then don't draw


the line.
else if( ( (c1.t && c2.t) || (c1.b && c2.b) || (c1.r &&
c2.r) || (c1.l && c2.l) ) !=0)
{
u1=v1=u2=v2=0;
break;
}

//if line partially inside the window changing the co-


ordinates as per following conditions.
else
{
if( c1.l==1 || c2.l==1)
{
xi=xl;
yi=v1+m*(xl-u1);

if(c1.l==1)
flag=0;

else
flag=1;

else if( c1.r==1 || c2.r==1 )


{
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

xi=xh;
yi=v1+m*(xh-u1);

if(c1.r==1)
flag=0;

else
flag=1;
}

else if( c1.b==1 || c2.b==1 )


{
xi=u1+((1/m)*(yl-v1));
yi=yl;

if(c1.b==1)
flag=0;

else
flag=1;
}
else if( c1.t==1 || c2.t==1 )
{
xi=u1+((1/m)*(yh-v1));
yi=yh;

if(c1.t==1)
flag=0;

else
flag=1;
}

//get the code of xi and yi.


c=get_code(xi,yi);

if(flag==0)
{
u1=xi;
v1=yi;
c1=c;
}
else if(flag==1)
{
u2=xi;
v2=yi;
c2=c;
}

}//end_else

}//end_while

//draw_the window and clipped line.


draw_window();
line(u1,v1,u2,v2);
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

void mykey(char unsigned key,int x,int y)


{
//press 'c' to clip the line.
if(key=='c')
{
init();
cohen();
}

//press 'r' to reset the window.


if(key=='r')
{
init();
draw_window();
flag=0;
}
}

int main(int argc,char **argv)


{
//initializing the glut-library.
glutInit(&argc, argv);

//set the display mode as GLUT_SINGLE for single buffer window.


glutInitDisplayMode(GLUT_SINGLE);

//set the size of window.


glutInitWindowSize(640,480);

//set the window position.


glutInitWindowPosition(0,0);

//creating the window and assigning the name.


glutCreateWindow("Line_Clipping");

//declaring the co-ordinates of ortho-2d function i.e getting the


orthographic projection.
gluOrtho2D(0,640,0,480);

//initialize the window created with background color,set the pixel


color.
init();
glFlush();

//draw the window.


draw_window();

//get the line using mouse.


glutMouseFunc(mymouse);

//clip the line by pressing 'c' and also the reset the window by
pressing 'r'.
glutKeyboardFunc(mykey);
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

//keeping the window open.


glutMainLoop();

return 0;

}//end_main

Output :
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

Prctical No 8
Animation.cpp

#include <GL/glut.h>
#include <cmath>
float ballY = 0.8f; // Ball Y position
float speed = 0.0f; // Ball speed
float gravity = -0.0015f;
bool goingDown = true;

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

// Set color
glColor3f(0.2f, 0.6f, 1.0f);

// Apply squash and stretch


float scaleX = 1.0f;
float scaleY = 1.0f;

if (ballY <= -0.8f) {


scaleY = 0.5f;
scaleX = 1.5f;
} else {
float stretchFactor = 1.0f + speed * 15.0f;
scaleY = std::min(1.2f, stretchFactor);
scaleX = 1.0f / scaleY;
}

glPushMatrix();
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

glTranslatef(0.0f, ballY, 0.0f);


glScalef(scaleX, scaleY, 1.0f);

// Draw the ball


int numSegments = 100;
float radius = 0.1f;
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= numSegments; i++) {
float angle = 2.0f * 3.14159f * i / numSegments;
float x = radius * cos(angle);
float y = radius * sin(angle);
glVertex2f(x, y);
}
glEnd();

glPopMatrix();

glutSwapBuffers();
}

void update(int value) {


speed += gravity;
ballY += speed;

if (ballY <= -0.8f) {


ballY = -0.8f;
speed = -speed * 0.8f; // bounce with damping
}

glutPostRedisplay();
glutTimerFunc(16, update, 0);
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // white background
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutCreateWindow("Squash and Stretch Animation");

init();
glutDisplayFunc(display);
glutTimerFunc(0, update, 0);
glutMainLoop();

return 0;
}

Output :
Name: Mhaske Suraj Machhindra Class : SE-IT

RollNo : 207B073 Batch : D

You might also like