Posts

Showing posts from March, 2016

Bresenham's Line Drawing Algorithm in OpenGL C++ code

//Author: Azhar Khan//Rajasthan Technical University//Poornima Institute of Engineering And Technology#include<GL/glut.h>#include<windows.h>#include<stdio.h>GLint x0,y0,xEnd,yEnd;void init(){glClearColor(1.0,1.0,1.0,0.0);glColor3f(1.0f,0.0f,0.0f);glPointSize(1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,600.0,0.0,600.0);}void setPixel(GLint xcoordinate,GLint ycoordinate){glBegin(GL_POINTS);glVertex2i(xcoordinate,ycoordinate);glEnd();glFlush();}void lineBA(GLint x0,GLint y0,GLint xEnd,GLint yEnd){GLint dx = xEnd-x0;GLint dy = yEnd-y0;GLint steps,k;     steps=dx;GLint x,y,p0=(2*dy)-dx;     setPixel(x0,y0);     x=x0;     y=y0;for(k=0;k<steps;k++){if(p0<0){             p0=p0+(2*dy);             x+=1;}else{             p0=p0+(2*dy)-(2*dx);             x+=1;             y+=1;}         setPixel(x,y);}}void readInput(){printf("\nEnter x0, y0, xEnd, yEnd(resp): ");scanf("%i%i

DDA algorithm implementation using OpenGL in C++

#include<GL/glut.h>#include<windows.h>#include<stdlib.h>#include<stdio.h>GLint x0,y0,xEnd,yEnd;inlineGLint round(constGLfloat a){returnGLint(a+0.5);}void myInit(void){glClearColor(0.0,1.0,1.0,0.0);glColor3f(1.0f,0.0f,0.0f);glPointSize(3.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,640.0,0.0,480.0);}void readInput(){printf("Enter x0, y0, xEnd, yEnd(resp): ");scanf("%i%i%i%i",&x0,&y0,&xEnd,&yEnd);}void setPixel(GLint xcoordinate,GLint ycoordinate){glBegin(GL_POINTS);glVertex2i(xcoordinate,ycoordinate);glEnd();glFlush();}void lineDDA(GLint x0,GLint y0,GLint xEnd,GLint yEnd){GLint dx =abs(xEnd-x0);GLint dy =abs(yEnd-y0);GLint steps,k;GLfloat xIncrement,yIncrement,x=x0,y=y0;if(dx>dy)         steps = dx;else         steps = dy;     xIncrement =GLfloat(dx)/GLfloat(steps);     yIncrement =GLfloat(dy)/GLfloat(steps);     setPixel(round(x),round(y));for(k=1;k<steps;k++