Saturday, March 9, 2019

Assignment 10a: Clock with Pendulum








#include<iostream>
#include<graphics.h>

using namespace std;

class clock
{

    public:
        void create()
        {
            rectangle(425,25,725,335);
            setcolor(14);
            circle(575,180,50);
            setfillstyle(SOLID_FILL,14);
            floodfill(575,180,14);

            setcolor(10);
            settextstyle(SIMPLEX_FONT,HORIZ_DIR,4);

            outtextxy(650,28,"1");
            outtextxy(700,98,"2");
            outtextxy(700,168,"3");
            outtextxy(700,238,"4");
            outtextxy(648,298,"5");
            outtextxy(558,298,"6");
            outtextxy(468,298,"7");
            outtextxy(428,238,"8");
            outtextxy(428,168,"9");
            outtextxy(428,98,"10");
            outtextxy(460,28,"11");
            outtextxy(555,28,"12");
        }

        void pend()
        {
            int a=576,b=535,r=30;
            setcolor(15);
            line(576,335,a,b);
            circle(a,(b+r),r);
            setfillstyle(SOLID_FILL,15);
            floodfill(a,b+r,15);
            delay(700);
            setcolor(0);
            line(576,335,a,b);
            circle(a,(b+r),r);
            setfillstyle(SOLID_FILL,0);
            floodfill(a,b+r,0);
        labela:
            while(a!=490)
            {
                a=a-1;
                b=b-1;

                setcolor(15);
                line(576,335,a,b);
                circle(a,(b+r),r);
                setfillstyle(SOLID_FILL,15);
                floodfill(a,b+r,15);

                delay(60);

                setcolor(0);
                line(576,335,a,b);
                circle(a,(b+r),r);
                setfillstyle(SOLID_FILL,0);
                floodfill(a,b+r,0);
            }
            while(a!=575)
            {
                a=a+1;
                b=b+1;

                setcolor(15);
                line(576,335,a,b);
                circle(a,(b+r),r);
                setfillstyle(SOLID_FILL,15);
                floodfill(a,b+r,15);

                delay(60);

                setcolor(0);
                line(576,335,a,b);
                circle(a,(b+r),r);
                setfillstyle(SOLID_FILL,0);
                floodfill(a,b+r,0);
            }
            while(a!=640)
            {
                a=a+1;
                b=b-1;

                setcolor(15);
                line(576,335,a,b);
                circle(a,(b+r),r);
                setfillstyle(SOLID_FILL,15);
                floodfill(a,b+r,15);

                delay(60);

                setcolor(0);
                line(576,335,a,b);
                circle(a,(b+r),r);
                setfillstyle(SOLID_FILL,0);
                floodfill(a,b+r,0);
            }
            while(a!=575)
            {
                a=a-1;
                b=b+1;

                setcolor(15);
                line(576,335,a,b);
                circle(a,(b+r),r);
                setfillstyle(SOLID_FILL,15);
                floodfill(a,b+r,15);

                delay(60);

                setcolor(0);
                line(576,335,a,b);
                circle(a,(b+r),r);
                setfillstyle(SOLID_FILL,0);
                floodfill(a,b+r,0);
            }
            goto labela;
        }
}c;

int main()
{

    initwindow(1200,800,"CLOCK WITH PENDULUM");

    c.create();
    c.pend();

    getch();
    cleardevice();
    closegraph();
}

Assignment 10b: National Flag Hoisting







#include<iostream>
#include<graphics.h>

using namespace std;

class flag
{
    float l,b,x,y,h;       //In reality length=width*1.5

    public:
        flag()
        {
            l=160;
            b=35;
            x=130;
            y=102;
            h=275;
        }
        void create()
        {
            //ORANGE(here red is used as orange is not available)
            setcolor(12);
            rectangle(50,50,50+l,50+b);
            setfillstyle(SOLID_FILL,12);
            floodfill(65,65,12);

            //WHITE
            setcolor(15);
            rectangle(50,50+b,50+l,50+(2*b));
            setfillstyle(SOLID_FILL,15);
            floodfill(65,90,15);

            //GREEN
            setcolor(2);
            rectangle(50,50+(2*b),50+l,50+(3*b));
            setfillstyle(SOLID_FILL,2);
            floodfill(65,135,2);

            //ASHOKA CHAKRA
            setcolor(1);
            circle(x,y,17);
            circle(x,y,7);
            setfillstyle(SOLID_FILL,1);
            floodfill(x+2,y+2,1);
            line(x,86,x,118);   //vertical
            line(113,y,147,y);   //horizontal

            //QUAD 1
            line(x+3,86+2,x,y);
            line(x+6,86+3,x,y);
            line(x+9,86+4,x,y);
            line(x+12,86+7,x,y);
            line(x+15,86+11,x,y);

            //QUAD 2
            line(x-3,86+2,x,y);
            line(x-6,86+3,x,y);
            line(x-9,86+4,x,y);
            line(x-12,86+7,x,y);
            line(x-15,86+11,x,y);

            //QUAD 3
            line(x-3,118-2,x,y);
            line(x-6,118-3,x,y);
            line(x-9,118-4,x,y);
            line(x-12,118-7,x,y);
            line(x-15,118-11,x,y);

            //QUAD 4
            line(x+3,118-2,x,y);
            line(x+6,118-3,x,y);
            line(x+9,118-4,x,y);
            line(x+12,118-7,x,y);
            line(x+15,118-11,x,y);
        }

        void pole()
        {
            setcolor(8);
            rectangle(47,50,50,50+h);
            setfillstyle(SOLID_FILL,8);
            floodfill(48,90,8);
        }
        void st()
        {
            int dest1=305;
            setcolor(15);
            line(52,dest1,70,dest1);
            delay(700);
            setcolor(0);
            line(52,dest1,70,dest1);
            while(dest1!=50)
            {
                dest1=dest1-1;
                setcolor(15);
                line(52,dest1,70,dest1);
                delay(60);
                setcolor(0);
                line(52,dest1,70,dest1);
                //pole();n for
            }

        }
}f;

int main()
{
    int gd=0,gm;
    initgraph(&gd,&gm,NULL);

    f.pole();
    f.st();

    cleardevice();

    f.pole();
    f.create();

    setcolor(12);
    outtextxy(240,240,"VAND");
    delay(1000);
    setcolor(15);
    outtextxy(276,240,"E MAT");
    delay(1000);
    setcolor(2);
    outtextxy(317,240,"ARAM");

    getch();
    cleardevice();
    closegraph();
}

Assignment 10c: • Vehicle locomotion










#include <iostream>
#include<graphics.h>
using namespace std;

class car
{
    //FOR WHEEL
    int w1,y,r,w2;

    //FOR OTHER THAN WHEEL
    int a1,a2,a3;
    int b1,b2;
    int c1,c2,c3;
    int d1;

public:
    car()
    {
        w1=77;
        y=655;
        w2=229;
        r=30;

        a1=35;
        a2=46;
        a3=80;
        b1=108;
        b2=198;
        c1=260;
        c2=320;
        c3=232;
        d1=156;
    }
    void road()
    {
        line(0,690,900,690);
    }
    void create()
    {
        //WHEELS//
        circle(w1,y,r);
        circle(w2,y,r);

        //OTHER THAN WHEELS
        line(a1,y,a2,y);
        line(b1,y,b2,y);
        line(c1,y,c2,y);
        line(a1,y,a1,595);
        line(a1,595,a2,595);
        line(c2,y,c2,605);
        line(c2,605,c1,595);
        line(a2,595,c1,595);
        line(a2,595,a3,525);
        line(c1,595,c3,525);
        line(a3,525,c3,525);
        line(d1,525,d1,595);

        delay(3000);

        setcolor(RED);
        settextstyle(BOLD_FONT,HORIZ_DIR,7);
        outtextxy(325,25,"3");
        /////////
        delay(1000);
        ////////
        setcolor(0);
        outtextxy(325,25,"3");
        setcolor(YELLOW);
        outtextxy(325,25,"2");
        ////////
        delay(1000);
        ///////
        setcolor(0);
        outtextxy(325,25,"2");
        setcolor(GREEN);
        outtextxy(325,25,"1");
        //////
        delay(1000);
        /////
        setcolor(0);
        outtextxy(325,25,"1");
        setcolor(9);
        outtextxy(325,25,"GO..GO..GO");

        while(c2!=890)
        {
            delay(2);
            setcolor(0);
            line(a1,y,a2,y);
            line(b1,y,b2,y);
            line(c1,y,c2,y);
            line(a1,y,a1,595);
            line(a1,595,a2,595);
            line(c2,y,c2,605);
            line(c2,605,c1,595);
            line(a2,595,c1,595);
            line(a2,595,a3,525);
            line(c1,595,c3,525);
            line(a3,525,c3,525);
            line(d1,525,d1,595);
            circle(w1,y,r);
            circle(w2,y,r);
            a1=a1+1;
            a2=a2+1;
            a3=a3+1;
            b1=b1+1;
            b2=b2+1;
            c1=c1+1;
            c2=c2+1;
            c3=c3+1;
            d1=d1+1;
            w1=w1+1;
            w2=w2+1;
            setcolor(15);
            line(a1,y,a2,y);
            line(b1,y,b2,y);
            line(c1,y,c2,y);
            line(a1,y,a1,595);
            line(a1,595,a2,595);
            line(c2,y,c2,605);
            line(c2,605,c1,595);
            line(a2,595,c1,595);
            line(a2,595,a3,525);
            line(c1,595,c3,525);
            line(a3,525,c3,525);
            line(d1,525,d1,595);
            circle(w1,y,r);
            circle(w2,y,r);
        }
    }
}c;

int main()
{
    initwindow(900,800,"Moving Car");

    c.road();
    c.create();
    getch();
}

Sunday, March 3, 2019

Assignment 2a: Write C++ program to draw circle using Bresenham‘s algorithm. Inherit pixel class.







#include<iostream>
#include<graphics.h>

using namespace std;

class circ
{
 int x,y,d;
 public:
  void bcircle(int r1)
  {
   x=0;
   y=r1;
   d=3-(2*r1);
   
   while(x<y)
   {
    putpixel(x+250,y+250,WHITE);
    putpixel(x+250,250-y,WHITE);
    putpixel(250-x,y+250,WHITE);
    putpixel(250-x,250-y,WHITE);
    putpixel(250+y,250+x,WHITE);
    putpixel(y+250,250-x,WHITE);
    putpixel(250-y,250+x,WHITE);
    putpixel(250-y,250-x,WHITE);
    if(d<=0)
    {
     d=d+(4*x)+6;
     x++;
    }
    else
    {
     d=d+(4*(x-y))+10;
     x++;
     y--;
    }
    
   }
  }
}C;

int main()
{
 int gd,gm,r;
 gd=gm=DETECT;
 
 cout<<"Enter radius of circle:";
 cin>>r;
 
 
 initgraph(&gd,&gm,NULL);
 
 C.bcircle(r);
 
 getch();
 cleardevice();
 closegraph();
 
}

Assignment 6: Write C++ program to fill polygon using scan line algorithm.




#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;

class scan
{
    public:
    int x[20],y[20],k;
    float slope[20],x_int[20];
    void polygon(int n);
};

void scan::polygon(int n)
{
    int i,p;
    float dx,dy;
    int temp;
    x[n]=x[0];
    y[n]=y[0];
    for(i=0;i<n;i++)
    {
        line(x[i],y[i],x[i+1],y[i+1]);
    }
    
    for(i=0;i<n;i++)
    {
        dy=y[i+1]-y[i];
        dx=x[i+1]-x[i];
        if(dy==0)
        slope[i]=1;
        else if(dx==0) 
        slope[i]=0;
        else 
        slope[i]=dx/dy;
    }
    
    for(p=0;p<480;p++)
    {
        k=0;
        for(i=0;i<n;i++)
        {
            if(((y[i]<=p)&&(y[i+1]>p))||((y[i]>p)&&(y[i+1]<=p)))
              {
                  x_int[k]=x[i]+slope[i]*(p-y[i]);
                  k++;
              }
          }
          
          for(int j=0;j<k-1;j++)
          {
              for(i=0;i<k-1;i++)
              {
                  if(x_int[i]>x_int[i+1])
                  {
                      temp=x_int[i];
                      x_int[i]=x_int[i+1];
                      x_int[i+1]=temp;
                  }
              }
          }
         
        
          for(i=0;i<k;i=i+2)
          {
               setcolor(YELLOW);
              line(x_int[i],p,x_int[i+1],p);
              delay(10);
              
          }
          
          
      }
  }
  
  
  int main()
  {
      int n,i;
      scan p;
      cout<<"Enter edge : \t";
      cin>>n;
      cout<<"\n\nEnter Coordinates : \t";
      for(i=0;i<n;i++)
      {
          cin>>p.x[i]>>p.y[i];
      }
      int gd,gm;
      gd=DETECT;
      initgraph(&gd,&gm,NULL);
      p.polygon(n);
      getch();
        closegraph();
        return 0;
 }

Assignment 5: Write C++ program to draw a convex polygon and fill it with desired color using Seed fill algorithm. Use mouse interfacing to draw polygon.





#include<iostream>
#include<graphics.h>
using namespace std;

void boundary(int x, int y, int newcol,int edgecol)
{
 int current;
 current = getpixel(x, y);
 if(current != edgecol && current != newcol)
  {
  putpixel(x,y,newcol);
 
  boundary(x+1, y, newcol,edgecol);
  boundary(x,y+1, newcol,edgecol);
  boundary(x-1,y, newcol,edgecol);
  boundary(x, y-1, newcol,edgecol);
  }
  
}


void flood(int x, int y, int newcol,int oldcol)
{ 
 if(getpixel(x, y) == oldcol)
  {
  putpixel(x,y,newcol);
  flood(x+1, y, newcol,oldcol);
  flood(x-1,y, newcol,oldcol);
  flood(x,y+1, newcol,oldcol);
  flood(x, y-1, newcol,oldcol);
  }
  delay(3);
}

int main()
{
 int x1,x2,y1,y2,x3,y3,x4,y4;
 int x5,y5,x6,y6;
 
  cout<<"\nEnter co-ordinates of left top corner of rectangle one: ";
  cin>>x1>>y1;
  cout<<"\nEnter co-ordinates of bottom right corner of rectangle one: ";
  cin>>x2>>y2;
  x5=((x2-x1)/2)+x1;
  y5=((y2-y1)/2)+y1;
    
  cout<<"\nEnter co-ordinates of first point for 2nd rectangle:";
  cin>>x3>>y3;
  cout<<"\nEnter co-ordinates of second point for 2nd rectangle:";
  cin>>x4>>y4;
  x6=((x4-x3)/2)+x3;
  y6=((y4-y3)/2)+y3;
 
 int gd=0,gm;
 initgraph(&gd,&gm,NULL);
 
 rectangle(x1,y1,x2,y2);
 rectangle(x3,y3,x4,y4);
 
 boundary(x5,y5,8,15);
 
 flood(x6,y6,10,0);
 
 getch();
 cleardevice();
    closegraph();
 return 0;
}

Assignment 7: Write C++ program to implement Cohen-Sutherland line clipping algorithm for given window.







#include<iostream>
#include<graphics.h>
using namespace std;

class clipping
{
    float a1,b1,a2,b2;
    float x1,x2,x3,x4,y1,y2,y3,y4;
    float y;

    public:
        clipping()
        {
            a1=100;
            a2=400;
            b1=100;
            b2=400;
        }
        void window()
        {
            rectangle(a1,b1,a2,b2);
        }
        void lget()
        {
            cout<<"\n\t --------------LINE DRAWING---------------";

            cout<<"\n\t Enter the coordinates for (x1,y1): ";
            cin>>x1>>y1;

            cout<<"\n\t Enter the coordinates for (x2,y2)";
            cin>>x2>>y2;
            line(x1,y1,x2,y2);

        }
        void clip()
        {
            if((x1<a1)&&(x1<a2)&&(x2>a1)&&(x2<a2)&&(y1>b1)&&(y1<b2)&&(y2>b1)&&(y2<b2))            //LEFT
            {
                if(x2-x1!=0)
                    y1=valint(a1);
                line(a1,y1,x2,y2);
                outtextxy(150,150,"The left side of line is clipped");
            }
            if((x1>a1)&&(x1<a2)&&(x2>a1)&&(x2>a2)&&(y1>b1)&&(y1<b2)&&(y2>b1)&&(y2<b2))        //RIGHT
            {
                if(x2-x1!=0)
                    y2=valint(a2);
                line(x1,y1,a2,y2);
                outtextxy(150,150,"The right side of line is clipped");
            }
            if((x1>a1)&&(x1<a2)&&(x2>a1)&&(x2<a2)&&(y1<b1)&&(y1<b2)&&(y2>b1)&&(y2<b2))        //TOP
            {
                if(y2-y1!=0)
                    x1=valinta(b1);
                line(x1,b1,x2,y2);
                outtextxy(150,150,"The top side of line is clipped");
            }
            if((x1>a1)&&(x1<a2)&&(x2>a1)&&(x2<a2)&&(y1>b1)&&(y1<b2)&&(y2>b1)&&(y2>b2))        //BOTTOM
            {
                if(y2-y1!=0)
                    x2=valinta(b2);
                line(x1,y1,x2,b2);
                outtextxy(150,150,"The bottom side of line is clipped");
            }
            if((x1>a1)&&(x1<a2)&&(x2>a1)&&(x2<a2)&&(y1>b1)&&(y1<b2)&&(y2>b1)&&(y2<b2))        //INSIDE
            {
                line(x1,y1,x2,y2);
                outtextxy(150,150,"The line is inside the window");
            }
        }

        float valint(float x)
        {
            float m,b;

                m=(y2-y1)/(x2-x1);
                b=y1-(m*x1);
                y=(m*x)+b;

            return y;
        }

        float valinta(float y)
        {
            float m1,b1;

                m1=(x2-x1)/(y2-y1);
                b1=x1-(m1*y1);
                y=(m1*y)+b1;

            return y;
        }
}c;

int main()
{
    int gd=0,gm;

    initgraph(&gd,&gm,NULL);

    c.window();
    c.lget();
    delay(3000);
    cleardevice();
    c.window();
    c.clip();

    delay(5000);
    getch();
    cleardevice();
    closegraph();
}