29
Computer Graphics Assignment 1 Out Date : 27 Sept 2015 Submission Date : 10 Oct 2015 Submitted by Name : Mradul Verma Roll No: Thirteen(13) Ques 1 : Implement a DDA line algorithm for both slope |m| <= 1 and |m| > 1. Code :

MradulVerma Thirteen Assignment1 CSE3

Embed Size (px)

DESCRIPTION

Computer Graphics

Citation preview

Page 1: MradulVerma Thirteen Assignment1 CSE3

Computer Graphics Assignment 1

Out Date : 27 Sept 2015

Submission Date : 10 Oct 2015

Submitted by

Name : Mradul Verma

Roll No: Thirteen(13)

Ques 1 : Implement a DDA line algorithm for both slope |m| <= 1 and |m| > 1.

Code :

/* DDA line algorithm in OpenGL in windows for |m|<1

Page 2: MradulVerma Thirteen Assignment1 CSE3

@author : Mradul Verma

@date : 10 - Oct - 2015

@time : 7:00 PM

*/

#include<glut.h>

#include<stdio.h>

#include<math.h>

#define Round(a) ((int)(a+0.5))

void init(void)

{

glClearColor(0.0,0.0,0.0,0.0); //Background color of distplaying window

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,400.0,0.0,400.0); /* orthographic projection yo map content of 2d rectangular area of world coordinate to screen */

}

void drawline(void)

{

glClear(GL_COLOR_BUFFER_BIT);

int w = 400;

int h = 400;

int distx = 8;

int disty = 8;

int xx, yy;

Page 3: MradulVerma Thirteen Assignment1 CSE3

glColor3f(1.0, 0.0, 0.0); // color of drawing object

glRecti(w / 2, 0, w / 2 + distx, h); // draw vertical axis

glRecti(0, h/ 2, w, h/ 2 + disty); // draw horizontal axis

glColor3f(1.0, 1.0, 1.0);

for (xx = 0; xx <= w; xx = xx + dx)

{

for (yy = 0; yy <= h; yy = yy + dy)

{

glBegin(GL_LINES); // for drawing lines

glVertex2i(xx, 0);

glVertex2i(xx, h);

glVertex2i(0, yy);

glVertex2i(w, yy);

glEnd();

}

}

glColor3f(0.0, 1.0, 0.0);

float x1 = 0;

float y1 = 0;

float x2 = 18;

float y2 = 9;

float dx = x2 - x1, dy = y2 - y1, steps, k;

float m = dy / dx;

float x = x1, y = y1;

Page 4: MradulVerma Thirteen Assignment1 CSE3

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

glRecti(Round(x)*distx + w/ 2, Round(y)*disty + h/ 2, ((Round(x))*distx + distx) + w/ 2, (Round(y)*disty + disty) + h / 2);

printf("%f %f", x, y);

for (k = 0; k < steps;k++)

{

x = x++;

y = y+m;

glRecti(Round(x)*distx + w / 2, Round(y)*disty + h / 2, ((Round(x))*distx + distx) + w/ 2, (Round(y)*disty + disty) + h / 2);

printf("%f %f", x, y);

}

glFlush();

}

int main(int argc, char **argv)

{

glutInit(&argc, argv); // initialze GLUT

glutInitDistplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowPosition(50,100); // position of window

glutInitWindowSize(400,300); // size of the window

glutCreateWindow("DDA for |m| < 1"); // name of the window

init();

glutDistplayFunc(drawline);

glutMainLoop();

return 0;

}

/* DDA line algorithm in OpenGL in windows for |m|>1

Page 5: MradulVerma Thirteen Assignment1 CSE3

@author : Mradul Verma

@date : 10- Oct - 2015

@time : 9:20 PM

*/

#include<glut.h>

#include<stdio.h>

#include<math.h>

#define Round(a) ((int)(a+0.5))

void init(void)

{

glClearColor(0.0,0.0,0.0,0.0); //Background color of distplaying window

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,400.0,0.0,400.0); /* orthographic projection yo map content of 2d rectangular area of world coordinate to screen */

}

void drawline(void)

{

glClear(GL_COLOR_BUFFER_BIT);

int w = 400;

int h = 400;

int distx = 8;

int disty = 8;

int xx, yy;

Page 6: MradulVerma Thirteen Assignment1 CSE3

glColor3f(1.0, 0.0, 0.0); // color of drawing object

glRecti(w / 2, 0, w/ 2 + distx, h); // draw vertical axis

glRecti(0, h/ 2, w, h/ 2 + disty); // draw horizontal axis

glColor3f(1.0, 1.0, 1.0);

for (xx = 0; xx <= w; xx = xx + distx)

{

for (yy = 0; yy <= h; yy = yy + disty)

{

glBegin(GL_LINES); // for drawing lines

glVertex2i(xx, 0);

glVertex2i(xx, h);

glVertex2i(0, yy);

glVertex2i(w, yy);

glEnd();

}

}

glColor3f(0.0, 1.0, 0.0);

float x1 = 0;

float y1 = 0;

float x2 = 9;

float y2 = 18;

float dx = x2 - x1, dy = y2 - y1, steps, k;

float m = dy / dx;

float x = x1, y = y1;

Page 7: MradulVerma Thirteen Assignment1 CSE3

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

glRecti(Round(x)*distx + w/ 2, Round(y)*disty + h/ 2, ((Round(x))*distx + distx) + w / 2, (Round(y)*disty + disty) + h / 2);

printf("%f %f", x, y);

for (k = 0; k < steps;k++)

{

x = x+1/m;

y = y++;

glRecti(Round(x)*distx + w / 2, Round(y)*disty + h / 2, ((Round(x))*distx + distx) + w / 2, (Round(y)*disty + disty) + h / 2);

printf("%f %f", x, y);

}

glFlush();

}

int main(int argc, char **argv)

{

glutInit(&argc, argv); // initialze GLUT

glutInitDistplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowPosition(50,100); // position of window

glutInitWindowSize(400,300); // size of the window

glutCreateWindow("DDA for |m| > 1"); // name of the window

init();

glutDistplayFunc(drawline);

glutMainLoop();

return 0;

}

Output :

Page 8: MradulVerma Thirteen Assignment1 CSE3
Page 9: MradulVerma Thirteen Assignment1 CSE3

Ques 2 : Implement a polynomial and trigonometrical method for drawing circle (by using the concept of 8 way symmetry ).

Code :

/* Polynomial circle algorithm in OpenGl in windows

@author : Mradul Verma

@date : 10 - Oct - 2015

@time : 10:32 PM

*/

Page 10: MradulVerma Thirteen Assignment1 CSE3

#include<glut.h>

#include<stdio.h>

#include<math.h>

void init(void)

{

glClearColor(0.0, 0.0, 0.0, 0.0); //Background color of distplaying window

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, 400.0, 0.0, 400.0); /* orthographic projection yo map content of 2d rectangular area of world coordinate to screen */

}

void putpixel(int x, int y, int w = 400, int h = 400, int distx = 8, int disty = 8)

{

glRecti(x*distx + w / 2, y*disty + h / 2, (x*distx + distx) + w / 2, (y*disty + disty) + h / 2);

printf("%f %f\n", x, y);

glRecti(w / 2 - x*distx, y*disty + h / 2, (-(x*distx + distx)) + w / 2, (y*disty + disty) + h / 2);

printf("%f %f\n", x, y);

glRecti(w / 2 + x*distx, -y*disty + h / 2, ((x*distx + distx)) + w / 2, -(y*disty + disty) + h / 2);

printf("%f %f\n", x, y);

glRecti(w / 2 - x*distx, -y*disty + h / 2, (-(x*distx + distx)) + w / 2, -(y*disty + disty) + h / 2);

printf("%f %f\n", x, y);

glRecti(w / 2 + y*disty, x*distx + h / 2, ((y*disty + disty)) + w / 2, (x*disty + distx) + h / 2);

printf("%f %f\n", x, y);

glRecti(w / 2 - y*disty, x*distx + h / 2, (-(y*disty + disty)) + w / 2, (x*distx + distx) + h / 2);

printf("%f %f\n", x, y);

Page 11: MradulVerma Thirteen Assignment1 CSE3

glRecti(w / 2 + y*disty, -x*distx + h / 2, ((y*distx + disty)) + w / 2, -(x*distx + distx) + h / 2);

printf("%f %f\n", x, y);

glRecti(w / 2 - y*disty, -x*distx + h / 2, (-(y*distx + disty)) + w / 2, -(x*distx + distx) + h / 2);

printf("%f %f\n", x, y);

}

void drawline(void)

{

glClear(GL_COLOR_BUFFER_BIT);

int w = 400;

int h = 400;

int distx = 8;

int disty = 8;

int xx, yy;

glColor3f(1.0, 0.0, 0.0); // color of drawing object

glRecti(w / 2, 0, w / 2 + distx, h); // draw vertical axis

glRecti(0, h / 2, w, h / 2 + disty); // draw horizontal axis

glColor3f(1.0, 1.0, 1.0);

for (xx = 0; xx <= w; xx = xx + distx)

{

for (yy = 0; yy <= h; yy = yy + disty)

{

glBegin(GL_LINES); // for drawing lines

glVertex2i(xx, 0);

glVertex2i(xx, h);

glVertex2i(0, yy);

Page 12: MradulVerma Thirteen Assignment1 CSE3

glVertex2i(w, yy);

glEnd();

}

}

glColor3f(0.0, 1.0, 0.0);

float x = 0;

int r = 18;

float y = r;

putpixel(x, y);

while (x <= r / 1.3)

{

x = x + 1;

y = sqrt(r*r - x*x);

putpixel(x, y);

}

glFlush();

}

int main(int argc, char **argv)

{

glutInit(&argc, argv); // initialze GLUT

glutInitDistplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowPosition(50, 100); // position of window

glutInitWindowSize(400, 300); // size of the window

glutCreateWindow("Polynomial Circle"); // name of the window

init();

glutDistplayFunc(drawline);

glutMainLoop();

Page 13: MradulVerma Thirteen Assignment1 CSE3

return 0;

}

/* Polynomial Cirlce in OpenGl in Windows

@author : Aarti Singh

@date : 10 - Oct - 2015

@time : 9:54 PM

*/

#include<glut.h>

#include<stdio.h>

#include<math.h>

void init(void)

{

glClearColor(0.0,0.0,0.0,0.0); //Background color of distplaying window

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,400.0,0.0,400.0); /* orthographic projection yo map content of 2d rectangular area of world coordinate to screen */

}

void putpixel(int x,int y,int w=400,int h=400,int distx=8,int disty=8)

{

glRecti(x*distx + w/2 ,y*disty + h/2 ,(x*distx+distx)+w/2,(y*disty+disty)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 - x*distx ,y*disty + h/2 ,(-(x*distx+distx))+w/2,(y*disty+disty)+h/2);

Page 14: MradulVerma Thirteen Assignment1 CSE3

printf("%f %f\n",x,y);

glRecti(w/2 + x*distx ,-y*disty + h/2 ,((x*distx+distx))+w/2,-(y*disty+disty)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 - x*distx ,-y*disty + h/2 ,(-(x*distx+distx))+w/2,-(y*disty+disty)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 + y*disty ,x*distx + h/2 ,((y*disty+disty))+w/2,(x*disty+distx)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 - y*disty ,x*distx + h/2 ,(-(y*disty+disty))+w/2,(x*distx+distx)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 + y*disty ,-x*distx + h/2 ,((y*distx+disty))+w/2,-(x*distx+distx)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 - y*disty ,-x*distx + h/2 ,(-(y*distx+disty))+w/2,-(x*distx+distx)+h/2);

printf("%f %f\n",x,y);

}

void drawline(void)

{

glClear(GL_COLOR_BUFFER_BIT);

int w = 400;

int h = 400;

int distx=8;

int disty=8;

int xx,yy;

glColor3f(1.0,0.0,0.0); // color of drawing object

glRecti(w/2 , 0 , w/2 + distx , h) ; // draw vertical axis

glRecti(0,h/2 , w , h/2 + disty) ; // draw horizontal axis

glColor3f(1.0,1.0,1.0);

Page 15: MradulVerma Thirteen Assignment1 CSE3

for(xx=0;xx<=w ; xx=xx+distx)

{

for(yy =0;yy<=h ; yy=yy+disty)

{

glBegin(GL_LINES); // for drawing lines

glVertex2i(xx,0);

glVertex2i(xx,h);

glVertex2i(0,yy);

glVertex2i(w,yy);

glEnd();

}

}

glColor3f(0.0,1.0,0.0);

float x=0;

int r=15;

float y =r;

putpixel(x,y);

while(x<=r/1.3)

{

x=x+1;

y=sqrt(r*r - x*x);

putpixel(x,y);

}

glFlush();

}

int main(int argc, char **argv)

Page 16: MradulVerma Thirteen Assignment1 CSE3

{

glutInit(&argc, argv); // initialze GLUT

glutInitDistplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowPosition(50,100); // position of window

glutInitWindowSize(400,300); // size of the window

glutCreateWindow("Polynomial Circle"); // name of the window

init();

glutDistplayFunc(drawline);

glutMainLoop();

return 0;

}

Output :

Page 17: MradulVerma Thirteen Assignment1 CSE3
Page 18: MradulVerma Thirteen Assignment1 CSE3

Ques 3 : Implement a Bresenham circle drawing algorithm.Code :

Code :

/* Bresenham Circle in OpenGl in windows

@author : Arti Singh

@date : 10 - Oct - 2015

@time : 11:16 PM

*/

#include<glut.h>

#include<stdio.h>

void init(void)

{

glClearColor(0.0,0.0,0.0,0.0); //Background color of distplaying window

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,400.0,0.0,400.0); /* orthographic projection yo map content of 2d rectangular area of world coordinate to screen */

Page 19: MradulVerma Thirteen Assignment1 CSE3

}

void putpixel(int x,int y,int w=400,int h=400,int distx=8,int disty=8)

{

glRecti(x*distx + w/2 ,y*disty + h/2 ,(x*distx+distx)+w/2,(y*disty+disty)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 - x*distx ,y*disty + h/2 ,(-(x*distx+distx))+w/2,(y*disty+disty)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 + x*distx ,-y*disty + h/2 ,((x*distx+distx))+w/2,-(y*disty+disty)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 - x*distx ,-y*disty + h/2 ,(-(x*distx+distx))+w/2,-(y*disty+disty)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 + y*disty ,x*distx + h/2 ,((y*disty+disty))+w/2,(x*disty+distx)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 - y*disty ,x*distx + h/2 ,(-(y*disty+disty))+w/2,(x*distx+distx)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 + y*disty ,-x*distx + h/2 ,((y*distx+disty))+w/2,-(x*distx+distx)+h/2);

printf("%f %f\n",x,y);

glRecti(w/2 - y*disty ,-x*distx + h/2 ,(-(y*distx+disty))+w/2,-(x*distx+distx)+h/2);

printf("%f %f\n",x,y);

}

void drawline(void)

{

glClear(GL_COLOR_BUFFER_BIT);

int w = 400;

int h = 400;

int distx=8;

int disty=8;

Page 20: MradulVerma Thirteen Assignment1 CSE3

int xx,yy;

glColor3f(1.0,0.0,0.0); // color of drawing object

glRecti(w/2 , 0 , w/2 + distx , h) ; // draw vertical axis

glRecti(0,h/2 , w , h/2 + disty) ; // draw horizontal axis

glColor3f(1.0,1.0,1.0);

for(xx=0;xx<=w ; xx=xx+distx)

{

for(yy =0;yy<=h ; yy=yy+disty)

{

glBegin(GL_LINES); // for drawing lines

glVertex2i(xx,0);

glVertex2i(xx,h);

glVertex2i(0,yy);

glVertex2i(w,yy);

glEnd();

}

}

glColor3f(0.0,1.0,0.0);

int x=0;

int r=15;

int y =r;

int d=3-2*r;

Page 21: MradulVerma Thirteen Assignment1 CSE3

putpixel(x,y);

while(x <=y)

{

if(d>=0)

{

x=x+1;

y=y-1;

d=d+(4*(x-y))+10;

}

else

{

x=x+1;

d=d+(4*(x))+6;

}

putpixel(x,y);

}

glFlush();

}

int main(int argc, char **argv)

{

glutInit(&argc, argv); // initialze GLUT

glutInitDistplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowPosition(50,100); // position of window

glutInitWindowSize(400,300); // size of the window

glutCreateWindow("Bresenham Circle"); // name of the window

init();

Page 22: MradulVerma Thirteen Assignment1 CSE3

glutDistplayFunc(drawline);

glutMainLoop();

return 0;

}

Output :

Ques 4: Draw ellipse by using the standard equation of ellipse.

Page 23: MradulVerma Thirteen Assignment1 CSE3

Code :

/* Method for ellipse drawing in OpenGL in Windows

@author : Mradul Verma

@date : 10 - Oct - 2015

@time : 11:30 PM

*/

#include<glut.h>

#include<stdio.h>

#include<math.h>

void init(void)

{

glClearColor(0.0, 0.0, 0.0, 0.0); //Background color of distplaying window

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, 400.0, 0.0, 400.0); /* orthographic projection yo map content of 2d rectangular area of world coordinate to screen */

}

void putpixel(int x, int y, int w = 400, int h = 400, int distx = 8, int disty = 8)

{

glRecti(x*distx + w / 2, y*disty + h / 2, (x*distx + distx) + w / 2, (y*disty + disty) + h / 2);

printf("%f %f\n", x, y);

glRecti(w / 2 - x*distx, y*disty + h / 2, (-(x*distx + distx)) + w / 2, (y*disty + disty) + h / 2);

printf("%f %f\n", x, y);

glRecti(w / 2 + x*distx, -y*disty + h / 2, ((x*distx + distx)) + w / 2, -(y*disty + disty) + h / 2);

printf("%f %f\n", x, y);

Page 24: MradulVerma Thirteen Assignment1 CSE3

glRecti(w / 2 - x*distx, -y*disty + h / 2, (-(x*distx + distx)) + w / 2, -(y*disty + disty) + h / 2);

printf("%f %f\n", x, y);

}

void drawline(void)

{

glClear(GL_COLOR_BUFFER_BIT);

int w = 400;

int h = 400;

int distx = 8;

int disty = 8;

int xx, yy;

glColor3f(1.0, 0.0, 0.0); // color of drawing object

glRecti(w / 2, 0, w / 2 + distx, h); // draw vertical axis

glRecti(0, h / 2, w, h / 2 + disty); // draw horizontal axis

glColor3f(1.0, 1.0, 1.0);

for (xx = 0; xx <= w; xx = xx + distx)

{

for (yy = 0; yy <= h; yy = yy + disty)

{

glBegin(GL_LINES); // for drawing lines

glVertex2i(xx, 0);

glVertex2i(xx, h);

glVertex2i(0, yy);

glVertex2i(w, yy);

glEnd();

Page 25: MradulVerma Thirteen Assignment1 CSE3

}

}

glColor3f(0.0, 1.0, 0.0);

float x = 0;

int a = 20, b =12;

float y =0;

if (a > b)

{

y = b;

while (x < a)

{

putpixel(x, y);

x = x + 1;

y = b * sqrt(((a*a) - (x*x)) / (a*a));

}

}

else

{

x = a;

while (y < b)

{

putpixel(x, y);

y = y + 1;

x = a * sqrt(((b*b) - (y*y)) / (b*b));

}

}

putpixel(x, y);

Page 26: MradulVerma Thirteen Assignment1 CSE3

glFlush();

}

int main(int argc, char **argv)

{

glutInit(&argc, argv); // initialze GLUT

glutInitDistplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowPosition(50, 100); // position of window

glutInitWindowSize(400, 300); // size of the window

glutCreateWindow("Ellipse"); // name of the window

init();

glutDistplayFunc(drawline);

glutMainLoop();

return 0;

}

Output :

Page 27: MradulVerma Thirteen Assignment1 CSE3