Monday, 24 August 2015

DDA Line drawing program in C++


/* DDA Line drawing program*/
#include<dos.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,length;
int i,gd=DETECT,gm;
//Read two end points of line
cout<<"Enter the value of x1 :\t";
cin>>x1;
cout<<"Enter the value of y1 :\t";
cin>>y1;
cout<<"Enter the value of x2 :\t";
cin>>x2;
cout<<"Enter the value of y2 :\t";
cin>>y2;

/* Initialize graphics mode*/
 initgraph(&gd,&gm,"c://tc//bgi");

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;  /* Factor 0.5 is added to round the values */
y = y1 + 0.5;  /* Factor 0.5 is added to round the values */
i = 1;         /* Initialise loop counter */
while(i <= length)
   {
    putpixel(x,y,WHITE);
    x = x + dx;
    y = y + dy;
    i = i + 1;
    delay(50); /* Delay is purposely inserted to see*/
    }
getch();
}

For OUTPUT check out the video! :







Bresenhams Line Drawing Program (Source Code)

/* Breseham's Line Drawing program*/

#include<graphics.h>
#include<math.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void main()
{
 float x,y,x1,y1,x2,y2,dx,dy,e;
 int i,gd=DETECT,gm;
 clrscr();
/* Read two end points of line */
 cout<<"Enter the value of x1 :\t";
 cin>>x1;
 cout<<"Enter the value of y1 :\t";
 cin>>y1;
 cout<<"Enter the value of x2 :\t";
 cin>>x2;
 cout<<"Enter the value of y2 :\t";
 cin>>y2;
/* Initialise graphics mode*/
 initgraph(&gd,&gm,"c:/tc/bgi");

   dx=abs(x2-x1);
   dy=abs(y2-y1);
/* Initialise starting point
-----------------------------*/
   x = x1;
   y = y1;
/* Initialise decision variable
-------------------------------- */
   e=2*dy-dx;
   i=1;    /* Initialise loop counter */
do
 {
  putpixel(x,y,WHITE);
  delay(60);
  while(e >= 0)
     {
      y=y+1;
      e=e-2*dx;
     }
  x=x+1;
  e=e+2*dy;
  i=i+1;
 }
while(i <= dx);
 getch();
 closegraph();
}

For OUTPUT check out the video! :





Thursday, 20 August 2015

C++ basic graphic shapes (Source Code)

/*Basic Shapes in C++ Graphics mode*/

#include<graphics.h> 
#include<conio.h> 
void main() 

 int gd=DETECT, gm; 
 int poly[12]={350,450,350,410,430,400,350,350,300,
430,350,450}; 

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

 circle(100,100,50);
 outtextxy(74,170,"circle");

 rectangle(200,50,350,150);
 outtextxy(240,170,"Rectangle");

 ellipse(500,100,0,360,100,50);
 outtextxy(480,170,"Ellipse");

 line(100,250,540,250);
 outtextxy(300,260,"Line");

 sector(150,400,30,300,100,50);
 outtextxy(120,460,"Sector");

 drawpoly(6,poly);
 outtextxy(340,460,"Polygon");

getch();
closegraph();
}

For OUTPUT check out the video! :





Tuesday, 18 August 2015

Midpoint Circle Drawing Program (Source Code)

/*Midpoint Circle Drawing Program*/

#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
void main()
{
float  p;
int i,gd,gm,x,y,maxx,maxy;
int r;

/* initialise graphics
------------------------ */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

/* Read the radius
----------------------- */
cout<<"Enter the radius of the circle : ";
cin>>r;

x=0;
y=r;
p = 1.25 - r;
maxx=getmaxx();
maxy=getmaxy();
do
{
putpixel((maxx/2)+x,(maxy/2)+y,5);
putpixel((maxx/2)+y,(maxy/2)+x,6);
putpixel((maxx/2)+x,(maxy/2)-y,7);
putpixel((maxx/2)+y,(maxy/2)-x,8);
putpixel((maxx/2)-x,(maxy/2)-y,9);
putpixel((maxx/2)-x,(maxy/2)+y,10);
putpixel((maxx/2)-y,(maxy/2)+x,11);
putpixel((maxx/2)-y,(maxy/2)-x,12);

if (p < 0)
{
x = x+1;
y = y;
p = p + 2*x + 2;
}
else
{
x= x+1;
y= y-1;
p = p + 2*(x-y) + 1;
}
delay(20);
}
while(x < y);
getch();
closegraph();
}

For OUTPUT check out the video! :



Ps: Please Like and Subscribe--->> https://youtu.be/1xhMs9qko3M

Bresenham's Circle Drawing Program (Source Code)

/*Bresenham's Circle Drawing algorithm*/

#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<iostream.h>
void main()
{
float d;
int gd,gm,x,y,midx,midy;
int r;
clrscr();

/* Read the radius of the circle
---------------------------------- */
cout<<"\tEnter the radius of a circle :";
cin>>r;

/* Initialise graphics mode
------------------------------*/
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
/* Initialise starting points
-------------------------------*/
x = 0;
y = r;
midx=getmaxx()/2;
midy=getmaxy()/2;
/* initialise the decision variable
---------------------------------------*/
d = 3 - 2 * r;
do
{ putpixel(midx+x,midy+y,BLUE);
putpixel(midx+y,midy+x,GREEN);
putpixel(midx+y,midy-x,RED);
putpixel(midx+x,midy-y,BROWN);
putpixel(midx-x,midy-y,LIGHTGRAY);
putpixel(midx-y,midy-x,LIGHTBLUE);
putpixel(midx-y,midy+x,LIGHTGREEN);
putpixel(midx-x,midy+y,LIGHTRED);
if (d <= 0)
{
d = d + 4*x + 6;
}
else
{
d = d + 4*(x-y) + 10;
y = y - 1;
}
x = x + 1;
delay(50);  /* Delay is purposely inserted to see
observe the circle drawing process */
}
while(x < y);
getch();
closegraph();
}


For OUTPUT check out the video! :



Ps: Please Like and Subscribe--->> https://youtu.be/JR4w_aY9dOs