Tuesday, July 26, 2011

Bresenham’s Line Drawing Algorithm


#include<graphics.h>
#include<iostream.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
class Line
{
      public:
      int x1,x2,y1,y2;
      int dx,dy,x,y,xend,p;
      int xa,ya,xb,yb;
      void input();
      void draw(int,int, int,int);
};
void Line :: input()
{
      cout<<"Enter the points x1,y1:";
      cin>>x1>>y1;
      cout<<"Enter the points x2,y2:";
      cin>>x2>>yb;
draw(x1,y1,x2,y2);
}
void Line :: draw(xa,ya,xb,yb)
{
      dx=abs(xa-xb);
      dy=abs(ya-yb);
      p=2*(dy-dx);
      if (xa>xb)
      {
                  x=xb;
                  y=yb;
                  xend=xa;
      }
      else
      {
                  x=xa;
                  y=ya;
                  xend=xb;
      }
      putpixel (x,y,1);
     
while (x<xend)
      {
                  x=x+1;
                  if (p<0)
                  {
                              p=p+2*dy;
                  }
                  else
                  {
                              y=y+1;
                              p=p+2*(dy-dx);
                  }
                  putpixel (x,y,1);
                  sleep(1);
                 
      }
}
void main()
{
      int gd=DETECT,gm;
      initgraph(&gd,&gm,"");
      Line b;
      b.input();
      closegraph();
      getch();

}

input:   

Enter the points x1,y1:200
300
Enter the points x2,y2:400
350

Moving a Car in C++


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>'
#include<dos.h>
class Car
{
public:
void design();
};
void Car::design()
{
for(int i=1;i<=448;i++)
{
cleardevice();
arc(100+i,200,0,180,50);
line(120+i,200,120+i,155);
line(121+i,200,121+i,155);
line(80+i,200,80+i,155);
line(79+i,200,79+i,155);
line(50+i,200,150+i,200);
line(10+i,240,35+i,240);
line(190+i,240,165+i,240);
line(65+i,240,135+i,240);
arc(50+i,240,90,180,40);
arc(150+i,240,0,90,40);
circle(50+i,240,5);
circle(50+i,240,15);
circle(150+i,240,5);
circle(150+i,240,15);
delay(5);
}
}
void main()
{
clrscr();
Car obj;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
obj.design();
getch();
}

2D-Shearing


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
class Trans
{
public:
int tx,ty,n,i,j,z;
int a[50],b[50];
float sh;
void get();
void show();
};
void Trans::get()
{
cout<<"\n  Enter the Side of the Polygan:";
cin>>n;
cout<<"\n Enter the "<<2*n<<"  Co-ordinates Value:";
for(i=0;i<2*n;i++)
cin>>a[i];
fillpoly(n,a);
getch();
cout<<"\n\n Enter the Y Reference  Vector:";
cin>> tx;
cout<<"\n\n Enter the X Reference  Vector:";
cin>>ty;
cout<<"\n\n Enter the Shearing Point:";
cin>>sh;
}
void Trans::show()
{
while(1)
{
cout<<"\n\t\t\t1.X Shear";
cout<<"\n\t\t\t2.Y Shear";
cout<<"\n\t\t\t3. Exit";
cout<<"\n Enter your Choice:";
cin>>z;
switch(z)
{
case 1:
cleardevice();
cout<<"\n Before Shear:";
fillpoly(n,a);
getch();
cleardevice();
cout<<"\n\n\n After After";
for(i=0;i<2*n;i+=2)
{
b[i]=a[i]+(sh*(a[i+1]-tx));
b[i+1]=a[i+1];
}
fillpoly(n,b);
break;
case 2:
cleardevice();
cout<<"\n Before Shear:";
fillpoly(n,a);
getch();
cleardevice();
cout<<"\n\n\n After After";
for(i=0;i<2*n;i+=2)
{
b[i]=a[i];
b[i+1]=a[i+1]+(sh*(a[i]-ty));
}

fillpoly(n,b);
break;
case 3:
exit(0);
break;
}
}
}
void main()
{
int v=DETECT,c=0;
initgraph(&v,&c,"");
cleardevice();
Trans w;
int q;
while(1)
{
cout<<"\n\t\t\t\t SHEARING";
cout<<"\n MENU:";
cout<<"\n\n\t\t\t 1.Shearing:";
cout<<"\n\n\t\t\t 2.Exit";

cout<<"\n Enter Your Option:";
cin>>q;
switch(q)
{
case 1:
w.get();
w.show();
break;
case 2:
exit(0);
default:
cout<<"\n\t\t\t Invalid!!!";
break;
}
getch();
}
}