Sunday, 19 April 2020

How to install Python3.8 on Uuntu 19.10 or 20.04

Python is one of the most popular programming languages in the world. With its simple and easy to learn syntax, Python is a great choice for beginners and experienced developers. Python is quite a versatile programming language. It can be used as a scripting language, to build games, develop websites, create machine learning algorithms, and analyze data.
Python 3.7 includes many new features such as postponed evaluation of type annotations, support for data classes and context variables, customization of access to module attributes, and more.
This tutorial describes two ways of installing Python 3.8 on Ubuntu 19.10: By using the standard apt tool from the deadsnakes PPA, and by building from the source code.


Installing Python 3.8 on Ubuntu with Apt

Installing Python 3.8 on Ubuntu with apt is a relatively straightforward process and will only take a few minutes:
  1. Start by updating the packages list and installing the prerequisites:

    sudo apt updatesudo apt install software-properties-common
     
  2. Next, add the deadsnakes PPA to your sources list:
     
    sudo add-apt-repository ppa:deadsnakes/ppa

    When prompted press Enter to continue:
     
    Press [ENTER] to continue or Ctrl-c to cancel adding it.
     

  3. Once the repository is enabled, install Python 3.8 with:
     
    sudo apt install python3.8
     
  4. At this point, Python 3.8 is installed on your Ubuntu system and ready to be used. You can verify it by typing:
     
    python3.8 --version
     
    Output: Python 3.8.0


Friday, 20 March 2020

Generate fractal patterns by using Koch curves.

#include <GL/glut.h>

#include <math.h>

GLfloat oldx=-0.7,oldy=0.5;

void drawkoch(GLfloat dir,GLfloat len,GLint iter)
{

GLdouble dirRad = 0.0174533 * dir ; 

GLfloat newX = oldx + len * cos(dirRad);

GLfloat newY = oldy + len * sin(dirRad);

if (iter==0)
         {

glVertex2f(oldx, oldy);

glVertex2f(newX, newY);

oldx = newX;

oldy = newY;

      }

else
    {

iter--;
//draw the four parts of the side _/\_

              drawkoch(dir, len, iter);

             dir += 60.0;

            drawkoch(dir, len, iter);

           dir -= 120.0;

          drawkoch(dir, len, iter);

         dir += 60.0;

 drawkoch(dir, len, iter);

}

}

void display()

{

 glClearColor(1.0,1.0,1.0,0); 

         glColor3f(0.0, 0.0, 0.0);     
       
 glClear( GL_COLOR_BUFFER_BIT );

 glBegin(GL_LINES);


 /*
drawkoch(0.0,0.5,1);

      drawkoch(-120.0, 0.5, 1);

  drawkoch(120.0,0.5,1);

 *//*

 drawkoch(0.0,0.15,2);

 drawkoch(-120.0, 0.15, 2);

drawkoch(120.0,0.15,2);
*/


 drawkoch(0.0,0.05,3);

 drawkoch(-120.0, 0.05, 3);

 drawkoch(120.0,0.05,3);
 

 glEnd();

 glFlush();

}


int main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);     

glutInitWindowSize(500,500);   

glutInitWindowPosition(0,0);

glutCreateWindow("Koch Curves");   

glutDisplayFunc(display); 

glutMainLoop();


}

Implement Cube rotation about vertical axis passing through its centroid


#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define  RADDEG  57.29577951f

float XUP[3] = {1,0,0},
   
 YUP[3] = {0,1,0},
   
 ZUP[3] = {0,0,1},
 
 ORG[3] = {0,0,0};

GLfloat viewangle = 0, tippangle = 0;

GLfloat d[3] = {0.1, 0.1, 0.1};

GLfloat  xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
//  Use arrow keys to rotate entire scene

void Special_Keys (int key, int x, int y)

{
   
switch (key)
 {

  case GLUT_KEY_LEFT :  viewangle -= 5;  break;
     
 case GLUT_KEY_RIGHT:  viewangle += 5;  break;
     
 case GLUT_KEY_UP   :  tippangle -= 5;  break;
   
  case GLUT_KEY_DOWN :  tippangle += 5;  break;

     
 default: printf ("   Special key %c == %d\n", key, key);
 
  }

 
  glutPostRedisplay();

}



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

{
 
 switch (key)
{

  case 'b' : d[0] += 0.1;  break; //x
     
 case 'n' : d[1] += 0.1;  break; //y
     
  case 'm' : d[2] += 0.1;  break; //z
     
     
 case 'x' : xAngle += 5;  break;
     
 case 'y' : yAngle += 5;  break;
     
 case 'z' : zAngle += 5;  break;

     
 default: printf ("   Keyboard %c == %d\n", key, key);
 
  }

 
 glutPostRedisplay();

}




void Triad (void)

{
 
 glColor3f (1.0, 1.0, 1.0);

 
 glBegin (GL_LINES);
   
 glVertex3fv (ORG);
 glVertex3fv (XUP); //x-axix
     
 glVertex3fv (ORG);
 glVertex3fv (YUP); //y-axix
     
 glVertex3fv (ORG);
 glVertex3fv (ZUP); //z-axix
 
 glEnd ();

 
  glRasterPos3f (1.1, 0.0, 0.0);
 
  glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'X');

 
 glRasterPos3f (0.0, 1.1, 0.0);
 
 glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Y');

 
 glRasterPos3f (0.0, 0.0, 1.1);
 
 glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Z');
}



void Draw_Box (void)

{
   
glBegin (GL_QUADS);

 
 glColor3f  ( 0.0,  0.7, 0.1);     // Front - green
   
 glVertex3f (-1.0,  1.0, 1.0);
   
  glVertex3f ( 1.0,  1.0, 1.0);
   
 glVertex3f ( 1.0, -1.0, 1.0);
   
 glVertex3f (-1.0, -1.0, 1.0);


     glColor3f  ( 0.9,  1.0,  0.0);    // Back  - yellow
 
   glVertex3f (-1.0,  1.0, -1.0);
 
   glVertex3f ( 1.0,  1.0, -1.0);
   
  glVertex3f ( 1.0, -1.0, -1.0);
 
   glVertex3f (-1.0, -1.0, -1.0);

   
 glColor3f  ( 0.2, 0.2,  1.0);     // Top - blue
   
 glVertex3f (-1.0, 1.0,  1.0);
     
glVertex3f ( 1.0, 1.0,  1.0);
   
 glVertex3f ( 1.0, 1.0, -1.0);
   
 glVertex3f (-1.0, 1.0, -1.0);

   
  glColor3f  ( 0.7,  0.0,  0.1);    // Bottom - red
     
glVertex3f (-1.0, -1.0,  1.0);
     
glVertex3f ( 1.0, -1.0,  1.0);
     
glVertex3f ( 1.0, -1.0, -1.0);
 
   glVertex3f (-1.0, -1.0, -1.0);

 
 glEnd();

}



void redraw (void)
{
    int v;

 
  glClear  (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 glEnable (GL_DEPTH_TEST);

 
  glLoadIdentity ();

 
 glTranslatef (0, 0, -3);
 
 glRotatef (tippangle, 1,0,0);  // Up and down arrow keys 'tip' view.
 
 glRotatef (viewangle, 0,1,0);  // Right/left arrow keys 'turn' view.

   // glDisable (GL_LIGHTING);

 
  Triad ();

 
 // glPushMatrix ();
   
   glTranslatef (d[0], d[1], d[2]); 
  // Move box down X axis.
     
glScalef (0.2, 0.2, 0.2);
     
 glRotatef (zAngle, 0,0,1);
   
   glRotatef (yAngle, 0,1,0);
 
    glRotatef (xAngle, 1,0,0);
     
Draw_Box ();
 
 //glPopMatrix ();

 
 glutSwapBuffers();

}



int main (int argc, char **argv)

{
 
 glutInit(&argc, argv);
 
 glutInitWindowSize (900, 600);
 
 glutInitWindowPosition (300, 300);

    glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);

 
 glutCreateWindow ("3D Cube Rotation(press arrows and x y z keys)");
 
  glutDisplayFunc( redraw );
 
 glutKeyboardFunc (  Keyboard  );
 
  glutSpecialFunc  (Special_Keys);


   //glClearColor (0.1, 0.0, 0.1, 1.0);

 
 glMatrixMode   (GL_PROJECTION);
 
  gluPerspective (60, 1.5, 1, 10);
 
 glMatrixMode   (GL_MODELVIEW);

   glutMainLoop ();


    return 1;

}
 

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();

}