Sunday, 17 January 2016

Cohen-Sutherland Line Clipping Algorithm Using C++

// Program to implement Cohen-Sutherland Line Clipping Algorithm Using C++.
#include<iostream>

#include<graphics.h>
typedef unsigned int outcode;
enum{TOP=0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8};

using namespace std;

outcode CompOutCode(double ,double ,double ,double ,double ,double );
void CSLCAD(double x0,double y0,double x1,double y1,double xmin,double xmax,double ymin,double ymax)
{

    outcode outcode0,outcode1,outcodeout;
    boolean accept=FALSE, done=FALSE;
    outcode0=CompOutCode(x0,y0,xmin,xmax,ymin,ymax);
    outcode1=CompOutCode(x1,y1,xmin,xmax,ymin,ymax);
    cout<<"outcode0="<<outcode0<<endl;
    cout<<"outcode1="<<outcode1<<endl;
    do
    {
        if(outcode0==0 && outcode1==0)
        {
            accept=TRUE;
            done=TRUE;
        }
        else if(outcode0 & outcode1)
            {
                done=TRUE;
            }
            else
            {
                double x,y;
                int ocd=outcode0 ? outcode0:outcode1;
                if(ocd & TOP)
                {
                    x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
                    y=ymax;
                }
                else if(ocd & BOTTOM)
                {
                    x=x0+(x1-x0)*(ymin-y0)/(y1-y0);
                    y=ymin;
                }
                else if(ocd & LEFT)
                {
                    y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
                    x=xmin;
                }
                else
                {
                    y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
                    x=xmax;
                }
                if(ocd==outcode0)
                {
                    x0=x;
                    y0=y;
                    outcode0=CompOutCode(x0,y0,xmin,xmax,ymin,ymax);
                }
                else
                {
                    x1=x;
                    y1=y;
                    outcode1=CompOutCode(x1,y1,xmin,xmax,ymin,ymax);
                }
            }

    }while(done==FALSE);

    if(accept==TRUE)
    {
        line(x0,y0,x1,y1);
    }
}

outcode CompOutCode(double x,double y,double xmin,double xmax,double ymin,double ymax)
{
    outcode code=0;
    if(y>ymax)
        code|=TOP;
    if(y<ymin)
        code|=BOTTOM;
    if(x>xmax)
        code|=RIGHT;
    if(x<xmin)
        code|=LEFT;
    return code;
}

int main()
{
    string ch;
    double xmin,xmax,ymin,ymax,x0,y0,x1,y1;

    initwindow(500,600);

    cout<<"Enter the bottom co-ordinates of window:";
 cin>>xmin;
 cout<<"Enter the left coordinates of the window:";
 cin>>ymin;
 cout<<"Enter the right coordinates of the window:";
 cin>>xmax;
 cout<<"Enter the top coordinates of the window:";
 cin>>ymax;
 rectangle(xmin,ymin,xmax,ymax);

 cout<<"Enter the coordinates(Terminal Points) of the line: ";
 cin>>x0>>y0;
 cin>>x1>>y1;
 line(x0,y0,x1,y1);

    delay(5000);
    cleardevice();
    CSLCAD(x0,y0,x1,y1,xmin,xmax,ymin,ymax);
    rectangle(xmin,ymin,xmax,ymax);
    delay(50000);

    closegraph();
}


// Output of the above program

Cohen-Sutherland Line Clipping Algorithm Using C++
Cohen-Sutherland Line Clipping Algorithm Using C++



Cohen-Sutherland Line Clipping Algorithm Using C++
Cohen-Sutherland Line Clipping Algorithm Using C++


Cohen-Sutherland Line Clipping Algorithm Using C++
Cohen-Sutherland Line Clipping Algorithm Using C++



Checkout this video on Cohen-Sutherland Line Clipping Algorithm Using C++ in CodeBlocks 13.12...


Thursday, 7 January 2016

Mid-Point Circle Drawing Algorithm Using C++

// Program to implement mid-point Circle Drawing Algorithm Using C++.
#include<iostream>

#include<graphics.h>
using namespace std;

void circle(int ,int ,int ,int );
void circlepoints(int ,int ,int ,int ,int );


void circle(int x0,int y0,int r,int xc)
{
 int x=0;
 int y=r;
 double d=1-r;
 initwindow(640,480);
 circlepoints(x0,x,y0,y,xc);
 cout<<"Value after iteration is::\n";

 while(y>x)
 {
  if(d<0)
  {
   d+=2*x+3;
   cout<<"Value of d is:"<<d<<"\n";
  }
  else
  {
   d+=2*(x-y)+5;
   cout<<"Value of d is:"<<d<<"\n";
   y--;
  }
  x++;
  circlepoints(x0,x,y0,y,xc);
  cout<<x<<" "<<y<<"\n"<<"\n";
 }
 delay(50000);
 closegraph();
}

void circlepoints(int x0,int x,int y0,int y,int xc)
{
 putpixel((x0+x),(y0+y), xc);
 putpixel((x0+x),(y0-y), xc);
 putpixel((x0-x),(y0-y), xc);
 putpixel((x0-x),(y0+y), xc);
 putpixel((x0+y),(y0+x), xc);
 putpixel((x0+y),(y0-x), xc);
 putpixel((x0-y),(y0+x), xc);
 putpixel((x0-y),(y0-x), xc);
}


int main()
{
 int m[16]={BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE};
 int x0,r,y0;
 int xc;

 cout<<"Enter the value of x0\n";
 cin>>x0;

 cout<<"Enter the value of y0\n";
 cin>>y0;

 cout<<"Enter the value of r\n";
 cin>>r;

 cout<<"Enter the color\n";
 for(int i=0;i<16;i++)
 {
       cin>>m[i];
       xc=m[i];
       break;
 }
 circle(x0,y0,r,xc);
 closegraph();
 return 0;
}

// Output of the above program

Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++



Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++

Mid-Point Circle Drawing Algorithm Using C++
Mid-Point Circle Drawing Algorithm Using C++


Checkout this video on mid-point Circle Drawing Algorithm Using C++ in CodeBlocks 13.12...




Friday, 1 January 2016

Bresenham Line Algorithm Using C++

// C++ Program to implement Bresenham Line Algorithm
#include<iostream>

#include<graphics.h>

using namespace std;

int main()
{
    initwindow(500,600);
    int x0,y0,x1,y1;
    cout<<"Enter the First Line Coordinates";
    cin>>x0>>y0;
    cout<<"Enter the Second Line Coordinates";
    cin>>x1>>y1;

    int dx=x1-x0;
    int dy=y1-y0;
    int d=2*dy-dx;
    int incrE=2*dy;
    int incrNE=2*(dy-dx);
    int x=x0;
    int y=y0;
    putpixel(x,y,5);

    while(x<x1)
    {
        if(d<=0)
        {
            d+=incrE;
            x++;
        }
        else
        {
            d+=incrNE;
            x++;
            y++;
        }
        putpixel(x,y,5);
    }
    delay(50000);
    closegraph();
    return 0;
}

// Output of the above program
Bresenham Line Algorithm
Bresenham Line Algorithm using C++
Bresenham Line Algorithm
Bresenham Line Algorithm using C++
Checkout this video on Bresenham Line Drawing Algorithm Using C++ in CodeBlocks 13.12...