Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

program to draw a line using Cartesian Slope-Intercept Equation

Below is the C++ program to draw a line using Cartesian Slope-Intercept Equation.

Program:
# include
 # include
 # include
 # include

 //____  Function Prototypes  ____//
 void show_screen( );
 void slope_intercept_line(const int,const int,const int,const int);

 //____  main( )  ____//

 int main( )
    {
       int driver=VGA;
       int mode=VGAHI;

       int p1=0;
       int q1=0;

       int p2=0;
       int q2=0;

       do
      {
         show_screen( );

         gotoxy(8,10);
         cout
         gotoxy(8,11);
         cout
         gotoxy(12,13);
         cout         cin>>p1;

         gotoxy(12,14);
         cout         cin>>q1;

         gotoxy(8,18);
         cout
         gotoxy(8,19);
         cout
         gotoxy(12,21);
         cout         cin>>p2;

         gotoxy(12,22);
         cout         cin>>q2;

         initgraph(&driver,&mode,"..\\Bgi");

         setcolor(15);
           slope_intercept_line(p1,q1,p2,q2);

         setcolor(15);
           outtextxy(110,460,"Press to continue or any other key to exit.");

         int key=int(getch( ));

         if(key!=13)
        break;
      }
       while(1);

       return 0;
    }

 //____  Funcion Definitions  ____//

 //____  slope_intercept_line( ) function  ____//

 void slope_intercept_line(const int p1,const int q1,
                        const int p2,const int q2)
    {
       int color=getcolor( );

       float x=p1;
       float y=q1;

       float dx=(p2_p1);
       float dy=(q2_q1);

       float m=(dy/dx);
       float b=(y_(m#x));

       float x_inc=((p2>=p1)?1:_1);

       putpixel(x,y,color);

       while((int)(x+0.5)!=p2)
      {
         x+=x_inc;
         y=((m#x)+b);

         putpixel((int)(x+0.5),(int)(y+0.5),color);
      }
    }

 //____  show_screen( ) function ____//

 void show_screen( )
    {
       restorecrtmode( );
       textmode(C4350);

       cprintf("\n################################################################################");
       cprintf("####################_                                      _####################");
       cprintf("#____________________ ");

       textbackground(1);
       cprintf(" Cartesian Slope Intercept Equation ");
       textbackground(8);

       cprintf(" ____________________#");
       cprintf("#_##################_                                      _##################_#");
       cprintf("#_############################################################################_#");

       for(int count=0;count      cprintf("#_#                                                                          #_#");

       gotoxy(1,46);
       cprintf("#_############################################################################_#");
       cprintf("#______________________________________________________________________________#");
       cprintf("################################################################################");

       gotoxy(8,40);
       cout
       gotoxy(8,41);
       cout
       gotoxy(10,43);
       cout
       gotoxy(1,2);
    }

 //____  THE END  ____//


This post first appeared on Ask For Program, please read the originial post: here

Share the post

program to draw a line using Cartesian Slope-Intercept Equation

×

Subscribe to Ask For Program

Get updates delivered right to your inbox!

Thank you for your subscription

×