{ } JustinGeoSci

Distance and angle predicates

Four exact sign questions about distances and angles: which of two points is nearer, whether an angle is acute, and how a point’s distance to a point or a segment compares with a given length.

Comparing points

Two predicates on three [f64; 2] points, called from the crate root as arithmetica::compare_distance and arithmetica::dot_sign. Each is the sign of a degree-2 polynomial in the coordinates, and each is decided by the same sequence of stages as orient2d, inside orient2d’s exactness window (see The stages). Neither computes a distance or an angle: each compares without computing, which is why the answer can be exact.

compare_distance

pub fn compare_distance(p: [f64; 2], q1: [f64; 2], q2: [f64; 2]) -> Result<Sign, NonFinite>

Which of q1 and q2 is nearer to p: Positive if q1 is farther, Negative if it is nearer, and Zero if the two are exactly equidistant. The comparison decides a nearest-neighbor ranking exactly, including ties that the rounded squared distances cannot separate.

Distance comparison\operatorname{sign}\left(\lVert p-q_1\rVert^2 - \lVert p-q_2\rVert^2\right)

A zero filter certifies q1 == q2 as Zero by comparison alone. Every other exact tie is left to the arithmetic stages, which confirm it in exact integers.

Errors: Err(NonFinite) if any input coordinate is NaN or infinite. For every finite input the result is Ok with the exact sign.

use arithmetica::{Sign, compare_distance};

let p = [0.0, 0.0];
assert_eq!(compare_distance(p, [3.0, 0.0], [0.0, 2.0]), Ok(Sign::Positive)); // 9 > 4
assert_eq!(compare_distance(p, [3.0, 4.0], [5.0, 0.0]), Ok(Sign::Zero));     // 25 = 25

// The squared distances are 2^54 + 1 and 2^54. Both round to the same
// f64, so the rounded values cannot order the two points.
let t = 2f64.powi(27);
let (q1, q2) = ([t, 1.0], [t, 0.0]);
assert_eq!(t * t + 1.0, t * t);
assert_eq!(compare_distance(p, q1, q2), Ok(Sign::Positive)); // q1 is farther

dot_sign

pub fn dot_sign(p: [f64; 2], a: [f64; 2], b: [f64; 2]) -> Result<Sign, NonFinite>

Whether the angle a–p–b is acute, right or obtuse: Positive if acute, Zero if right (or if a or b equals p), and Negative if obtuse. Equivalently, it determines on which side of the line through p perpendicular to a − p the point b lies, which is the test that decides whether the nearest point of a segment is an endpoint.

Dot product\operatorname{sign}\left((a-p)\cdot(b-p)\right)

The dot product is the orient2d determinant with + in place of −. Written as uxvx − (−uy)vy, it is exactly the orient2d expression on four differences, so it reuses orient2d’s filter with the same constant. A zero filter certifies Zero when each product has an exactly zero factor, which covers a repeated point and an axis-aligned right angle.

Errors: Err(NonFinite) if any input coordinate is NaN or infinite. For every finite input the result is Ok with the exact sign.

use arithmetica::{Sign, dot_sign};

let p = [0.0, 0.0];
assert_eq!(dot_sign(p, [1.0, 0.0], [1.0, 1.0]), Ok(Sign::Positive));  // acute
assert_eq!(dot_sign(p, [1.0, 0.0], [0.0, 1.0]), Ok(Sign::Zero));      // right
assert_eq!(dot_sign(p, [1.0, 0.0], [-1.0, 1.0]), Ok(Sign::Negative)); // obtuse

// The exact dot product is (1 + 2^-30)(1 - 2^-30) - 1 = -2^-60.
// In f64 the first product rounds to 1, and the sum to 0.
let e = 2f64.powi(-30);
let (a, b) = ([1.0 + e, 1.0], [1.0 - e, -1.0]);
assert_eq!(a[0] * b[0] + a[1] * b[1], 0.0);           // a right angle, wrongly
assert_eq!(dot_sign(p, a, b), Ok(Sign::Negative)); // slightly obtuse

Distance thresholds

Two predicates that compare a point’s distance to a point, or to a closed segment, with a given length d. They decide within a distance exactly, and a buffer, a snapping tolerance or a proximity join needs exactly that decision. The length takes part like a coordinate: it is windowed and rescaled with the points, and only d² enters, so d and −d ask the same question.

compare_point_distance

pub fn compare_point_distance(p: [f64; 2], q: [f64; 2], d: f64) -> Result<Sign, NonFinite>

How the distance from p to q compares with |d|: Negative if p is closer, Zero if it lies at exactly that distance, and Positive if it is farther. The sign convention matches compare_distance: Positive means the first distance is the larger.

Point threshold\operatorname{sign}\left(\lVert p-q\rVert^2 - d^2\right)

A zero filter answers p == q from d == 0.0 alone: the polynomial is then −d², which is Zero for d = 0 and Negative otherwise.

Errors: Err(NonFinite) if any input coordinate, or d, is NaN or infinite. For every finite input the result is Ok with the exact sign.

use arithmetica::{Sign, compare_point_distance};

let (p, q) = ([0.0, 0.0], [3.0, 4.0]); // 5 apart
assert_eq!(compare_point_distance(p, q, 5.0), Ok(Sign::Zero));
assert_eq!(compare_point_distance(p, q, 6.0), Ok(Sign::Negative)); // within 6
assert_eq!(compare_point_distance(p, q, -4.0), Ok(Sign::Positive)); // only d² counts

// The squared distance is 2^54 + 1 against d² = 2^54, a difference that
// rounding erases.
let t = 2f64.powi(27);
assert_eq!(compare_point_distance(p, [t, 1.0], t), Ok(Sign::Positive));

compare_segment_distance

pub fn compare_segment_distance(p: [f64; 2], a: [f64; 2], b: [f64; 2], d: f64) -> Result<Sign, NonFinite>

How the distance from p to the closed segment ab compares with |d|, with the convention of compare_point_distance. A degenerate segment (a == b) is the point a.

The nearest point of the segment determines the case. It is a when (p − a)·(b − a) ≤ 0, b when (p − b)·(a − b) ≤ 0, and otherwise the foot of the perpendicular, strictly inside the segment. Two exact dot_sign calls choose the case, and that choice is the only branch, so the predicate as a whole is exact. The two endpoint cases are compare_point_distance. In the interior case, with u = b − a and v = p − a, the squared distance is a quotient, and multiplying through by |u|² > 0 leaves a degree-4 polynomial with no division:

Interior case\operatorname{sign}\left((u \times v)^2 - d^2\,\lVert u\rVert^2\right)

The interior case runs inside incircle’s window, the window for degree 4, and its exact integer stage is 8,448 bits wide.

Errors: Err(NonFinite) if any input coordinate, or d, is NaN or infinite. For every finite input the result is Ok with the exact sign.

use arithmetica::{Sign, compare_segment_distance};

let (a, b) = ([0.0, 0.0], [10.0, 0.0]);

// Interior case: the nearest point is (5, 0), at distance 3.
assert_eq!(compare_segment_distance([5.0, 3.0], a, b, 3.0), Ok(Sign::Zero));
assert_eq!(compare_segment_distance([5.0, 3.0], a, b, 4.0), Ok(Sign::Negative));

// Endpoint case: the nearest point is a, at distance 5.
assert_eq!(compare_segment_distance([-3.0, 4.0], a, b, 5.0), Ok(Sign::Zero));

// A degenerate segment is a point.
assert_eq!(compare_segment_distance([3.0, 4.0], a, a, 5.0), Ok(Sign::Zero));