By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,640 Members | 2,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,640 IT Pros & Developers. It's quick & easy.

Anjola

P: 11
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).
Sep 22 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
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).
The equation of a straight line is

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.2250738585072014e-308

suppose your m6 = 2.2250738585072014e-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.2250738585072014e-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.2250738585072014e"

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.2250738585072014e-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.
Sep 22 '06 #2

P: 11
#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;
}
Sep 23 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Please don't double post.
Sep 23 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.