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

}

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:

 

How to Install OpenGL in Ubuntu

Prerequisite : Internet Connection

To install OpenGL on system you need to install some basic packages. Open terminal and execute the following commands on terminal.
Step 1: sudo apt-get update 
             To update your basic packages
Step 2: sudo apt-get install build-essential
              For installing essential packages.
Step 3: sudo apt-get install freeglut3 freeglut3-dev
Step 4: sudo apt-get install binutils-gold
Step 5: sudo apt-get install g++ cmake
Step 6: sudo apt-get install mesa-common-dev mesa-utils
Step 7: sudo apt-get install libglew-dev libglew1.5-dev libglm-dev
Step 8: glxinfo | grep OpenGL


Now you are ready to compile your program!
● Write your program using any editor and save.
● Open the terminal for the specified folder and then run with:
gcc MyProg.c -lGL -lGLU -lglut (for C program)
g++ MyProg.cpp -lGL -lGLU -lglut (for C++ program)
./a.out

● Once all this installation process is done, you don't need to follow this process again and again. Just
compile and execute the program.

Monday, 28 March 2016

SEED FILL NON RECURSIVE

#include<stdio.h>
#include<math.h>
#include<graphics.h>
struct stack
{
   long int x;
   long int y;
   struct stack *next;
}*p,*temp,*head=NULL;
typedef struct stack t1;
void push(int ,int);
void pop();
int xf,yf;
void bfillnrc(int,int,int,int);
int main()
{
  int gd=DETECT,gm;
  int xy,yc,rx,r,n,choice,ch,newcolor;
  int i,ax[200],ay[200];
  printf("\n ENter the fill color:");
  scanf("%d",&newcolor);
  printf("\nENter no of sides:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    printf("\n ENter %d CO-Ordinates",i+1);
    scanf("%d%d",&ax[i],&ay[i]);
  }
printf("\n ENter the inner point:");
scanf("%d%d",&xf,&yf);
initgraph(&gd,&gm,"c:/tc/bgi");
for(i=0;i<n;i++)
 {
   line(ax[i],ay[i],ax[(i+1)%n],ay[(i+1)%n]);//delay
  }
bfillnrc(xf,yf,newcolor,15);
getch();
return 0;
}
void bfillnrc(int x,int y,int newcolor,int bdry)
{
  if(getpixel(x,y)==bdry)
{
   return;
}
  push(x,y);
  putpixel(x,y,newcolor);
  while(head!=NULL)
{
  delay(1);
  pop();
  x=p->x;
  y=p->y;
if((getpixel(x+1,y)!=bdry)&&(getpixel(x+1,y)!=newcolor))
 {
   putpixel(x+1,y,newcolor);
   push(x+1,y);
}
 if((getpixel(x-1,y)!=bdry)&&(getpixel(x-1,y)!=newcolor))
  {
    putpixel(x-1,y,newcolor);
    push(x-1,y);
  }
if((getpixel(x,y+1)!=bdry)&&(getpixel(x,y+1)!=newcolor))
  {
    putpixel(x,y+1,newcolor);
    push(x,y+1);
   }
if((getpixel(x,y-1)!=bdry)&&(getpixel(x,y-1)!=newcolor))
  {
    putpixel(x,y-1,newcolor);
    push(x,y-1);
  }
 }
}
void push(int x1,int y1)
{
  temp=(t1*)malloc(sizeof(t1));
  temp->x=x1;
  temp->y=y1;
  if(head==NULL)
{
  head=temp;
  head->next=NULL;
}
else
 {
   temp->next=head;
   head=temp;
}
}
void pop()
{
  p=head;
  head=head->next;
}


Tuesday, 24 March 2015

Simple Bezier curve

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>  
void bezier (int x[4], int y[4])
{
int gd = DETECT, gm,i;
double t;
initgraph (&gd, &gm, "");
for (t = 0.0; t < 1.0; t += 0.0005)
 {
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] + 3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];
double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] + 3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
 setcolor(RED);
 for (i=0; i<3; i++)
  {
    line (x[i], y[i], x[i+1], y[i+1]);
  }
     
 getch();
 closegraph();
 return;
 }  
void main()
{
 int x[4], y[4]; int i;
   printf ("Enter the x- and y-coordinates of the four control points.\n");
 for (i=0; i<4; i++)
  {
      scanf ("%d%d", &x[i], &y[i]);
  }
 bezier (x, y);
}

Monday, 23 March 2015

Bresenham's Circle Drawing algo

# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
# include<dos.h>

void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc,yc;

initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();


printf("Enter the radius ");
scanf("%d",&r);
printf("\n Enter the centre of circle");
scanf("%d%d",&xc,&yc);
line(320,0,320,480);
line(0,240,640,240);

x=0;
y=r;
putpixel(xc+x,yc-y,1);

p=3-(2*r);

for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;

p=p+((4*(x-y)+10));
}

putpixel(320+xc+x,240-yc-y,1);
putpixel(320+xc-x,240-yc-y,2);
putpixel(320+xc+x,240-yc+y,3);
putpixel(320+xc-x,240-yc+y,4);
putpixel(320+xc+y,240-yc-x,5);
putpixel(320+xc-y,240-yc-x,6);
putpixel(320+xc+y,240-yc+x,7);
putpixel(320+xc-y,240-yc+x,8);
sleep(1);

}
getch();
closegraph();
}

Tuesday, 17 March 2015

How to install Libgraph on UBUNTU

●This project is not part of the GNU Project. libgraph is an implementation of the TurboC graphics API (graphics.h) on GNU/Linux using SDL. The library requires SDL for primitive graphics and SDL_image (to blit fonts). Functions for text display are based heavily on code "borrowed" from Karl Bartel's SFont library.
● The library is not very powerful or flexible. It is probably not suitable for use in production-quality applications. I see it more as a simple, easy-to-use 2D graphics interface - could be used for simple prototyping, visualization or studying graphics algorithms. It is simplified library to run graphics programs in C.
It works efficiently with open source C compiler gcc. It is required to add the sdl-libgraph library to current existing gcc
Prerequisite: Internet Connection

You need to install some basic packages. Open terminal and exicute the following commands on terminal.
1) sudo apt-get update 
                 To update your basic packages
2) sudo apt-get install build-essential
                 For installing essential packages.
3) sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev
                 For installing lib packages
4) Now, download libgraph from: 
(http://download.savannah.gnu.org/releases/libgraph/libgraph-1.0.2.tar.gz). Then copy the file libgraph-1.0.2.tar.gz to our home folder. Right click on the file and select Extract here.
5) Open a terminal and run the following commands, one by one.
cd libgraph-1.0.2
./configure
sudo make
sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib+

Now you are ready to compile your program!
● Write your program using any editor and save.
● Open the terminal for the specified folder and then run with:
gcc MyProg.c -lgraph (for C program)
g++ MyProg.cpp -lgraph (for C++ program)
./a.out
● Once all this installation process is done, you don't need to follow this process again and again. Just
compile and execute the program. 

Monday, 16 March 2015

Polygon Filling with Non recursive

#include<stdio.h>
#include<graphics.h>
struct stack
{
 long int x;
 long int y;
 struct stack *next;
}*p,*temp,*head=NULL;

typedef struct stack t1;
void push(int,int);
void pop();
int xf,yf;
void fill4nonrec(int,int,int,int);
int main()
{
 int gd=DETECT,gm,xc,yc,rx,ry,r,n,choice,ch,newcolor;
 int i,ax[200],ay[200];
 printf("\nEnter the fill color ");
 scanf("%d",&newcolor);
 printf("\nEnter the no of sides ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
     printf("\nEnter the %d co=ordinates ",i+1);
     scanf("%d%d",&ax[i],&ay[i]);
    }
    printf("\nEnter the inner point ");
    scanf("%d%d",&xf,&yf);
    initgraph(&gd,&gm,"");
    for(i=0;i<n;i++)
     line(ax[i],ay[i],ax[(i+1)%n],ay[(i+1)%n]);
fill4nonrec(xf,yf,newcolor,15);
getch();
return 0;
}

void fill4nonrec(int x,int y,int new1,int bdry)
{
 if(getpixel(x,y)==bdry)
 return;
 push(x,y);
 putpixel(x,y,new1);
 while(head!=NULL)
 {
  pop();
  x=p->x;
  y=p->y;
  if((getpixel(x+1,y)!=bdry)&&(getpixel(x+1,y)!=new1))
  {
   putpixel(x+1,y,new1);
   push(x+1,y);
  }
  if((getpixel(x-1,y)!=bdry)&&(getpixel(x-1,y)!=new1))
  {
   putpixel(x-1,y,new1);
   push(x-1,y);
  }
  if((getpixel(x,y+1)!=bdry)&&(getpixel(x,y+1)!=new1))
  {
   putpixel(x,y+1,new1);
   push(x,y+1);
  }
  if((getpixel(x,y-1)!=bdry)&&(getpixel(x,y-1)!=new1))
  {
   putpixel(x,y-1,new1);
   push(x,y-1);
  }
 }
}
void push(int x1,int y1)
{
 temp=(t1 *)malloc(sizeof(t1));
 temp->x=x1;
 temp->y=y1;
 if(head==NULL)
 {
  head=temp;
  temp->next=NULL;
 }
 else
 {
  temp->next=head;
  head=temp;
 }
}
void pop()
{
 p=head;
 head=head->next;
}

Reflection about X-axis, Y-axis, XY-axis

#include<graphics.h>
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int gd=DETECT,gm,ch;
int a,b[20],i,c[20],d[20],e[20];
void reflx();
void refly();
void reflxy();
void quad();

int main()
{
cout<<"enter no points of poly";
cin>>a;
cout<<"\n enter no of cordinates of polygon";
for (i=0;i<a*2;i++)
{
cin>>b[i];
if (i%2==0)
c[i]=b[i]+320;
else
c[i]=240-b[i];
}
c[i]=c[0];
c[i+1]=c[1];
cout<<"\n please enter choice 1:Reflection about X 2:Reflection about Y 3:Reflection about X and Y ";
cin>>ch;

switch(ch)
{
case 1:
quad();
reflx();
break;
case 2:
quad();
refly();
break;

case 3:
quad();
reflxy();
break;
}
getch();
closegraph();
return 0;
}

void quad()
{
initgraph(&gd,&gm,NULL);
line(0,240,640,240);
line(320,0,320,480);
drawpoly(a+1,c);
}
void reflx()
{
 for (i=0;i<a*2;i++)
{
if (i%2==0)
c[i]=b[i]+320;
else
c[i]=240+b[i];
}
c[2*a]=c[0];
c[2*a+1]=c[1];
drawpoly(a+1,c);
}

void refly()
{
 for (i=0;i<a*2;i++)
{
if (i%2==0)
d[i]=(b[i]*-1)+320;
else
d[i]=240-b[i];
}
d[2*a]=d[0];
d[2*a+1]=d[1];
drawpoly(a+1,d);
 getch();
}

void reflxy()
{
 setcolor(3);
 for(i=0;i<a*2;i++)
{
e[i]=320-b[i];
i++;
e[i]=240+b[i];
}
e[2*a]=e[0];
e[2*a+1]=e[1];
drawpoly(a+1,e);
}

Friday, 27 February 2015

Sutherland Hodgman Polygon Clipping

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
accept_poly(int p[10][2])
{
int i,v;
 printf("\nEnter no. of vertices in a polygon:");
 scanf("%d",&v);
 for(i=0;i<v;i++)
 {
   printf("\n Enter the %d co-ordinate(x%d,y%d): -->",i+1,i+1,i+1);
   scanf("%d%d",&p[i][0],&p[i][1]);
 }
 return(v);
}

void draw_poly(int p[10][2],int v)
{
int i;
setcolor(BLUE);
for(i=0;i<v;i++)
 {
   line(p[i][0],p[i][1],p[(i+1)%v][0],p[(i+1)%v][1]);
  }
}

int leftclip(int p[10][2],int v,int wxmin)
{
int i,t[10][2];
int k=0;
int x1,y1,x2,y2;
for(i=0;i<v;i++)
{
if(p[i][0] >= wxmin)
{
t[k][0]=p[i][0];
t[k][1]=p[i][1];
k++;
}
if((p[i][0] > wxmin && p[(i+1)%v][0] < wxmin) || (p[i][0] < wxmin && p[(i+1)%v][0] > wxmin))
{
x1=p[i][0];
y1=p[i][1];
x2=p[(i+1)%v][0];
y2=p[(i+1)%v][1];
t[k][1] = y1 + (wxmin-x1) * ((float)(y2-y1)/(x2-x1));
t[k][0] = wxmin;
k++;
}
}
for(i=0;i<k;i++)
{
p[i][0] = t[i][0];
p[i][1] = t[i][1];
}
return(k);
}

int rightclip(int p[10][2],int v,int wxmax)
{
int i,t[10][2];
int k=0;
int x1,y1,x2,y2;
for(i=0;i<v;i++)
{
if(p[i][0] <= wxmax)
{
t[k][0]=p[i][0];
t[k][1]=p[i][1];
k++;
}
if((p[i][0] > wxmax && p[(i+1)%v][0] < wxmax) || (p[i][0] < wxmax && p[(i+1)%v][0] > wxmax))
{
x1=p[i][0];
y1=p[i][1];
x2=p[(i+1)%v][0];
y2=p[(i+1)%v][1];
t[k][1] = y1 + (wxmax-x1) * ((float)(y2-y1)/(x2-x1));
t[k][0] = wxmax;
k++;
}
}
for(i=0;i<k;i++)
{
p[i][0] = t[i][0];
p[i][1] = t[i][1];
}
return(k);
}

int topclip(int p[10][2],int v,int wymin)
{
int i,t[10][2];
int k=0;
int x1,y1,x2,y2;
for(i=0;i<v;i++)
{
if(p[i][1] >= wymin)
{
t[k][0]=p[i][0];
t[k][1]=p[i][1];
k++;
}
if((p[i][1] > wymin && p[(i+1)%v][1] < wymin) || (p[i][1] < wymin && p[(i+1)%v][1] > wymin))
{
x1=p[i][0];
y1=p[i][1];
x2=p[(i+1)%v][0];
y2=p[(i+1)%v][1];
t[k][0] = x1 + (wymin-y1) * ((float)(x2-x1)/(y2-y1));
t[k][1] = wymin;
k++;
}
}
for(i=0;i<k;i++)
{
p[i][0] = t[i][0];
p[i][1] = t[i][1];
}
return(k);
}

int bottomclip(int p[10][2],int v,int wymax)
{
int i,t[10][2];
int k=0;
int x1,y1,x2,y2;
for(i=0;i<v;i++)
{
if(p[i][1] <= wymax)
{
t[k][0]=p[i][0];
t[k][1]=p[i][1];
k++;
}
if((p[i][1] > wymax && p[(i+1)%v][1] < wymax) || (p[i][1] < wymax && p[(i+1)%v][1] > wymax))
{
x1=p[i][0];
y1=p[i][1];
x2=p[(i+1)%v][0];
y2=p[(i+1)%v][1];
t[k][0] = x1 + (wymax-y1) * ((float)(x2-x1)/(y2-y1));
t[k][1] = wymax;
k++;
}
}
for(i=0;i<k;i++)
{
p[i][0] = t[i][0];
p[i][1] = t[i][1];
}
return(k);
}

int main(void)
{
   int gd = DETECT, gm, errorcode;
   int v,wxmin,wymin,wxmax,wymax,p[10][2];
   printf("\nEnter the window co-ordinates:");
   printf("\nWxmin ==> ");
  scanf("%d",&wxmin);
   printf("\nWymin ==> ");
   scanf("%d",&wymin);
   printf("\nWxmax ==> ");
   scanf("%d",&wxmax);
   printf("\nWymax ==> ");
   scanf("%d",&wymax);
   v=accept_poly(p);
   initgraph(&gd, &gm, NULL);
   setcolor(RED);
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   v=leftclip(p,v,wxmin);
   cleardevice();
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   v=rightclip(p,v,wxmax);
   cleardevice();
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   v=topclip(p,v,wymin);
   cleardevice();
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   v=bottomclip(p,v,wymax);
   cleardevice();
   rectangle(wxmin,wymin,wxmax,wymax);
   draw_poly(p,v);
   getch();
   closegraph();
   return 0;
}