Skip to main content

Line

Struct Line 

Source
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: f64

Coefficient a in the line formula a*x + b*y + c = 0.

§b: f64

Coefficient b in the line formula a*x + b*y + c = 0.

§c: f64

Coefficient c in the line formula a*x + b*y + c = 0.

Implementations§

Source§

impl Line

Source

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 }.

Source

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));
Source

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));
Source

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));
Source

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 Clone for Line

Source§

fn clone(&self) -> Line

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Line

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Line

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&LineSegment> for Line

Source§

fn from(l: &LineSegment) -> Self

Converts to this type from the input type.
Source§

impl From<[[f64; 2]; 2]> for Line

Source§

fn from(pts: [[f64; 2]; 2]) -> Self

Converts to this type from the input type.
Source§

impl From<LineSegment> for Line

Source§

fn from(l: LineSegment) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Line

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Primitive for Line

Source§

fn contains_point(&self, point: [f64; 2], epsilon: f64, max_ulps: u32) -> bool

Returns true if self contains the given point and false otherwise. Read more
Source§

fn intersections_line( &self, line: &Line, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections

Returns the intersections between self and a Line. Read more
Source§

fn intersections_line_segment( &self, line_segment: &LineSegment, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections

Returns the intersections between self and a LineSegment. Read more
Source§

fn intersections_arc_segment( &self, arc_segment: &ArcSegment, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections

Returns the intersections between self and an ArcSegment. Read more
Source§

fn intersections_primitive<T: Primitive>( &self, other: &T, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections

Returns the intersection between self and another type implementing Primitive. Read more
Source§

fn intersections_point( &self, point: [f64; 2], epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections

Returns the intersections between 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 more
Source§

fn intersections_segment( &self, segment: &Segment, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections

Returns the intersection between self and a Segment. Read more
Source§

impl Serialize for Line

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Transformation for Line

Source§

fn translate(&mut self, shift: [f64; 2])

Translates self by the given shift. Read more
Source§

fn rotate(&mut self, center: [f64; 2], angle: f64)

Rotates self around the center by the given angle (in rad). Read more
Source§

fn scale(&mut self, factor: f64)

Scales self by factor with respect to the origin [0.0, 0.0]. Read more
Source§

fn line_reflection(&mut self, start: [f64; 2], stop: [f64; 2])

Mirrors self about a line defined by two points. Read more
Source§

fn point_reflection(&mut self, point: [f64; 2])

Mirrors self about a point. This operation is equivalent to a rotation around the point with the angle PI. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,