Monday, 29 December 2014

TRANSLATION

Program for Translation

#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void draw2d(int,int [],int [],int,int);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],tx=0,ty=0,i,fs;
initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
printf("No of sides : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
draw2d(fs,x,y,tx,ty);
printf("translation (x,y) : ");
scanf("%d%d",&tx,&ty);
draw2d(fs,x,y,tx,ty);
getch();
}
void draw2d(int fs,int x[20],int y[20],int tx,int ty)
{
int i;
for(i=0;i<fs;i++)
{
if(i!=(fs-1))
line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty);
else
line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty);
}
}
OUTPUT:



SHEARING

Program for Shearing

#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void draw2d(int,int [],int [],int,int);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],x1[20],y1[20],tx=0,ty=0,i,fs,a;
initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
printf("No of sides : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
draw2d(fs,x,y,tx,ty);
printf("translation (x,y) : ");
scanf("%d%d",&tx,&ty);
while(1)
{
clrscr();
cleardevice();
printf("1. X Shear\n\n2. Y Shear\n\n3. Exit\n\n");
draw2d(fs,x,y,0,0);
switch(getche())
{
case '1':
printf("X Shear Value : ");
scanf("%d",&a);
for(i=0;i<fs;i++)
{
x1[i]=x[i]+(a*(y[i]-ty));
y1[i]=y[i];
}
break;
case '2':
printf("Y Shear Value : ");
scanf("%d",&a);
for(i=0;i<fs;i++)
{
x1[i]=y[i];
y1[i]=y[i]+(a*(x[i]-tx));
}
break;
case '3':
closegraph();
exit(0);
}
draw2d(fs,x1,y1,tx,ty);
getch();
}
}
void draw2d(int fs,int x[20],int y[20],int tx,int ty)
{
int i;
for(i=0;i<fs;i++)
{
if(i!=(fs-1))
line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty);
else
line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty);
}
}
OUTPUT:




SCALING

Program for Scaling

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

int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();

void main()
{
   int gd=DETECT,gm;
   int c;
   initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
   printf("Enter the 1st point for the triangle:");
   scanf("%d%d",&x1,&y1);
   printf("Enter the 2nd point for the triangle:");
   scanf("%d%d",&x2,&y2);
   printf("Enter the 3rd point for the triangle:");
   scanf("%d%d",&x3,&y3);
   draw();
   scale();
}

void draw()
{
   line(x1,y1,x2,y2);
   line(x2,y2,x3,y3);
   line(x3,y3,x1,y1);
}
void scale()
{
   int x,y,a1,a2,a3,b1,b2,b3;
   int mx,my;
   printf("Enter the scalling coordinates");
   scanf("%d%d",&x,&y);
   mx=(x1+x2+x3)/3;
   my=(y1+y2+y3)/3;
   cleardevice();
   a1=mx+(x1-mx)*x;
   b1=my+(y1-my)*y;
   a2=mx+(x2-mx)*x;
    b2=my+(y2-my)*y;
    a3=mx+(x3-mx)*x;
   b3=my+(y3-my)*y;
   line(a1,b1,a2,b2);
   line(a2,b2,a3,b3);
   line(a3,b3,a1,b1);
   draw();
   getch();
}
OUTPUT:



ROTATION

Program for Rotation

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

void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);

void main()
{
    int gd=DETECT,gm;
    int x1,y1,x2,y2,x3,y3;
    initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
    printf("Enter the 1st point for the triangle:");
    scanf("%d%d",&x1,&y1);
    printf("Enter the 2nd point for the triangle:");
    scanf("%d%d",&x2,&y2);
    printf("Enter the 3rd point for the triangle:");
    scanf("%d%d",&x3,&y3);
    TriAngle(x1,y1,x2,y2,x3,y3);
    getch();
    cleardevice();
    Rotate(x1,y1,x2,y2,x3,y3);
     setcolor(1);
    TriAngle(x1,y1,x2,y2,x3,y3);
    getch();
}

void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3)
{
   line(x1,y1,x2,y2);
   line(x2,y2,x3,y3);
   line(x3,y3,x1,y1);
}

void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
    int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
    float Angle;
    printf("Enter the angle for rotation:");
    scanf("%f",&Angle);
    cleardevice();
    Angle=(Angle*3.14)/180;
    a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
    b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
    a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
    b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
    a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
    b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
     printf("Rotate");
    TriAngle(a1,b1,a2,b2,a3,b3);
}
OUTPUT:



REFLECTION

Program For Reflection

#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void draw2d(int,int [],int [],int,int);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],x1[20],y1[20],tx=0,ty=0,i,fs;
initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
printf("No of sides : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
draw2d(fs,x,y,tx,ty);
printf("translation (x,y) : ");
scanf("%d%d",&tx,&ty);
while(1)
{
clrscr();
cleardevice();
printf("1. X Reflection\n\n2. Y Reflection\n\n3. Exit");
draw2d(fs,x,y,0,0);
switch(getche())
{
case '1':
for(i=0;i<fs;i++)
{
x1[i]=(tx+((x[i]-tx)*cos(M_PI))-((y[i]-ty)*sin(M_PI)));
y1[i]=y[i];
}
break;
case '2':
for(i=0;i<fs;i++)
{
x1[i]=x[i];
y1[i]=(tx+((y[i]-ty)*cos(M_PI))+((x[i]-tx)*sin(M_PI)));
}
break;
case '3':
closegraph();
exit(0);
}
draw2d(fs,x1,y1,tx,ty);
getch();
}
}
void draw2d(int fs,int x[20],int y[20],int tx,int ty)
{
int i;
for(i=0;i<fs;i++)
{
if(i!=(fs-1))
line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty);
else
line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty);
}
}
OUTPUT:



Program to Draw a Circle using Midpoint Algorithm

Program to Draw a Circle using Midpoint Algorithm

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

void circleBres(int xc, int yc, int r);
void drawCircle(int xc, int yc, int x, int y);

void main()
{

int xc, yc, r;
        int gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "c:\\TurboC3\\BGI");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleBres(xc, yc, r);
getch();
}

void circleBres(int xc, int yc, int r)
{

int x = 0, y = r;
int d = 1 - r;
while (x < y)
{
drawCircle(xc, yc, x, y);
if (d >= 0)
      {
x++;
y--;
d=d+2*(x-y)+5;
}
  else
    {
x++;
d = d + 2 * x + 3;
     }
        drawCircle(xc, yc, x, y);
delay(50);
}
}

void drawCircle(int xc, int yc, int x, int y)
{

putpixel(xc+x, yc+y, RED);

putpixel(xc-x, yc+y, RED);

putpixel(xc+x, yc-y, RED);

putpixel(xc-x, yc-y, RED);

putpixel(xc+y, yc+x, RED);

putpixel(xc-y, yc+x, RED);

putpixel(xc+y, yc-x, RED);

putpixel(xc-y, yc-x, RED);
}
OUTPUT:



Digital Differential Analyzer (DDA) Algorithm

Program to draw a line using DDA Algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main()
{
   int x1,x2,dx,p,y1,y2,i=0,dy,xmax,ymax,m;
   float xmid,ymid;
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "c:\\TurboC3\\BGI");

   /* read result of initialization */
   errorcode = graphresult();

   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }
   line(getmaxx()/2,0,getmaxx()/2,getmaxy());
   line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
   xmax= getmaxx()/2;
   ymax= getmaxy()/2;
   printf("mid point %d \t %d", xmax,ymax);
   printf("\n enter the value of x1 and y1:");
   scanf("%d %d",&x1,&y1);
   printf("\n enter the values of x2 and y2:");
   scanf("%d %d",&x2,&y2);
   if (abs(x2-x1)>abs(y2-y1))
m=abs(x2-x1);
   else
m=abs(y2-y1);
   dx=(x2-x1)/m;
   dy=(y2-y1)/m;
   printf("\n value of dx and dy :%d %d",dx,dy);
   putpixel(x1+319.5,-y1+234.5,2);
   for(i=0;i<m;i++)
   {
x1=x1+dx;
y1=y1+dy;
   putpixel(x1+314.5,-y1+239.5,2);
   }
   getch();
   closegraph();
   return 0;
}
OUTPUT:

Program to draw a line using Bresenham's Line Algorithm

Program to draw a line using Bresenham's Line Algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main()
{
   int x1,x2,dx,p,y1,y2,i=0,dy,xmax,ymax;
   float xmid,ymid;
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "c:\\TurboC3\\BGI");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)     /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }
   line(getmaxx()/2,0,getmaxx()/2,getmaxy());
   line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
   xmax= getmaxx()/2;
   ymax= getmaxy()/2;
   printf("mid point %d \t %d", xmax,ymax);
   printf("\n enter the value of x1 and y1:");
   scanf("%d %d",&x1,&y1);
   printf("\n enter the values of x2 and y2:");
   scanf("%d %d",&x2,&y2);
   dx=x2-x1;
   dy=y2-y1;
   printf("\n value of dx and dy :%d %d",dx,dy);
   p=(2*dy)-dx;
   printf("\n value of p=%d",p);
   putpixel(x1+319.5,-y1+234.5,2);
   for(i=0;i<dx;i++)
   {
if(p<0)
{
x1=x1+1;
y1=y1;
p=p+2*dy;
       }
   else
   {
y1=y1+1;
x1=x1+1;
p=p+(2*dy)-(2*dx);
   }
   putpixel(x1+314.5,-y1+239.5,2);
   }
   getch();
   closegraph();
   return 0;
}

OUTPUT:





Program to Draw a Circle using Bresenham's Algorithm

Program to Draw a Circle using Bresenham's Algorithm

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

void circleBres(int xc, int yc, int r);
void drawCircle(int xc, int yc, int x, int y);
void main()
{

int xc, yc, r;
        int gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm,"c:\\TurboC3\\BGI");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleBres(xc, yc, r);
getch();

}

void circleBres(int xc, int yc, int r)
{

int x = 0, y = r;
int d = 3 - 2 * r;
while (x < y)
{
drawCircle(xc, yc, x, y);
if (d >= 0)
      {
x++;
y--;
d=d+4*(x-y)+10;
}
else
{
x++;
d = d + 4 * x + 6;
}

drawCircle(xc, yc, x, y);
delay(50);
}
}

void drawCircle(int xc, int yc, int x, int y)
{

putpixel(xc+x, yc+y, RED);

putpixel(xc-x, yc+y, RED);

putpixel(xc+x, yc-y, RED);

putpixel(xc-x, yc-y, RED);

putpixel(xc+y, yc+x, RED);

putpixel(xc-y, yc+x, RED);

putpixel(xc+y, yc-x, RED);

putpixel(xc-y, yc-x, RED);
}

OUTPUT:



Monday, 22 December 2014

C++ PROGRAM: implement the following class hierarchy and implement the methods to input dimensions of shape and also calculate area of each shape.


SOLUTION:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class shape
{
public:
int a;
};
class two_dimensional:public shape
{
public:
int d1,d2;
};
class three_dimensional:public shape
{
public:
int d1,d2;
};
class triangle:public two_dimensional
{
public:
void area()
{
cout<<"\n\nEnter the Triangle dimensions (Base,Vertical Height):";
cin>>d1>>d2;
a=0.5*(d1*d2);
cout<<"The area of Triangle is="<<a;
}
};
class rectangle:public two_dimensional
{
public:
void area()
{
cout<<"\n\n Enter the Rectangle dimenssions (Width,Height):";
cin>>d1>>d2;
a=d1*d2;
cout<<"The area of Rectangle is="<<a;
}
};

class box:public three_dimensional
{
public:
void area()
{
cout<<"\n\nEnter the Box dimenssion of any side:";
cin>>d1;
a=d1*d1*d1;
cout<<"The area of Box is="<<a;
}
};

class cone:public three_dimensional
{
public:
void area()
{
cout<<"\n\nEnter the dimenssions of Cone (Radius,Height):";
cin>>d1>>d2;
a=M_PI*d1*(d1+sqrt(d1*d1+d2*d2));
cout<<"The area of the Cone is="<<a;
}
};

class sphere:public three_dimensional
{
public:
void area()
{
cout<<"\n\nEnter the dimensssion of Sphere (Radius):";
cin>>d1;
a=4*M_PI*d1*d1;
cout<<"The area of Sphere is="<<a;
}
};

void main()
{
int ch;
triangle t;
rectangle r;
box b;
cone c;
sphere s;
clrscr();
cout<<"Press:\n1.Area of Triangle\n2.Area of Rectangle\n3.Area of Box\n4.Area of Cone\n5.Area of Sphere";
cout<<"\n\nEnter your choice:";
cin>>ch;
switch (ch)
{
case 1:
t.area();
break;
case 2:
r.area();
break;
case 3:
b.area();
break;
case 4:
c.area();
break;
case 5:
s.area();
break;
default:
cout<<"\nYou didnot enter the correct choice.";
break;
}
getch();
}
OUTPUT:




C++ PROGRAM: Write a program that inputs data about few students and professors and displays whether student or processor is outstanding or not using one for loop.

Write a program that inputs data about few students and professors and displays whether student or processor is outstanding or not using one for loop.    

SOLUTION:
#include<iostream.h>
#include<conio.h>
class person
{
public:
char first_name[20],last_name[20];
int grade[3],i;
void gradefun();
};
void person::gradefun()
{
for (int i=0;i<3;i++)
{
if (grade[i]>=90)
cout<<"\nGrade is outsatndaing for the"<<"\t"<<first_name[i]<<"\t"<<last_name[i];
else
cout<<"\nGrade isnot outstanding for the"<<"\t"<<first_name[i]<<"\t"<<last_name[i];
}
}
class professor:public person
{
public:
void inputfun();
};
void professor::inputfun()
{
cout<<"Enter the recods of the three professors:";
for (int i=0;i<3;i++)
{
cout<<"\n\nEnter the Professor First name:";
cin>>first_name[i];
cout<<"Enter the Professor Last Name:";
cin>>last_name[i];
cout<<"Enter the garde for the Professor:";
cin>>grade[i];
}
}
class student: public person
{
public:
void inputfun();
};
void student::inputfun()
{
cout<<"\n\nEnter the recods of three students:";
for (int i=0;i<3;i++)
{
cout<<"\n\nEnter the Student First name:";
cin>>first_name[i];
cout<<"Enter the Student Last Name:";
cin>>last_name[i];
cout<<"Enter the garde for the Student:";
cin>>grade[i];
}
}
void main()
{
int ch;
professor p;
student s;
clrscr();
p.inputfun();
s.inputfun();
cout<<"Press\n1.For Professor grade\n2.For Student grade";
cout<<"\nEnter your choice:";
cin>>ch;
switch (ch)
{
case 1:
p.gradefun();
break;
case 2:
s.gradefun();
break;
default:
cout<<"\nYou didnot enter the correct choice.";
break;
}
getch();
}
OUTPUT:





C++ PROGRAM: Define a class Name with member last, first, suffix, and nick name implement the class person so that persons name is of the type name class instead string.


Define a class Name with member last, first, suffix, and nick name implement the class person so that persons name is of the type name class instead string.

SOLUTION:
#include<iostream.h>
#include<conio.h>
class Name
{
public:
char first_name[20],last_name[20],suffix[20],nick_name[20];
};
class Person:public Name
{
public:
void input();
void output();
};
void Person::input()
{
cout<<"Enter your First Name:";
cin>>first_name;
cout<<"\nEnter your Last Name:";
cin>>last_name;
cout<<"\nEnter the Suffix for your Last Name:";
cin>>suffix;
cout<<"\nEnter your Nick Name:";
cin>>nick_name;
}
void Person::output()
{
cout<<"\n\n\nYour First Name is:"<<first_name;
cout<<"\nYour Last Name:"<<last_name;
cout<<"\nThe Suffix for your Last Name:"<<suffix;
cout<<"\nYour Nick Name is:"<<nick_name;
}
void main()
{
Person p;
clrscr();
p.input();
p.output();
getch();
}
OUTPUT:






C++ PROGRAM: Implement a class Distance with 2 data members feet, inches. Here feet is integer and inches are float, include constructors to initialize data members and member functions as following:

Implement a class Distance with 2 data members feet, inches. Here feet is integer and inches are float, include constructors to initialize data members and member functions as following:
A. Method a.compareTo(b) which compares 2 distances and then retuns -1 if d1>d2, a zero when equal or +1 for correct order.
B. Method to add two distances, it should return a distance object.
C. Method to subtract two distances, it should return a distance object.

D. Write a mian program that inputs 10 distances and arranges them in ascending order.

SOLUTION:
#include<iostream.h>
#include<conio.h>
class distance
{
int feet;
float inch;
public:
distance()
{
cout<<"Constructor for initialization of data members and funtions.";
}
void comparison();
void addition();
void subtraction();
};
void distance::comparison()
{
float d1,d2;
cout<<"\nEnter the 2 distances for comparision:";
cin>>d1>>d2;
if (d1>d2)
cout<<"-1";
else if (d1==d2)
cout<<"0";
else
cout<<"1";
}
void distance::addition()
{
float d1,d2;
cout<<"\nEnter the 2 distances for addition:";
cin>>d1>>d2;
inch=d1+d2;
cout<<"\nThe total distance object is:"<<inch;
}
void distance::subtraction()
{
float d1,d2;
cout<<"\nEnter the 2 distances for subtraction:";
cin>>d1>>d2;
inch=d1-d2;
cout<<"\nThe remaining distance object is:"<<inch;
}
void main()
{
int d[10],i,j,temp, ch;
clrscr();
distance d1;
cout<<"\n\nEnter the 10 distances:";
for (i=0;i<10;i++)
{
cin>>d[i];
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if (d[i]>d[j])
{
temp=d[j];
d[j]=d[i];
d[i]=temp;
}
}
}
cout<<"The 10 distances sorted in ascending order are:\n";
for(i=0;i<10;i++)
cout<<"\n"<<d[i];
cout<<"\n\nPress\n1.For Comparison of 2 distances\n2.For Addition of 2 distances\n3.For Subtraction of 2 distances";
cout<<"\nEnter you choice:";
cin>>ch;
switch (ch)
{
case 1:
d1.comparison();
break;
case 2:
d1.addition();
break;
case 3:
d1.subtraction();
break;
default:
cout<<"\nYou didnot the correct option:";
break;
}
getch();
}
OUTPUT: