pub struct Line {
pub a: f64,
pub b: f64,
pub c: f64,
}Expand description
A mathematical Line which has an infinite length, but no width.
A line can be represented by the equation a*x + b*y + c = 0. The three fields
Line::a, Line::b and Line::c correspond to these coefficients.
The main purpose of this type is being a tool for calculations. For example,
the intersection of two LineSegments (straight
connections between two points of usually finite length) can be calculated by
first calculating the intersection of the corresponding infinite lines and then
by checking whether the found intersection point is actually located on the
segments. Another example is the ray casting algorithm, which is used in the
contains_point implementation
for Shapes.
Obviously, a Line object can be directly created by providing its three
coefficients. Additionally, it is also possible to derive a Line from a
point it goes through and its angle (Line::from_point_angle) and from a
two-point representation (Line::from_two_points).
Because the Line is closely related to the
LineSegment, a From implementation exists.
The crate docstring describes the relationship between the
Line and the other geometric types provided by this crate.
§Serialization and deserialization
This struct can be serialized and deserialized from its three fields
Line::a, Line::b and Line::c using the serde crate if the
serde feature is enabled.
Fields§
§a: f64Coefficient a in the line formula a*x + b*y + c = 0.
b: f64Coefficient b in the line formula a*x + b*y + c = 0.
c: f64Coefficient c in the line formula a*x + b*y + c = 0.
Implementations§
Source§impl Line
impl Line
Sourcepub fn new(a: f64, b: f64, c: f64) -> Self
pub fn new(a: f64, b: f64, c: f64) -> Self
Creates a Line from its three coefficients. This is an alias for using
the literal struct construction syntax Line { a, b, c }.
Sourcepub fn from_point_angle(pt: [f64; 2], angle: f64) -> Self
pub fn from_point_angle(pt: [f64; 2], angle: f64) -> Self
Creates a Line from a point it goes through and its angle (relative to
the x / horizontal axis).
§Examples
use std::f64::consts::PI;
use planar_geo::prelude::*;
// A line with a 45° angle going through [2.0, 0.0].
let line = Line::from_point_angle([2.0, 0.0], 0.25 * PI);
assert!(line.contains_point([1.0, -1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
assert!(line.contains_point([2.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
assert!(line.contains_point([3.0, 1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS));Sourcepub fn from_two_points(
pt1: [f64; 2],
pt2: [f64; 2],
epsilon: f64,
max_ulps: u32,
) -> Result<Self, Error>
pub fn from_two_points( pt1: [f64; 2], pt2: [f64; 2], epsilon: f64, max_ulps: u32, ) -> Result<Self, Error>
Creates a Line from two points. This constructor fails if the points are
identical (which is checked with approx::assert_ulps_eq, using the
arguments epsilon and max_ulps for defining the absolute and ULPs
tolerance respectively).
§Examples
use planar_geo::prelude::*;
// A line with a 45° angle going through [2.0, 0.0].
let line = Line::from_two_points([2.0, 0.0], [3.0, 1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).expect("points not identical");
assert!(line.contains_point([1.0, -1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
assert!(line.contains_point([2.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
assert!(line.contains_point([3.0, 1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS));Sourcepub fn parallel(&self, other: &Self, epsilon: f64, max_ulps: u32) -> bool
pub fn parallel(&self, other: &Self, epsilon: f64, max_ulps: u32) -> bool
Returns true if the two given lines are parallel within the tolerance band
defined by the absolute tolerance epsilon and the ULPs tolerance
max_ulps.
§Examples
use planar_geo::prelude::*;
let line_1 = Line::from_point_angle([2.0, 0.0], 1.0);
let line_2 = Line::from_point_angle([2.0, 1.0], 1.0);
let line_3 = Line::from_point_angle([2.0, 0.0], -1.0);
assert!(line_1.parallel(&line_2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
assert!(!line_1.parallel(&line_3, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
assert!(!line_2.parallel(&line_3, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));Sourcepub fn identical(&self, other: &Self, epsilon: f64, max_ulps: u32) -> bool
pub fn identical(&self, other: &Self, epsilon: f64, max_ulps: u32) -> bool
Returns true if the two given lines are identical within the tolerance
band defined by the absolute tolerance epsilon and the ULPs tolerance
max_ulps.
§Examples
use planar_geo::prelude::*;
let line_1 = Line::from_point_angle([2.0, 0.0], 0.0);
let line_2 = Line::from_point_angle([-3.0, 0.0], 0.0);
let line_3 = Line::from_point_angle([2.0, 1.0], 0.0);
assert!(line_1.identical(&line_2, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
assert!(!line_1.identical(&line_3, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));
assert!(!line_2.identical(&line_3, DEFAULT_EPSILON, DEFAULT_MAX_ULPS));Trait Implementations§
Source§impl<'de> Deserialize<'de> for Line
impl<'de> Deserialize<'de> for Line
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<&LineSegment> for Line
impl From<&LineSegment> for Line
Source§fn from(l: &LineSegment) -> Self
fn from(l: &LineSegment) -> Self
Source§impl From<LineSegment> for Line
impl From<LineSegment> for Line
Source§fn from(l: LineSegment) -> Self
fn from(l: LineSegment) -> Self
Source§impl Primitive for Line
impl Primitive for Line
Source§fn intersections_line(
&self,
line: &Line,
epsilon: f64,
max_ulps: u32,
) -> PrimitiveIntersections
fn intersections_line( &self, line: &Line, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections
Source§fn intersections_line_segment(
&self,
line_segment: &LineSegment,
epsilon: f64,
max_ulps: u32,
) -> PrimitiveIntersections
fn intersections_line_segment( &self, line_segment: &LineSegment, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections
Source§fn intersections_arc_segment(
&self,
arc_segment: &ArcSegment,
epsilon: f64,
max_ulps: u32,
) -> PrimitiveIntersections
fn intersections_arc_segment( &self, arc_segment: &ArcSegment, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections
Source§fn intersections_primitive<T: Primitive>(
&self,
other: &T,
epsilon: f64,
max_ulps: u32,
) -> PrimitiveIntersections
fn intersections_primitive<T: Primitive>( &self, other: &T, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections
Source§fn intersections_point(
&self,
point: [f64; 2],
epsilon: f64,
max_ulps: u32,
) -> PrimitiveIntersections
fn intersections_point( &self, point: [f64; 2], epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections
self and a point [f64; 2]
*
This function wraps the given point in PrimitiveIntersections::One if
Primitive::contains_point returned true and returns
PrimitiveIntersections::Zero otherwise. Read moreSource§fn intersections_segment(
&self,
segment: &Segment,
epsilon: f64,
max_ulps: u32,
) -> PrimitiveIntersections
fn intersections_segment( &self, segment: &Segment, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections
Source§impl Transformation for Line
impl Transformation for Line
impl Copy for Line
Auto Trait Implementations§
impl Freeze for Line
impl RefUnwindSafe for Line
impl Send for Line
impl Sync for Line
impl Unpin for Line
impl UnsafeUnpin for Line
impl UnwindSafe for Line
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more