what is the equation of the straight line lying between two points with coordinates (x1,y1) and (x2,y2). I got the point of finding the slope, and when (x2-x1!)=0. How you avoid the fact of fabs(y2-y1) much smaller than fabs(x2-x1).
Anjola
Collapse
X
-
Originally posted by Anjolawhat is the equation of the straight line lying between two points with coordinates (x1,y1) and (x2,y2). I got the point of finding the slope, and when (x2-x1!)=0. How you avoid the fact of fabs(y2-y1) much smaller than fabs(x2-x1).
y = m.x + c
Where m is the gradient and c is a constant.
If you have 2 points (x1, y1) (x2, y2) then you can use simulatneous equations to work out m and c (which I think you have already done) because
1) y1 = m.x1 + c
2) y2 = m.x2 + c
so
1 - 2) y1 - y2 = m.x1 + c - (m.x2 - c)
equivilent to y1 - y2 = m(x1 -x2c)
therefore m = (y1 - y2)/(x1 - x2)
Once you have m c is easily calculatable from either 1 or 2
c = y1 - m.x1
If x1 - x2 was 0 you'd have a divide by zero error, however if x1 - x2 == 0 then y1 - y2 == 0 and you actually only have 1 point.
(Sorry I've had to type all this out to get to the point of your question so I could actually understand what you were asking)
You have clearly got to this point and found that in the case that (y1 - y2) is very much smaller than (x1 - x2) you get a 0 result for m when you were expecting a very very very small value. Apart from making sure thatyou are using doubles not floats so that you don't hit this point so soon there is not much you can do, at some point the value of m will get to the point where it is too small to be represented in a double.
There is not much you can do about it mathematically (i.e. using arithmatic), there may be a way of doing something by cheating.
if (y1 - y2)/(x1 - x2) is a very small number that can not be represented in a double then try
1000000.0*(y1 - y2)/(x1 - x2)
What you will get is a value 1000000 times the value of m call it m6 and
c = (1000000.0.y1 - m6.x1)/1000000.0
if c is too small to represent then leave out the last /1000000.0
The smallest positive value that a double(64 bit) can represent is 2.2250738585072 014e-308
suppose your m6 = 2.2250738585072 014e-306
But this is actually 1000000 times the actual result value. How do you output the value? (And here is the cheat
First convert your double to a string (using sprintf)
"2.225073858507 2014e-306"
now find the e using strstr or strchr
Then starting at the character after the e ('-') use strtol to convert the exponent to an integer e = -306. Then terminate the string after the e
"2.225073858507 2014e"
now we know that the value is 1000000 times to big which means that the exponent (which is a power of 10) is +6 to large so take 6 off it -306 - 6 = -312.
convert that to a string and add it to the end of the original string giving the string
"2.225073858507 2014e-312"
And there is your result string, a value too small to hold in a double.
I have to say that I am quite pleased with this given that I was going to answer that there is no solution to your problem, of course you may not consider that this is a practical solution but hey ho. -
i have the program, what if fabs(x1-x2)much smallerthan fabs(y1-y2)
#include <iostream.h>
#include <conio.h>
main()
{
float x1,x2,y1,y2;
float A, B,C;
float z=1;
while (z==1)
{
cout<<"\nPlease enter the cordinates of the first point(x1, y1)";
cin>>x1>>y1;
cout<<"\nPlease enter the cordinates of the second point(x2, y2)\n";
cin>>x2>>y2;
z++;
z--;
if(x2-x1==0)
{
if(x1>0)
cout<<"\tThe equation is : "<<"X"<<"-"<<x1<<"="<<"0\ n\n";
else if (x1<0)
cout<<"\tThe equation is : "<<"X"<<x1<<"=" <<"0\n\n";
else
cout<<"\tThe equation is : "<<"X"<<"="<<"0 \n\n";
}
else if(y2-y1==0)
{
if(y1>0)
cout<<"\tThe equation is : "<<"Y"<<"-"<<y1<<"="<<"0\ n\n";
else if (y1<0)
cout<<"\tThe equation is : "<<"Y"<<y1<<"=" <<"0\n\n";
else
cout<<"\tThe equation is : "<<"Y"<<"="<<"0 \n\n";
}
else
{
A=(y2-y1)/(x2-x1);
B=-1;
C=((A)*(-x1))+y1;
if(C>0)
cout<<"\tThe equation is : "<<A<<"X"<< "-Y"<<"+"<<C<<"=" <<"0\n\n";
else if(C<0)
cout<<"\tThe equation is : "<<A<<"X"<< "-Y"<<C<<"="<<"0\ n\n";
else
cout<<"\tThe equation is : "<<A<<"X"<< "-Y"<<"="<<"0\n\n ";
}
}
getch();
return 0;
}Comment
Comment