Friday, 20 March 2020

Implement translation, sheer, rotation and scaling transformations on equilateral triangle and rhombus.

#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#define MAX 10

int n,op,tx,ty,sx,sy,angle,shx,shy,y,z,xr,yr;
float a[MAX][3],b[3][3],c[MAX][3];


/* Function to plot a point */
void setPixel(GLint x, GLint y) {
  glBegin(GL_POINTS);
  glVertex2i(x,y);
  glEnd();
}

// Function of Rounding.
inline int round(const GLfloat a)
{
    return int(a+0.5);
}

//  DDA  Line  Algorithm
void lineDDA(int x0,int y0,int xEnd,int yEnd)
{
    int dx = xEnd-x0;
    int dy = yEnd-y0;
    int steps,k;
    GLfloat xIncrement,yIncrement,x=x0,y=y0;

    if(abs(dx) > abs(dy))
        steps = abs(dx);
    else
        steps = abs(dy);

    xIncrement = GLfloat(dx) / GLfloat(steps);
    yIncrement = GLfloat(dy) / GLfloat(steps);
    setPixel(round(x),round(y));

    for(k=1;k<steps;k++)
    {
        x+= xIncrement;
        y+= yIncrement;
        setPixel(round(x),round(y));
    }
    glFlush();
}


void axis()
{
int i,x,y;
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0,1.0,1.0,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);
lineDDA(320,30,320,450); //x axis
lineDDA(20,240,620,240); //y axis
glColor3f (0.0, 1.0, 1.0);
lineDDA(0,0,640,480);//digonal
lineDDA(0,480,640,0);//digonal

for(i=-10,x=20;i<=10;i++,x+=30) //horizontal line numbering
{
glColor3f (0.0, 1.0, 0.0);
lineDDA(x,238,x,242);
}

for(i=7,y=30;i>=-7;i--,y+=30) //vertical line numbering
{
glColor3f (0.0, 1.0, 0.0);
lineDDA(315,y,325,y);
}
}

void accept()
{
int i;
printf("Enter the coordinate : ");
for(i=0;i<n;i++)
{
printf("\nx%d,y%d : ",i+1,i+1);
scanf("%f%f",&a[i][0],&a[i][1]);
a[i][2]=1;
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
a[n][2]=1;
}

void show()
{
int i;
axis();
glColor3f (1.0, 0.0, 0.0);

        //  INPUT DATA
for(i=0;i<n;i++)
    lineDDA(320+a[i][0],240+a[i][1],320+a[(i+1)%n][0],240+a[(i+1)%n][1]);//homogeneous form


glColor3f (0.0, 0.0, 1.0);

//  OUTPUT DATA
for(i=0;i<n;i++)
    lineDDA(320+c[i][0],240+c[i][1],320+c[(i+1)%n][0],240+c[(i+1)%n][1]);//homogeneous form
}


void mul()
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
}
}

for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}


}

void trans()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
b[i][j]=1;
else
b[i][j]=0;
  }
}
b[2][0]=tx;
b[2][1]=ty;

}


void scal()
{
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=1;
b[0][1]=b[1][0]=b[1][2]=b[0][2]=b[2][1]=b[2][0]=0;
}

void rota()
{
int i,j;
float rad;

if(z==1)
angle = angle ;
else
angle = angle * -1;

rad=(angle*3.14)/180;

b[0][0]=b[1][1]=cos(rad);
b[2][2]=1;
b[0][1]=sin(rad);
b[1][0]=-sin(rad);
b[2][0]=b[2][1]=b[1][2]=0;


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%f",b[i][j]);
}
printf("\n");
}

}

void rot_ref()
{
float rad;
int sign;

rad=(angle*3.14)/180;

if(z==1)
sign=1;
else
sign=-1;
b[0][0]=b[1][1]=cos(rad);
b[2][2]=1;
b[0][1]=sign*sin(rad);
b[1][0]=-sign*sin(rad);
b[2][0]=-xr*cos(rad)+yr*sign*sin(rad)+xr;
b[2][1]=-xr*sin(rad)+yr*(-sign)*cos(rad)+yr;
b[0][2]=b[1][2]=0;
}

void shear()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
b[i][j]=1;
else
b[i][j]=0;
  }
}
b[0][1]=shx;
b[1][0]=shy;


}

void reflection()
{
int i;
b[2][2]=1;
b[0][1]=b[1][0]=b[1][2]=b[0][2]=b[2][1]=b[2][0]=0;

switch(z)
{
case 1 :  b[0][0]=1;
  b[1][1]=-1;
  break;

case 2 :  b[0][0]=-1;
  b[1][1]=1;
  break;

case 3 :  b[0][0]=-1;
  b[1][1]=-1;
  break;

case 4 :  b[0][0]=0;
  b[1][1]=0;
    b[0][1]=1;
  b[1][0]=1;
  break;

case 5 :  b[0][0]=0;
  b[1][1]=0;
    b[0][1]=-1;
  b[1][0]=-1;
  break;

}
}


void choice(void)
{
printf("\n\n***********   2D  Transformations   *********");
printf("\n\nEnter the no of vertices of polygon : ");
scanf("%d",&n);
accept();
printf("\n***********MENU**********");
printf("\n \n1) Translation \n2) Scaling \n3) Rotation \n4) Rotation of arbitary  \n5) Shearing \n6) Reflection");
printf("\nEnter your choice : ");
scanf("%d",&op);
switch(op)
{
       case 1:  printf("\nThe polygon before translation");
printf("\nEnter the tx : ");
scanf("%d",&tx);
printf("\nEnter the ty : ");
scanf("%d",&ty);
trans();
mul();
show();
break;

       case 2:  printf("\nThe polygon before scaling");
printf("\nEnter the sx");
scanf("%d",&sx);
printf("\nEnter the sy");
scanf("%d",&sy);
scal();
mul();
show();
break;

       case 3:  printf("\nThe polygon befor rotation");
printf("\nEnter the angle :  ");
scanf("%d",&angle);
printf("\nPress 1 for anticlockwise and 2 for clockwise : ");
scanf("%d",&z);
rota();
mul();
show();
break;

      case 4:   printf("\nThe polygon befor rotation");
printf("\nEnter the angle : ");
scanf("%d",&angle);
printf("\nPress 1 for anticlockwise and 2 for clockwise : ");
scanf("%d",&z);
printf("Enter the x and y coordinate : ");
scanf("%d%d",&xr,&yr);
rot_ref();
mul();
show();
break;

case 5: printf("\nThe polygon before Shearing");
printf("\nEnter the Shx : ");
scanf("%d",&shx);
printf("\nEnter the Shy : ");
scanf("%d",&shy);
shear();
mul();
show();
break;

case 6: printf("\nThe polygon before Reflection");
printf("\n-----------------------------");
printf("\n\t1. Against X-axis\n\t2. Against Y-axis\n\t3. Against Origin");
printf("\n\t4. X = Y\n\t5. X = -Y\n\tEnter you Choice : ");
scanf("%d",&z);
  reflection();
mul();
show();
break;


      default: printf("Invalid option");

}
}



void init()
{ 
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title "2D Transformations" */
  glutCreateWindow("2D Transformations");
  /* Set clear color to white */
  glClearColor(1.0,1.0,1.0,1.0);
  /* Set fill color to black */
  glColor3f(0.0,0.0,0.0);
  /* glViewport(0 , 0 , 640 , 480); */
  /* glMatrixMode(GL_PROJECTION); */
  /* glLoadIdentity(); */
glPointSize(1.0f);
  gluOrtho2D(0 , 640 , 0 , 480);

 
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  choice();

  init();
  glutDisplayFunc(show);

  glutMainLoop();
  return 0;
}

/*
ubuntu@ubuntu:~$ g++ 6poly.c -lglut -lGL -lGLEW -lGLU -o final
ubuntu@ubuntu:~$ ./final


***********   2D  Transformations   *********

Enter the no of vertices of polygon : 3
Enter the coordinate :
x1,y1 : 10
10

x2,y2 : 100
100

x3,y3 : 100
10

***********MENU**********

1) Translation
2) Scaling
3) Rotation
4) Rotation of arbitary 
5) Shearing
6) Reflection
Enter your choice : 1

The polygon before translation
Enter the tx : 10

Enter the ty : 1
ubuntu@ubuntu:~$ ./final


***********   2D  Transformations   *********

Enter the no of vertices of polygon : 3
Enter the coordinate :
x1,y1 : 10
10

x2,y2 : 100
100

x3,y3 : 100
10

***********MENU**********

1) Translation
2) Scaling
3) Rotation
4) Rotation of arbitary 
5) Shearing
6) Reflection
Enter your choice : 2

The polygon before scaling
Enter the sx1

Enter the sy2
ubuntu@ubuntu:~$ ./final


***********   2D  Transformations   *********

Enter the no of vertices of polygon : 3
Enter the coordinate :
x1,y1 : 10
10

x2,y2 : 100
100

x3,y3 : 100
10

***********MENU**********

1) Translation
2) Scaling
3) Rotation
4) Rotation of arbitary 
5) Shearing
6) Reflection
Enter your choice : 3

The polygon befor rotation
Enter the angle :  45

Press 1 for anticlockwise and 2 for clockwise : 2
0.707388-0.7068250.000000
0.7068250.7073880.000000
0.0000000.0000001.000000
ubuntu@ubuntu:~$ ./final


***********   2D  Transformations   *********

Enter the no of vertices of polygon : 3
Enter the coordinate :
x1,y1 : 10
10

x2,y2 : 100
100

x3,y3 : 100
10

***********MENU**********

1) Translation
2) Scaling
3) Rotation
4) Rotation of arbitary 
5) Shearing
6) Reflection
Enter your choice : 5

The polygon before Shearing
Enter the Shx : 2

Enter the Shy : 1
ubuntu@ubuntu:~$ ./final


***********   2D  Transformations   *********

Enter the no of vertices of polygon : 3
Enter the coordinate :
x1,y1 : 100
100

x2,y2 : 10
10

x3,y3 : 100
10

***********MENU**********

1) Translation
2) Scaling
3) Rotation
4) Rotation of arbitary 
5) Shearing
6) Reflection
Enter your choice : 6

The polygon before Reflection
-----------------------------
1. Against X-axis
2. Against Y-axis
3. Against Origin
4. X = Y
5. X = -Y
Enter you Choice : 1
ubuntu@ubuntu:~$ ^C
ubuntu@ubuntu:~$
*/

Implement Cohen Sutherland Hodgman algorithm to clip any given polygon. Provide the vertices of the polygon to be clipped and pattern of clipping interactively

//#include <windows.h>
#include <GL/glut.h>

struct Point
{

    float x,y;
} w[4],oVer[4];
int Nout;   

void drawPoly(Point p[],int n)
{
    glBegin(GL_POLYGON);           
    for(int i=0;i<n;i++)
        glVertex2f(p[i].x,p[i].y);   
    glEnd();
}

bool insideVer(Point p)
{   
        if((p.x>=w[0].x)&&(p.x<=w[2].x))       
        if((p.y>=w[0].y)&&(p.y<=w[2].y))
        return true;               
        return false;       
}

void addVer(Point p){       
    oVer[Nout]=p;       
    Nout=Nout+1;
}

Point getInterSect(Point s,Point p,int edge)
{
    Point in;           
    float m;
    if(w[edge].x==w[(edge+1)%4].x)
      {
//Vertical Line       
        m=(p.y-s.y)/(p.x-s.x);   
        in.x=w[edge].x;               
        in.y=in.x*m+s.y;
    }
    else
{
//Horizontal Line           
        m=(p.y-s.y)/(p.x-s.x);   
        in.y=w[edge].y;
        in.x=(in.y-s.y)/m;       
    }   
    return in;
}

void clipAndDraw(Point inVer[],int Nin){
    Point s,p,interSec;   
    for(int i=0;i<4;i++)
    {
        Nout=0;
        s=inVer[Nin-1];
        for(int j=0;j<Nin;j++)
        {           
            p=inVer[j];
            if(insideVer(p)==true)
               {               
                if(insideVer(s)==true){
                    addVer(p);                                   
                }
                else
                 {
                    interSec=getInterSect(s,p,i);
                    addVer(interSec);                   
                    addVer(p);                   
                }
            }
            else
               {       
                if(insideVer(s)==true){
                    interSec=getInterSect(s,p,i);               
                    addVer(interSec);                                                   
                }
            }
            s=p;           
        }       
        inVer=oVer;       
        Nin=Nout;   
    }   
    drawPoly(oVer,4);
}

void init(){
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glMatrixMode(GL_PROJECTION);       
    glLoadIdentity();   
    glOrtho(0.0,100.0,0.0,100.0,0.0,100.0);
    glClear(GL_COLOR_BUFFER_BIT);   
    w[0].x =20,w[0].y=10;
    w[1].x =20,w[1].y=80;
    w[2].x =80,w[2].y=80;
    w[3].x =80,w[3].y=10;
}
void display(void){   
    Point inVer[4];
    init();
    // As Window for Clipping
    glColor3f(1.0f,0.0f,0.0f);       
    drawPoly(w,4);
    // As Rect
    glColor3f(0.0f,1.0f,0.0f);
    inVer[0].x =10, inVer[0].y=40;
    inVer[1].x =10, inVer[1].y=60;
    inVer[2].x =60, inVer[2].y=60;
    inVer[3].x =60, inVer[3].y=40;
    drawPoly(inVer,4);
    // As Rect
    glColor3f(0.0f,0.0f,1.0f);
    clipAndDraw(inVer,4);       
    // Print
    glFlush();
}

int main(int argc,char *argv[]){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Polygon Clipping!");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Draw a 4X4 chessboard rotated 45˚ with the horizontal axis. Use Bresenham algorithm to draw all the lines. Use seed fill algorithm to fill black squares of the rotated chessboard

#include <GL/glut.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#define PI 3.14159265/180
using namespace std;
float rtri=0.0f;
int ww=660,wh=660; 
int xi,yi,xf,yf,xx,yy;
float fillcol[3]={0.9,0.0,0.2};
float bordercol[3]={0.0,0.0,0.0};
void setpixel(int pointx,int pointy,float f[3])
{
glBegin(GL_POINTS);
glColor3f(f[0],f[1],f[2]);
glVertex2d(pointx,pointy);
glEnd();
glFlush();
}
void getpixel(int x,int y,float pixels[3])
{
glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,pixels);
}
int sign(float arg)
{
if(arg<0)
return -1;
else if(arg==0)
return 0;
else
return 1;
}
double calx(double xold,double yold,double angle)
{
double xnew;
xnew=xold*cos(PI*angle)-yold*sin(PI*angle);
return xnew;
}
double caly(double xold,double yold,double angle)
{
double ynew;
ynew=xold*sin(PI*angle)+yold*cos(PI*angle) ;
return ynew;
}
void Bre(int X1,int Y1,int X2,int Y2,double angle)
{
 double xold=0,yold=0;
 double xnew=0,ynew=0;
xold=X1;
yold=Y1;
xnew=calx(xold,yold,angle);
ynew=caly(xold,yold,angle);
X1=(int)xnew;
   Y1=(int)ynew;
xold=X2;
yold=Y2;
xnew=xold*cos(PI*angle)-yold*sin(PI*angle) ;
ynew=xold*sin(PI*angle)+yold*cos(PI*angle) ;
X2=(int)xnew;
Y2=(int)ynew;
  float dx=abs(X2-X1);
 float dy=abs(Y2-Y1);
 int s1,s2,exc,y,x,i;
  float g,temp;
x=X1;
y=Y1;
s1=sign(X2-X1);
s2=sign(Y2-Y1);
glBegin(GL_POINTS); // Plot the points
//rotate 45 on x axis
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
exc=1;
}
else
{
exc=0;
}
g=2*dy-dx;
i=1;
while(i<=dx)
{
glVertex2d(x,y);
while(g>=0)
{
if(exc==1)
x=x+s1;
else
y=y+s2;
g=g-2*dx;
}
if(exc==1)
y=y+s2;
else
x=x+s1;
g=g+2*dy;
i++;
}
 glEnd();
glFlush();
}
void display() 
  { 
double angle=45;
glClearColor(1.0,1.0,1.0,0); // Set Background color to white
glColor3f(0.0,0.0,0.0);
glViewport(0,0,1360,760);
glLoadIdentity();
gluOrtho2D(0 , 1360 , 0 , 760);       
glClear(GL_COLOR_BUFFER_BIT);
Bre(300,00,500,00,angle);
Bre(300,00,300,200,angle);
Bre(500,200,300,200,angle);
Bre(500,200,500,00,angle);//outer
Bre(300,50,500,50,angle);
Bre(300,100,500,100,angle);
Bre(300,150,500,150,angle);//outer
Bre(350,00,350,200,angle);
Bre(400,00,400,200,angle);
Bre(450,00,450,200,angle);//outer
angle=0;
Bre(300,00,500,00, angle);
Bre(300,00,300,200, angle);
Bre(500,200,300,200, angle);
Bre(500,200,500,00, angle);
Bre(300,50,500,50, angle);
Bre(300,100,500,100,angle);
Bre(300,150,500,150,angle);//outer
Bre(350,00,350,200, angle);
Bre(400,00,400,200, angle);
Bre(450,00,450,200, angle);//outer
}
void bfill(int x,int y,float fillcolor[3],float bordercolor[3])
{
float inter_color[3];
getpixel(x,y,inter_color);
if((inter_color[0]!=bordercolor[0]&&inter_color[1]!=bordercolor[1]&&inter_color[2]!=bordercolor[2])&&(inter_color[0]!=fillcolor[0]&&inter_color[1]!=fillcolor[1]&&inter_color[2]!=fillcolor[2]))
{
setpixel(x,y,fillcolor);
bfill(x+1,y,fillcolor,bordercolor);
bfill(x-1,y,fillcolor,bordercolor);
bfill(x,y+1,fillcolor,bordercolor);
bfill(x,y-1,fillcolor,bordercolor);
}
}
 void mouse(int btn,int state,int x,int y) 
  { 
     if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) 
     { 
              xi=x; 
               yi=wh-y;
printf("\nPoints collected are:%d and %d",xi,yi);
bfill(xi,yi,fillcol,bordercol);   
     }
  }

 int main(int argc,char** argv) 
 {
glutInit(&argc,argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
glutInitWindowSize(ww,wh);
glutInitWindowPosition(0,0);
glutCreateWindow("CheckBox(Click on any box to fill color)"); 
glutDisplayFunc(display); 
glutMouseFunc(mouse); 
glutMainLoop(); 
return 0;
  } 

Draw the polygons by using the mouse. Choose colors by clicking on the designed color pane

#include<iostream>
#include<GL/glut.h>
#include<math.h>
#define w 500
#define h 500

using namespace std;
float a[30][2];
int k=0;

void myMouse(GLint button,GLint state,GLint x,GLint y)
{

if(state==GLUT_DOWN)
{
if(button==GLUT_LEFT_BUTTON)
{
a[k][0]=(float)(x-250);
a[k][1]=(float)(250-y);

glBegin(GL_POINTS);
glVertex2f(a[k][0],a[k][1]);
glEnd();
k++;
glFlush();
}
if(button==GLUT_RIGHT_BUTTON)
{
glBegin(GL_LINE_LOOP);
for(int i=0;i<k;i++)
{
glVertex2f(a[i][0],a[i][1]);
}
glEnd();
k=0;
glFlush();

}
}
}
void menu(GLint item)
{
if(item==1)
{
       glColor3f(1.0,0.0,0.0);
       glutMouseFunc(myMouse);
}
if(item==2)
{
glColor3f(0.0,1.0,0.0);
glutMouseFunc(myMouse);
}
if(item==3)
{
glColor3f(0.0,0.0,1.0);
glutMouseFunc(myMouse);
}
if(item==4)
{
glColor3f(0.0,1.0,1.0);
glutMouseFunc(myMouse);
}

}


void myInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glPointSize(5);
glLoadIdentity();
gluOrtho2D(-w/2,w/2,-h/2,h/2);
glClear(GL_COLOR_BUFFER_BIT);

}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("POLYGON(First Middle Click->Then LEft Clicks->Then Right Clicks)");
myInit();

    glutCreateMenu(menu);
    glutAddMenuEntry("RED",1);
    glutAddMenuEntry("GREEN",2);
    glutAddMenuEntry("BLUE",3);
    glutAddMenuEntry("CYAN",4);
    glutAttachMenu(GLUT_MIDDLE_BUTTON);
    glutMainLoop();

}

Wednesday, 11 January 2017

Draw inscribed and Circumscribed circles in the triangle by using Bresenham circle and bresenham Line Alorithm

Program:


  #include<stdio.h> 
  #include<GL/glut.h> 
  #include<math.h> 
  int ww=1200,wh=800; 
  int xi,yi,xf,yf,r;
  float theta=2.0933;

 void putpixel(int x,int y) 
  { 
       glBegin(GL_POINTS); 
       glVertex2i(x,y); 
       glEnd(); 
       glFlush(); 
  } 
  double round(double n) 
  { 
       return (n>=0)?(int)(n+0.5):(int)(n-0.5); 
  } 
  void Bresenham_circle(int xc, int yc, int xr) 
  { 
        int x=0,y=xr;
        int d=3-2*y;
        while(x<=y){ 
        putpixel(x+xc, y+yc); 
        putpixel(xc+y, yc+x); 
        putpixel(xc-x, yc+y); 
        putpixel(xc-x, yc-y); 
        putpixel(xc-y, yc+x); 
        putpixel(xc-y, yc-x); 
        putpixel(xc+y, yc-x); 
        putpixel(xc+x, yc-y); 
            if(d<0) 
                 d=d+(4*x)+6; 
            else{ 
                 d=d+(4*(x-y))+10; 
                 y--; 
            } 
            x++; 
       } 
  } 

 void bresenhamAlg (int x0, int y0, int x1, int y1) 
  { 
  int dx = abs (x1 - x0); 
  int dy = abs (y1 - y0); 
  int x, y; 
  if (dx >= dy) 
  { 
   int d = 2*dy-dx; 
   int ds = 2*dy; 
   int dt = 2*(dy-dx); 
       if (x0 < x1)  
       { 
            x = x0; 
            y = y0; 
       } 
        else 
        {  
             x = x1; 
             y = y1; 
             x1 = x0; 
             y1 = y0; 
        } 
       putpixel (x, y); 
   while (x < x1) 
   { 
        if (d < 0) 
        d += ds; 
        else { 
             if (y < y1) { 
              y++; 
              d += dt; 
             }  
             else { 
              y--; 
              d += dt; 
             } 
            } 
   x++; 
        putpixel (x, y); 
       }  }
       else {  
             int d = 2*dx-dy; 
             int ds = 2*dx; 
             int dt = 2*(dx-dy); 
             if (y0 < y1) { 
             x = x0; 
             y = y0; 
             } 
             else {  
             x = x1; 
             y = y1; 
             y1 = y0; 
             x1 = x0; 
             } 
            putpixel (x, y);  
        while (y < y1) 
        { 
              if (d < 0) 
                 d += ds; 
              else { 
                      if (x > x1){ 
                      x--; 
                      d += dt; 
                 } else { 
                      x++; 
                      d += dt; 
                   } 
              } 
              y++;
              putpixel (x, y); 
        } 
       } 
  } 
 void triangle (int ix , int iy)
 {
    int x=ix, y=iy,x1,x2,y1,y2;
    x1=xi+(x-xi)*cos(theta)-(y-yi)*sin(theta);
    y1=yi-r/2;
    x2=xi+(x-xi)*cos(theta)+(y-yi)*sin(theta);
    y2=yi-r/2;;
    bresenhamAlg (x, y, x1, y1);
    bresenhamAlg (x, y, x2, y2);
    bresenhamAlg (x1, y1, x2, y2);
 }
 void display() 
  { 
        glClearColor(0.4,0.7,0.2,1.0); 
        glColor3f(0.5,0.3,0.0); 
        glClear(GL_COLOR_BUFFER_BIT); 
        glutSwapBuffers();
        Bresenham_circle(xi,yi,r);
        Bresenham_circle(xi,yi,r/2);
        triangle(xi,(yi+r));
        glFlush(); 
  } 
 

  void myinit() 
  { 
        glViewport(0,0,ww,wh); 
        glMatrixMode(GL_PROJECTION); 
        glLoadIdentity(); 
        gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh); 
        glMatrixMode(GL_MODELVIEW); 
  } 
  int main(int argc,char** argv) 
  { 
    printf("enter centre of the circle");
    scanf("%d%d",&xi,&yi);
    printf("\nenter radius of circle");
    scanf("%d",&r);      
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(ww,wh); 
    glutCreateWindow("Bresenham-Circle"); 
    myinit();
    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
  } 



Output:

Draw the pattern using BRESENHAM Line drawing algorithms using MOUSE in OpenGL


 Program:

#include <GL/glut.h> 
#include <math.h> 
int ww = 600, wh = 400; 
int first = 0; 
int xi, yi, xf, yf; 
void putPixel (int x, int y) 
{ 
   glBegin (GL_POINTS); 
   glVertex2i (x, y); 
   glEnd (); 
   glFlush ();
  } 
  void display() 
  { 
   glClearColor(0.4, 0.7, 0.5, 1.0); 
   glClear(GL_COLOR_BUFFER_BIT); 
   glFlush(); 
  } 
  void bresenhamAlg (int x0, int y0, int x1, int y1) 
  { 
  int dx = abs (x1 - x0); 
  int dy = abs (y1 - y0); 
  int x, y; 
  if (dx >= dy) 
  { 
   int d = 2*dy-dx; 
   int ds = 2*dy; 
   int dt = 2*(dy-dx); 
       if (x0 < x1)  
       { 
            x = x0; 
            y = y0; 
       } 
        else 
        {  
             x = x1; 
             y = y1; 
             x1 = x0; 
             y1 = y0; 
        } 
       putPixel (x, y); 
   while (x < x1) 
   { 
        if (d < 0) 
        d += ds; 
        else { 
             if (y < y1) { 
              y++; 
              d += dt; 
             }  
             else { 
              y--; 
              d += dt; 
             } 
            } 
   x++; 
        putPixel (x, y); 
       }  }
       else {  
             int d = 2*dx-dy; 
             int ds = 2*dx; 
             int dt = 2*(dx-dy); 
             if (y0 < y1) { 
             x = x0; 
             y = y0; 
             } 
             else {  
             x = x1; 
             y = y1; 
             y1 = y0; 
             x1 = x0; 
             } 
            putPixel (x, y);  
        while (y < y1) 
        { 
              if (d < 0) 
                 d += ds; 
              else { 
                      if (x > x1){ 
                      x--; 
                      d += dt; 
                 } else { 
                      x++; 
                      d += dt; 
                   } 
              } 
              y++; 
              putPixel (x, y); 
        } 
       } 
  } 
       void mouse(int btn, int state, int x, int y) 
       { 
        if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
        { 
        switch(first) 
        { 
        case 0: 
         xi = x; 
         yi = (wh-y); 
         first = 1; 
         break; 
        case 1: 
         xf = x; 
         yf = (wh-y); 
        
glColor3f(1.0,0.0,0.0);
    bresenhamAlg(xi,yi,xi,yf);
    bresenhamAlg(xi,yf,xf,yf);
    bresenhamAlg(xf,yf,xf,yi);
    bresenhamAlg(xf,yi,xi,yi);
 glColor3f(0.0,1.0,0.0);
    bresenhamAlg(xi,(yi+yf)/2,(xi+xf)/2,yf);
    bresenhamAlg((xi+xf)/2,yf,xf,(yi+yf)/2);
    bresenhamAlg(xf,(yi+yf)/2,(xi+xf)/2,yi);
    bresenhamAlg((xi+xf)/2,yi,xi,(yi+yf)/2);   
 glColor3f(0.0,0.0,1.0);
    bresenhamAlg((xi+(xi+xf)/2)/2,(yf+(yi+yf)/2)/2,(xf+(xi+xf)/2)/2,(yf+(yi+yf)/2)/2);
    bresenhamAlg((xf+(xi+xf)/2)/2,(yf+(yi+yf)/2)/2,(xf+(xi+xf)/2)/2,(yi+(yi+yf)/2)/2);
    bresenhamAlg((xf+(xi+xf)/2)/2,(yi+(yi+yf)/2)/2,(xi+(xi+xf)/2)/2, (yi+(yi+yf)/2)/2);
    bresenhamAlg((xi+(xi+xf)/2)/2, (yi+(yi+yf)/2)/2,(xi+(xi+xf)/2)/2,(yf+(yi+yf)/2)/2); 
         first = 0; 
         break;  
        } 
        } 
       } 
  void myinit() 
  {       
     glViewport(0,0,ww,wh); 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh); 
     glMatrixMode(GL_MODELVIEW); 
  } 
  int main(int argc, char** argv) 
  { 
     glutInit(&argc,argv); 
     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
     glutInitWindowSize(ww,wh); 
     glutCreateWindow("Bresenham Line Algorithm With Mouse"); 
     glutDisplayFunc(display); 
     myinit(); 
     glutMouseFunc(mouse); 
     glutMainLoop(); 
     return 0; 
  } 

Output:

Tuesday, 10 January 2017

Draw the pattern using DDA Line drawing algorithm with KeyBoard in OpenGL

Program:
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
int xx1,xx2,yy1,yy2;
int sign(int x)
{
    if(x > 0) return 1;
    if(x < 0) return -1;
    return 0;
}
void drawline(int X1,int Y1, int X2,int Y2)  
{
    float x,y,dx,dy,length;
    int i;
    dx=abs(X2-X1);
    dy=abs(Y2-Y1);
    if(dx>=dy)
        length=dx;
    else
        length=dy;
    dx=(X2-X1)/length;
    dy=(Y2-Y1)/length;
    x=X1 + 0.5*sign(X1);
    y=Y1 + 0.5*sign(Y1);
    i=1;
    while(i<=length)
    {
   
                 glBegin(GL_POINTS);
             glVertex2i(x,y);
             glEnd();
                 glFlush();
        x=x+dx;
        y=y+dy;
        i=i+1;
    }
    glFlush();
}
  
void display(void)                            
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,0.0,0.0);
    drawline(xx1,yy1,xx1,yy2);
    drawline(xx1,yy2,xx2,yy2);
    drawline(xx2,yy2,xx2,yy1);
    drawline(xx2,yy1,xx1,yy1);
     glColor3f(0.0,1.0,0.0);
    drawline(xx1,(yy1+yy2)/2,(xx1+xx2)/2,yy2);
    drawline((xx1+xx2)/2,yy2,xx2,(yy1+yy2)/2);
    drawline(xx2,(yy1+yy2)/2,(xx1+xx2)/2,yy1);
    drawline((xx1+xx2)/2,yy1,xx1,(yy1+yy2)/2);  
     glColor3f(0.0,0.0,1.0);
    drawline((xx1+(xx1+xx2)/2)/2,(yy2+(yy1+yy2)/2)/2,(xx2+(xx1+xx2)/2)/2,(yy2+(yy1+yy2)/2)/2);
    drawline((xx2+(xx1+xx2)/2)/2,(yy2+(yy1+yy2)/2)/2,(xx2+(xx1+xx2)/2)/2,(yy1+(yy1+yy2)/2)/2);
    drawline((xx2+(xx1+xx2)/2)/2,(yy1+(yy1+yy2)/2)/2,(xx1+(xx1+xx2)/2)/2, (yy1+(yy1+yy2)/2)/2);
    drawline((xx1+(xx1+xx2)/2)/2, (yy1+(yy1+yy2)/2)/2,(xx1+(xx1+xx2)/2)/2,(yy2+(yy1+yy2)/2)/2);  

}
void init(void)
{
       glViewport(0,0,640,480); 
       glMatrixMode(GL_PROJECTION); 
       glLoadIdentity(); 
       gluOrtho2D(0.0,640.0,0.0,480.0); 
       glMatrixMode(GL_MODELVIEW);    
}
int main(int argc,char** argv)
{
    printf("\n Enter vertex x1 ");
        scanf("%d",&xx1);
        printf("\n Enter vertex y1 ");
        scanf("%d",&yy1);
        printf("\n Enter vertex x2 ");
        scanf("%d",&xx2);
        printf("\n Enter vertex y2 ");
        scanf("%d",&yy2);
        glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(0,0);
    glutCreateWindow("DDA Line");
    init();
    glClear(GL_COLOR_BUFFER_BIT);
    glutDisplayFunc(display);
    glFlush();
    glutMainLoop();
    return 0;
}

Output: