473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Anjola

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
3 2000
Banfa
9,065 Expert Mod 8TB
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
Anjola
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
9,065 Expert Mod 8TB
Please don't double post.
Sep 23 '06 #4

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

Similar topics

1
by: Anjola | last post by:
if I haveto multiply two matrices one 3x2 and the other 2x3, what threads I have to create . I know that there are 10 of them. #include <stdio.h> #include <iostream.h> /* Allocate memory for a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.