473,809 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to know two lines are a pare parallel lines

For example, line L1 and line L2 are two lines in two-dimensional
space, the start-points and end-points can be described with following
the `point_t' type. The start-points and end-points are: Line L1
(L1_str, L1_end), L2 (L2_str, L2_end).

Can I compare the float value of sloping rate of two lines to determine
whether they are parallel lines? Some paper books tell that float value
can not be compared to float value. Some people also try to convince
others on this point that do not perform the comparing on float values.
Then how can I complete the task of determining whether two lines are
parallel lines by their sloping rate?

/* type for points */
typedef struct
{
double x;
double y;
} point_t;

/* calculate the value of sloping rate of a line */
double slope(point_t pt_start, point_t pt_end);

double sl_L1 = slope (L1_str, L1_end);
double s2_L2 = slope (L2_str, L2_end);

if (s1_L1 == s2_L2) /* CAUTION: compare float data to float, is it ok?
*/
{
/* L1 and L2 are parallel lines */
}
else
{
/* L1 and L2 are not parallel lines */
}

--
Sincerely,
lovecreatesbeau ty

Apr 27 '06 #1
11 10485
lovecreatesbeau ty wrote:
For example, line L1 and line L2 are two lines in two-dimensional
space, the start-points and end-points can be described with following
the `point_t' type. The start-points and end-points are: Line L1
(L1_str, L1_end), L2 (L2_str, L2_end).

Can I compare the float value of sloping rate of two lines to determine
whether they are parallel lines? Some paper books tell that float value
can not be compared to float value. Some people also try to convince
others on this point that do not perform the comparing on float values.
Then how can I complete the task of determining whether two lines are
parallel lines by their sloping rate?


You simply need to determine how close to numerical equality the slopes
need to be to count as parallel. This will tell whether, within the
constraints of your measurements and arithmetic precision, you can tell
whether the lines are not parallel. You can never tell that they are
actually parallel when any of the values involved are not expressible
exactly as floating point values. Notice that if your points are
expressible in integral or even rational terms, then you may be able to
avoid floating point representations entirely.
Apr 27 '06 #2
"lovecreatesbea uty" writes:
For example, line L1 and line L2 are two lines in two-dimensional
space, the start-points and end-points can be described with following
the `point_t' type. The start-points and end-points are: Line L1
(L1_str, L1_end), L2 (L2_str, L2_end).

Can I compare the float value of sloping rate of two lines to determine
whether they are parallel lines? Some paper books tell that float value
can not be compared to float value. Some people also try to convince
others on this point that do not perform the comparing on float values.
Then how can I complete the task of determining whether two lines are
parallel lines by their sloping rate?


<snip>

It is common to compare two floating point numbers to see whether they are
within a "guard band" of one another. An elementary way would be to create
a constant such as this:

const double epsilon = 1.0e-06;

And then see if two numbers are equal + or - one epsilon. For more advanced
usage you could actually compute epsilon so it had some relationship to the
problem you are solving. For example, if you are dealing with the distance
to Pluto and the distance is in centimeters, epsilon might be bigger.
Apr 27 '06 #3
In article <11************ **********@t31g 2000cwb.googleg roups.com>,
lovecreatesbeau ty <lo************ ***@gmail.com> wrote:
For example, line L1 and line L2 are two lines in two-dimensional
space, the start-points and end-points can be described with following
the `point_t' type. The start-points and end-points are: Line L1
(L1_str, L1_end), L2 (L2_str, L2_end). Can I compare the float value of sloping rate of two lines to determine
whether they are parallel lines? Some paper books tell that float value
can not be compared to float value. Some people also try to convince
others on this point that do not perform the comparing on float values.
Then how can I complete the task of determining whether two lines are
parallel lines by their sloping rate?


The question implicitly assumes that you need to distinguish the
"very-nearly" parallel lines from the "exactly" parallel lines.
That in turn assumes that the coordinates you have been given
are exact coordinates.

As your coordinates are being paseed in as double, you should instead
be assuming that the coordinates are not precise. You then cannot
calculate the exact slope: you can only calculate a range of slopes
for each of the pairs, and then attempt to determine whether the two
ranges overlap. That comparision in turn might have to be inexact.

In short, I would suggest that you do not try to determine whether
the lines are "exactly" parallel, and instead write your code to
determine whether they are parallel to within a tolerance.
You will find even this to be a challenge to get right in the case
where the coordinate differences are either very small or very large.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Apr 27 '06 #4
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <11************ **********@t31g 2000cwb.googleg roups.com>,
lovecreatesbeau ty <lo************ ***@gmail.com> wrote:
For example, line L1 and line L2 are two lines in two-dimensional
space, the start-points and end-points can be described with following
the `point_t' type. The start-points and end-points are: Line L1
(L1_str, L1_end), L2 (L2_str, L2_end).

Can I compare the float value of sloping rate of two lines to determine
whether they are parallel lines? Some paper books tell that float value
can not be compared to float value. Some people also try to convince
others on this point that do not perform the comparing on float values.
Then how can I complete the task of determining whether two lines are
parallel lines by their sloping rate?


The question implicitly assumes that you need to distinguish the
"very-nearly" parallel lines from the "exactly" parallel lines.
That in turn assumes that the coordinates you have been given
are exact coordinates.

As your coordinates are being paseed in as double, you should instead
be assuming that the coordinates are not precise. You then cannot
calculate the exact slope: you can only calculate a range of slopes
for each of the pairs, and then attempt to determine whether the two
ranges overlap. That comparision in turn might have to be inexact.

In short, I would suggest that you do not try to determine whether
the lines are "exactly" parallel, and instead write your code to
determine whether they are parallel to within a tolerance.
You will find even this to be a challenge to get right in the case
where the coordinate differences are either very small or very large.


Right. Checking whether the slopes are exactly equal is relatively
easy, but doesn't work if the input data is inexact (as it almost
inevitably will be). Checking whether the slopes are nearly equal is
tempting, but it breaks down in some cases; for example, two lines
might both be nearly vertical, and therefore nearly parallel to each
other, but have slopes of -1.0e6 and +1.0e6.

Mathematically, I think the best approach is to compute the *angle*
between the lines; if the angle is close to 0, the lines are nearly
parallel. You also need to decide what "nearly" means; determining
whether a small difference in angle comes from roundoff error or
indicates that the lines really aren't parallel is probably the
toughest part of the problem.

comp.graphics.a lgorithms might be a good place for this kind of thing.
(I base that purely on the name of the newsgroup; browse it and/or
read its FAQ before posting.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 27 '06 #5
Keith Thompson wrote:
Right. Checking whether the slopes are exactly equal is relatively
easy, but doesn't work if the input data is inexact (as it almost
inevitably will be). Checking whether the slopes are nearly equal is
tempting, but it breaks down in some cases; for example, two lines
might both be nearly vertical, and therefore nearly parallel to each
other, but have slopes of -1.0e6 and +1.0e6.
Is that 6 supposed to be negative?
Mathematically, I think the best approach is to compute the *angle*
between the lines; if the angle is close to 0, the lines are nearly
parallel. You also need to decide what "nearly" means; determining
whether a small difference in angle comes from roundoff error or
indicates that the lines really aren't parallel is probably the
toughest part of the problem.

You could simply store rise/run as two distinct integers (or floats, I
guess), and compare those. In the case of integers you will get an exact
match.

Of course, then you have problems with lowest terms; how do you tell if
(6/3) and (2/1) are the same?

--
Andrew Poelstra <http://www.wpsoftware. net/blog>
Every prime number in a series as a joke
Made all the patterns clear when I took that final toke
Apr 27 '06 #6
Andrew Poelstra <ap*******@shaw .ca> writes:
Keith Thompson wrote:
Right. Checking whether the slopes are exactly equal is relatively
easy, but doesn't work if the input data is inexact (as it almost
inevitably will be). Checking whether the slopes are nearly equal is
tempting, but it breaks down in some cases; for example, two lines
might both be nearly vertical, and therefore nearly parallel to each
other, but have slopes of -1.0e6 and +1.0e6.

Is that 6 supposed to be negative?


No.

A line with a very large positive slope is nearly vertical. A line
with a very large negative slope is also nearly vertical.

A line with a slop of -1.0e-6 or +1.0e-6 would be nearly horizontal;
that doesn't illustrate the point I was making because those two
slopes are numerically close to each other. With slopes of very large
magnitude, you can have nearly parallel lines whose slopes differ
greatly.

On the other hand, if you're really interested in whether the lines
have similar slopes, you can ignore this. It all depends on what the
actual requirements are, and what they're based on.
Mathematically, I think the best approach is to compute the *angle*
between the lines; if the angle is close to 0, the lines are nearly
parallel. You also need to decide what "nearly" means; determining
whether a small difference in angle comes from roundoff error or
indicates that the lines really aren't parallel is probably the
toughest part of the problem.

You could simply store rise/run as two distinct integers (or floats, I
guess), and compare those. In the case of integers you will get an
exact match.

Of course, then you have problems with lowest terms; how do you tell
if (6/3) and (2/1) are the same?


Reducing integer fractions to lowest terms isn't difficult; Euclid
knew how to do it.

If you can get your raw data in a form that's not subject to rounding
errors (multi-precision rationals, for example), you don't have to
worry about approximate results; you can determine whether the lines
are exactly parallel or not. The details depend strongly on the input
data format. Without knowing anything about that, there aren't really
any C issues.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 27 '06 #7
Andrew Poelstra wrote:
Keith Thompson wrote:
Right. Checking whether the slopes are exactly equal is relatively
easy, but doesn't work if the input data is inexact (as it almost
inevitably will be). Checking whether the slopes are nearly equal is
tempting, but it breaks down in some cases; for example, two lines
might both be nearly vertical, and therefore nearly parallel to each
other, but have slopes of -1.0e6 and +1.0e6.
Is that 6 supposed to be negative?


No. Consider a line that's almost vertical but not quite (eg.
the side of that tall pointy thing near the white house).
Say its "rise" is 100m and its "run" is 10mm. So its slope is
10000. Then consider a nearby line whose rise is 1000m and
run is 10.01mm. Its slope is 9990. This difference is 10, which
is much larger than our tolerance of 1e-6 so the lines should
not be considered parallel.

But consider the same structure lying on its side. Now the
first line has "rise" 10mm and "run" 100m so its slope is
0.0001. The second line's slope is 0.0001001. The difference
is 0.0000001 which is within our tolerance, so the lines are
now parallel!

Hence the suggestion to consider the angles of the lines,
rather than their slope, ie. arctan(rise/run)
You could simply store rise/run as two distinct integers (or floats, I
guess), and compare those. In the case of integers you will get an
exact match.
Most floats are not exact.
Of course, then you have problems with lowest terms; how do you
tell if (6/3) and (2/1) are the same?


a/b == c/d iff ad == bc.

If you can't multiply due to problems with integer overflow,
then test (a/c).d == b and (a/c).c == a.

Apr 27 '06 #8
In article <11************ **********@t31g 2000cwb.googleg roups.com>,
lovecreatesbeau ty <lo************ ***@gmail.com> wrote:
For example, line L1 and line L2 are two lines in two-dimensional
space, the start-points and end-points can be described with following
the `point_t' type. The start-points and end-points are: Line L1
(L1_str, L1_end), L2 (L2_str, L2_end).

Can I compare the float value of sloping rate of two lines to determine
whether they are parallel lines?


Comparing floating point values is problematic.
Comparing slopes to check for parallelism is even more problematic.
Slope is undefined for lines parallel to the y axis (the axis of
ordinates.)

Here is the proper way to check for parallelism.

Suppose line 1 goes through points A=(a1,a2), B=(b1,b2), and A != B.
Suppose line 2 goes through points C=(c1,c2), D=(d1,d2), and C != D.

Then the lines are parallel if and only if

(b1 - a1) * (d2 - c2) - (b2 - a2) * (d1 - c1) = 0.

This involves no divisions and works for arbitrary lines, including
those that are parallel to the y axis.

--
Rouben Rostamian
Apr 27 '06 #9
In article <e2**********@p c18.math.umbc.e du>,
Rouben Rostamian <ro****@pc18.ma th.umbc.edu> wrote:
Here is the proper way to check for parallelism. Suppose line 1 goes through points A=(a1,a2), B=(b1,b2), and A != B.
Suppose line 2 goes through points C=(c1,c2), D=(d1,d2), and C != D.
The OP's data structure showed the coordinates to be stored as doubles.
What is the "proper way" to test that A != B or C != D for
pairs of doubles? cf. a recent thread that explored that even
if you had b = a; if (a = b) that you could not be sure
the test would succeed.

Then the lines are parallel if and only if (b1 - a1) * (d2 - c2) - (b2 - a2) * (d1 - c1) = 0. This involves no divisions and works for arbitrary lines, including
those that are parallel to the y axis.


Yes, it involves no divisions, but on the other hand it involves
2 multiplications and 4 subtractions -- which not only results
in precision problems, but also leads to the possibility of
arithmetic overflow (e.g., let a1 and c2 be small and b1 and d2 be
very large...)

If you had
b1 = -a1; b2 = -a2; d1 = -c1; d2 = -c2;
then as you changed the signs of all of the components, the two should
have exactly the same slope. Have a look, though, at what the formula
would calculate:

(-a1 - a1) * (-c2 - c2) - (-a2 - a2) *(-c1 - c1)

Evalulated symbolically, that would be

4 * a1 * c2 - 4 * a2 * c1

but in practice it's going to be:

(-2 * a1 + e1) * (-2 * c2 + e2) - (-2 * a2 + e3) * (-2 * c1 + e4)

which differs by

2 * (a2 * e4 + c1 * e3) - 2 * (a1 * e2 + c2 * e1) + e1 * e2 - e3 * e4
Suppose we get really close -- suppose only one of the epsilons (e1)
is 1e-15 and the others are 0. The difference would then be
- 1/500000000000000 * c2 and if c2 were (say) 5e18 then the
difference term would be -10000 which is clearly non-negligable.
When you are doing your numerical analysis, keep in mind that
(-a + -b) is not necessarily going to be the same as (- (a + b))
because rounding can be different for negative and positive
quantities. The rounding scheme that attempts to avoid that is
"round to even" -- but if you "round to even" then if you are
given a particular double P then instead of knowing that the
original value was (P +/- epsilon), you now have to deal with
the original value potentially being (P +/- (2 * epsilon)),
which will change your analysis...
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Apr 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4312
by: Jose | last post by:
Hello: I'm trying to use a serial port to activate a leds panel playing three lines. I decided to do this way: one to send the data (RTS), another one to use it as a data clock (DTR) and another one when the data frame has finished (Tx with bit stop). It works fine, but it is not quick enough. I can send about 50 bits every millisecond using DTR and RTS, and using Windows CE it's worse. Maybe the problem is in the api...
3
2721
by: paytam | last post by:
Hi all, Is it possible to write parallel programming in C? I mean for example a simple program like I have a clock on a program that show me current time and and at the same time another job like input data from a user.If it is possible PLZ write a simple code for me ..
11
3864
by: Isaac T Alston | last post by:
Basically, I'm thinking about building a robot which can be controlled by programs which I write, I'm going to interface to the robot through the parallel port (like in this tutorial here: linuxfocus.org/English/May2001/article205.shtml). However, I know that this will probably need to be done in low level C. Now, although I can code in C, I don't particularly want to :-) , instead I'd like to use Python. I'm wondering, is there any way I...
12
8637
by: david.brown.0 | last post by:
I'm trying to make a Java program access a parallel port. Java's comm API does not provide me with the control I need. I need to be able to write to the data and control pins and read the status pins. Any Java people know a good solution? I'm trying to use JNI and create my own library, but building the library gives me these errors: ld: warning: cannot find entry symbol _start; defaulting to 0000000008048094 ParallelPort.o: In...
4
4226
by: sesling | last post by:
I currently use an SQL command line that utilizes parallel threads. ex. select /*+ PARALLEL (ACCOUNT 20) */ Account_ID, Customer_Name, Add_Date from Customer_Info. By running this query using parallel threads, it returns results in less than one minute. If I did not use the parallel threads, it would take 20 minutes or more to run. This command is run using SQL+ Is it possible to use Parallel thread processing in Access? If so, how?
4
15366
by: Soren | last post by:
Hi, I want to control some motors using the parallel port.. however, my laptop does not have any parallel ports (very few do). What I do have is a USB->Parallel converter... I thought about using PyParallel, but the USB->Parallel converter doesn't actually map to the LPT port .. and PyParallel only looks for LPT ports? Has anyone tried doing this? What are my options for controlling parallel connections on a laptop with no parallel...
0
1449
by: Jim Kennedy | last post by:
If you are firing that many queries you better be using bind variables and parsing the query once and rebinding, and executing many times and NOT closing the cursor. Doing that will help you immensely lower CPU usage. You are sending the queries sychronsly and the type of queries you are sending are not suited for doing parallel queries on (" single row from the a table and uses the primary key in its WHERE predicate"). Parallel queries...
3
2891
by: John | last post by:
I have a program that needs to run on a regular basis that looks at a queue table in my database. If there are items in the queue database I need to grab the data from the database and pass it to a web service to process. The web service will return either a completion code or error msg (the only errors expected would be timeout or cannot connection errors). This queue file can contain 0 to N messages. Naturally if there are no messages...
1
2233
by: spiralfire | last post by:
I'm writing a parser, and im totally confused. Its apparently correct, but something is happening: I do a fread command to a char array of the entire file (it isn't big), this is the file: sphere { center <-3,0,0> radius 2 }
0
10633
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.