Below is the C++ program to draw a line using Cartesian Slope-Intercept Equation.
Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
//____ 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<<"Coordinates of Point_I (p1,q1) :";
gotoxy(8,11);
cout<<"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";
gotoxy(12,13);
cout<<"Enter the value of p1 = ";
cin>>p1;
gotoxy(12,14);
cout<<"Enter the value of q1 = ";
cin>>q1;
gotoxy(8,18);
cout<<"Coordinates of Point_II (p2,q2) :";
gotoxy(8,19);
cout<<"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";
gotoxy(12,21);
cout<<"Enter the value of p2 = ";
cin>>p2;
gotoxy(12,22);
cout<<"Enter the value of q2 = ";
cin>>q2;
initgraph(&driver,&mode,"..\\Bgi");
setcolor(15);
slope_intercept_line(p1,q1,p2,q2);
setcolor(15);
outtextxy(110,460,"Press <Enter> 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<42;count++)
cprintf("#_# #_#");
gotoxy(1,46);
cprintf("#_############################################################################_#");
cprintf("#______________________________________________________________________________#");
cprintf("################################################################################");
gotoxy(8,40);
cout<<"Note :";
gotoxy(8,41);
cout<<"IIIIII";
gotoxy(10,43);
cout<<"This program is better for those lines with é<ñ45ø with x_axis.";
gotoxy(1,2);
}
//____ THE END ____//
# include <graphics.h>
# include <conio.h>
# include <math.h>
//____ 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<<"Coordinates of Point_I (p1,q1) :";
gotoxy(8,11);
cout<<"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";
gotoxy(12,13);
cout<<"Enter the value of p1 = ";
cin>>p1;
gotoxy(12,14);
cout<<"Enter the value of q1 = ";
cin>>q1;
gotoxy(8,18);
cout<<"Coordinates of Point_II (p2,q2) :";
gotoxy(8,19);
cout<<"IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";
gotoxy(12,21);
cout<<"Enter the value of p2 = ";
cin>>p2;
gotoxy(12,22);
cout<<"Enter the value of q2 = ";
cin>>q2;
initgraph(&driver,&mode,"..\\Bgi");
setcolor(15);
slope_intercept_line(p1,q1,p2,q2);
setcolor(15);
outtextxy(110,460,"Press <Enter> 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<42;count++)
cprintf("#_# #_#");
gotoxy(1,46);
cprintf("#_############################################################################_#");
cprintf("#______________________________________________________________________________#");
cprintf("################################################################################");
gotoxy(8,40);
cout<<"Note :";
gotoxy(8,41);
cout<<"IIIIII";
gotoxy(10,43);
cout<<"This program is better for those lines with é<ñ45ø with x_axis.";
gotoxy(1,2);
}
//____ THE END ____//
0 Comments