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