etl/docs/blog/graphics/determining-line-line-intersections.md
John Wellbelove 4a88884b39
Issue/add hugo support for documentation (#1449)
* Add ranges

* Initial Hugo setup

* Work in progress

* Added selection for local or remote site

* Updated to 'light' theme

* Changed to using Hextra Hugo theme

* Changed to using Hextra Hugo theme

* Changed to Hextra Hugo theme

* Change to Hextra Hugo theme

* Updated Hugo setup.

* Updated Hugo setup.

# Conflicts:
#	docs/releases/_index.md

* Work in progress

* Added new fonts

Added new documentation

* Latest documentation updates

* Latest documentation updates

# Conflicts:
#	docs/containers/array.md
#	docs/containers/array_view.md
#	docs/containers/array_wrapper.md
#	docs/containers/bip_buffer_spsc_atomic.md
#	docs/containers/bitset.md
#	docs/containers/indirect_vector.md
#	docs/containers/vector.md
#	docs/getting-started/compilers.md

* Added bloom_filter markdown doc

* Added more documentation

Updated CSS for light and dark modes

* Fixed some menus

Added mode documentation files

* Updated CSS rules

Added badges to home page
Added uniqur_ptr + pool tutorial

* Fixed formatting on the home page markdown

Modified light amd dark code formatting

* Updated unique_ptr-with-pool

* Added container and shared message tutorials

* Updates to documentation

* Added const_multimap

* Updated source-formatting.md

* Added initial raw text files form Web site editor

* Innore coverage build directory

* Exported raw text documentation files from the web site editor

* Hugo updates

* Added Hugo intalation and markdown descriptions

* More addition to the documentation

* Added closure.md and updates to delegate.md

* Added format.md

* Added documentation for etl::delegate_observable, etl::function, Base64 codec

* Added io_port documentation

* Added basic_format_spec

* Added documentation for string_stream and string utilities.

* Added more documentation

Updated the documentation CSS

* Added documentation for clocks, day, duration

* Added more documentation for chrono classes

Updated callouts

* More chrono documentation

* Completed chrono documentation

* Maths functions documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Completed maths documentation

* Added multiple documentation files

* Added iterator.md

* Added debug_count.md and versions.md

* Added debug_count.md and versions.md

* Added more documentation

* More documentation

* Added some design pattern documentation

Modified some of the layout files
Modified the About documentation

* Converted more documentation pages

Modified the site CSS

* Added more documentation

Moced some documentation files to new directories

* Added more documentation

Tweaks to CSS

* Added callback_timer_deferred_locked documentation

* Added callback_timer_locked documentation

* More documentation updates

* More documentation updates

* More documentation updates

* New documentation files.

Harmonised file name format

* New documentation files.

* Multiple document updates

* Multiple document updates

* Final conversion of web pages

* Updates before PR

* Updates before PR

* Updates before PR

# Conflicts:
#	docs/blog/_index.md

* Final pre PR updates

* Updates to message framework documentation

* Renamed directory

* Fix spelling

* Added author and date to blog files

Moved documentation files merged from development

* Fixed 'Description' typo

* Fix typos

# Conflicts:
#	docs/IO/io_port.md
#	docs/containers/sets/const-multiset.md
#	docs/containers/sets/const-set.md
#	docs/maths/correlation.md
#	docs/maths/gamma.md

* Renamed two files to lower case

* Minor renaming

* Added author and date

* Updated callout on bresenham_line.md

Added support for showing the ETL version on the documentation first page, by copying the version.txt file as a hugo asset.
Updated the Python 'update_release.py' to copy 'version.txt'

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

* Replace space in filename with hyphen.

Added more information to hugo-commands.md

# Conflicts:
#	docs/getting-started/view-the-docs-locally/hugo-commands.md

* Added a link to pseudo_moving_average.md

* Updated title pages for groups

* Fixed missing 404 for non-existent pages

* Fixed coordinate variable names in the 'Calculating the intersection' example

---------

Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.co.uk>
2026-06-06 13:12:44 +01:00

3.8 KiB
Raw Blame History

title weight
Determining line-line intersections 2

Author: John Wellbelove
Date: 2019

It's quite common in graphics and image processing to want to know the intersection coordinates of two lines.

The common formula for a line is the familiar y = Mx + C.

But there is another that can be a lot easier to use when determining line to line intersections in a graphical environment.

The issues

When using y = Mx + C you must be aware of the situations of when the line approaches 'vertical'.
In this case M tends towards infinity, which is not good in a programming environment.

The usual trick is to flip the coordinates when the slope is more than 1, and then flip back after the calculations have been made. This can be confusing to follow and result in errors.

Also, to keep any accuracy, the calculations must normally be kept in the floating point domain, which is not ideal for performance, as the image will be in integral pixel coordinates.

The solution

Change the definition of your lines to use the formula Ax + By = C.

Ideally, your lines would already be in the form Ax + By = C, but this is not normally the case, but we can easily generate the parameters from two points.
Assume we have a line defined by (x1, y1) and (x2, y2).

We can deduce A, B, and C thus:

A = y2 - y1
B = x1 - x2
C = Ax1+ By1


Another useful calculation is the determinant.
Given two lines A1x + B1y = C1 and A2x + B2y = C2:

determinant = A1 * B2 - A2 * B1

If determinant is zero, then the lines are parallel.

Notes

  • If A and B are both non-zero.
    The equation represents a diagonal line.

  • If A == 0, B != 0.
    The line is the parallel to the x-axis.

  • If A != 0, B == 0.
    The line is the parallel to the y-axis.

  • If C == 0 The line passes through the origin.

Calculating the intersection

The intersection point is calculated like this:

Given two lines described by the points (x1, y1), (x2, y2), and (x3, y3), (x4, y4).

LineCross1

  • Calculate the parameters A, B, and C for each.
    A1x + B1y = C1 and A2x + B2y = C2

  • Calculate the determinant.
    determinant = A1 * B2 - A2 * B1

  • If determinant == 0 then the lines are parallel, and there is no intersection point.

  • Otherwise
    x = (B2 * C1 - B1 * C2) / determinant
    y = (A1 * C2 - A2 * C1) / determinant

They don't need to physically intersect

The intersection point can be found even if the line segments aren't actually long enough to intersect.
The calculation will effectively extend them to where they would intersect, if long enough.

This means you can find intersection points relative to a fixed reference line.

LineCross2

Refection

This describes reflecting a point across a reference line.
It uses the intersection method described above.

In the example below, we want to reflect P in the line Reference, to give us P'.

Reflection1

First, we need the reference line in the form Ax + By = C.

Next, we need to find the perpendicular from Reference through P.
Any line perpendicular to Ax + By = C takes the form Bx + Ay = D.
To find D, just substitute the x,y coordinates from P.

Now we have the two lines in the form we require to find the intersection.

Find I, which is the intersection of the reference line and the perpendicular to P.

Compute P' by using the formula P' = I + (I - P).
This calculates the vector from P to I and then adds it to I to move the same amount again.

Line to point distance

The above technique can be used to find the distance of a point to a reference line.
This distance is merely the absolute length of the vector I - P.