Skip to main content

LineSegment

Struct LineSegment 

Source
pub struct LineSegment { /* private fields */ }
Expand description

A straight, directed connection between a start and an end / stop point.

Line segment example

By definition, start and end point must not be identical, because otherwise the line segment would degenerate to a point. The finite length defined by those two points is the main difference between this type and the Line struct. It is trivial to convert a LineSegment to a Line via the corresponding From / Into implementations, however a direct conversion in the other direction is not possible because the start and end point information is lost.

§Serialization and deserialization

When the serde feature is enabled, line segments can be serialized and deserialized using the serde crate.

Implementations§

Source§

impl LineSegment

Source

pub fn new( start: [f64; 2], stop: [f64; 2], epsilon: f64, max_ulps: u32, ) -> Result<Self>

Creates a new LineSegment instance if the start and stop are not equal (within the tolerances defined by epsilon and max_ulps).

§Examples
use planar_geo::segment::LineSegment;

// Successfull construction
assert!(LineSegment::new([0.0, 0.0], [1.0, 0.0], 0.0, 0).is_ok());

// Points are identical
assert!(LineSegment::new([0.0, 0.0], [0.0, 0.0], 0.0, 0).is_err());

// Points are identical within the defined tolerance
assert!(LineSegment::new([0.0, 0.0], [1.0, 0.0], 1.0, 0).is_err());
Source

pub fn from_start_angle_length( start: [f64; 2], angle: f64, length: f64, epsilon: f64, max_ulps: u32, ) -> Result<Self>

Creates a LineSegment from a start point, an angle and its length. If the length is zero, the points are identical and the construction fails.

§Examples
use planar_geo::segment::LineSegment;

// Successfull construction
assert!(LineSegment::from_start_angle_length([0.0, 0.0], 0.0, 1.0, 0.0, 0).is_ok());
assert!(LineSegment::from_start_angle_length([0.0, 0.0], 0.0, -1.0, 0.0, 0).is_ok());

// Points are identical
assert!(LineSegment::from_start_angle_length([0.0, 0.0], 0.0, 0.0, 0.0, 0).is_err());

// Points are identical within the defined tolerance
assert!(LineSegment::from_start_angle_length([0.0, 0.0], 0.0, 1.0, 1.0, 0).is_err());
Source

pub fn xmin(&self) -> f64

Returns the smallest x-value of self.

§Examples
use planar_geo::segment::LineSegment;

// Successfull construction
let ls = LineSegment::new([0.0, 0.0], [1.0, -1.0], 0.0, 0).expect("points not identical");
assert_eq!(ls.xmin(), 0.0);
Source

pub fn xmax(&self) -> f64

Returns the largest x-value of self.

§Examples
use planar_geo::segment::LineSegment;

// Successfull construction
let ls = LineSegment::new([0.0, 0.0], [1.0, -1.0], 0.0, 0).expect("points not identical");
assert_eq!(ls.xmax(), 1.0);
Source

pub fn ymin(&self) -> f64

Returns the smallest y-value of self.

§Examples
use planar_geo::segment::LineSegment;

// Successfull construction
let ls = LineSegment::new([0.0, 0.0], [1.0, -1.0], 0.0, 0).expect("points not identical");
assert_eq!(ls.ymin(), -1.0);
Source

pub fn ymax(&self) -> f64

Returns the largest y-value of self.

§Examples
use planar_geo::segment::LineSegment;

// Successfull construction
let ls = LineSegment::new([0.0, 0.0], [1.0, -1.0], 0.0, 0).expect("points not identical");
assert_eq!(ls.ymax(), 0.0);
Source

pub fn slope(&self) -> f64

Returns the slope of self.

§Examples
use planar_geo::segment::LineSegment;

// 45° slope
let line = LineSegment::new([0.0, 0.0], [1.0, -1.0], 0.0, 0).unwrap();
assert_eq!(line.slope(), -1.0);

// Infinite slope (vertical line)
let line = LineSegment::new([0.0, 0.0], [0.0, -1.0], 0.0, 0).unwrap();
assert!(line.slope().is_infinite());
Source

pub fn angle(&self) -> f64

Returns the angle of self from LineSegment::start to LineSegment::stop.

use planar_geo::prelude::*;
use std::f64::consts::PI;

// 45°
let line = LineSegment::new([0.0, 0.0], [1.0, 1.0], DEFAULT_EPSILON,
                            DEFAULT_MAX_ULPS).unwrap();
approx::assert_abs_diff_eq!(line.angle(), 0.25 * PI);

// 180°
let line = LineSegment::new([1.0, 1.0], [-1.0, 1.0], DEFAULT_EPSILON,
                            DEFAULT_MAX_ULPS).unwrap();
approx::assert_abs_diff_eq!(line.angle(), PI);

// 225°
let line = LineSegment::new([-2.0, -8.0], [-4.0, -10.0], DEFAULT_EPSILON,
                            DEFAULT_MAX_ULPS).unwrap();
approx::assert_abs_diff_eq!(line.angle(), -0.75 * PI);

// 315°
let line = LineSegment::new([5.0, 0.0], [6.0, -1.0], DEFAULT_EPSILON,
                            DEFAULT_MAX_ULPS).unwrap();
approx::assert_abs_diff_eq!(line.angle(), -0.25 * PI);
Source

pub fn euclidian_distance_to_point(&self, point: [f64; 2]) -> f64

Returns the euclidian distance of the point to self. The euclidian distance is the length of the shortest line which can be drawn between the point and any point on self.

§Examples
use planar_geo::prelude::*;

let line = LineSegment::new([0.0, 0.0], [1.0, -1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap();
assert_eq!(line.euclidian_distance_to_point([-1.0, 0.0]), 1.0);
approx::assert_abs_diff_eq!(line.euclidian_distance_to_point([2.0, 0.0]), 2.0f64.sqrt());

// Point is on the line
assert_eq!(line.euclidian_distance_to_point([0.5, -0.5]), 0.0);
Source

pub fn length(&self) -> f64

Returns the length of self calculated with the Pythagorean theorem.

§Examples
use planar_geo::segment::LineSegment;

let ls = LineSegment::new([0.0, 0.0], [-2.0, 0.0], 0.0, 0).unwrap();
assert_eq!(ls.length(), 2.0);
Source

pub fn length_sq(&self) -> f64

Returns the squared length of self calculated with the Pythagorean theorem.

This function is more efficient / faster than LineSegment::length, since it does not require the expensive square root function. For example, if the longer between two LineSegments should be found, using this function is the preferred way to do it:

use planar_geo::segment::LineSegment;

let ls1 = LineSegment::new([0.0, 0.0], [1.0, 0.0], 0.0, 0).unwrap();
let ls2 = LineSegment::new([0.0, 0.0], [2.0, 0.0], 0.0, 0).unwrap();

assert_eq!(ls1.length_sq() > ls2.length_sq(), ls1.length() > ls2.length());
Source

pub fn start(&self) -> [f64; 2]

Returns the start point of self.

Source

pub fn stop(&self) -> [f64; 2]

Returns the stop point of self.

Source

pub fn number_points(&self) -> usize

Returns the number of points in the segment. For a LineSegment, this is always 2.

Source

pub fn reverse(&mut self)

Reverses the direction of self - i.e., exchange its start and stop point.

use planar_geo::segment::LineSegment;

let mut ls = LineSegment::new([0.0, 0.0], [1.0, 0.0], 0.0, 0).unwrap();

assert_eq!(ls.start(), [0.0, 0.0]);
assert_eq!(ls.stop(), [1.0, 0.0]);

ls.reverse();

assert_eq!(ls.start(), [1.0, 0.0]);
assert_eq!(ls.stop(), [0.0, 0.0]);
Source

pub fn segment_point(&self, normalized: f64) -> [f64; 2]

Returns a point on the segment defined by its normalized position on it.

For example, normalized = 0 returns the start point, normalized = 1 returns the end point and normalized = 0.5 returns the middle point of the segment. The input normalized is clamped to [0, 1].

§Examples
use planar_geo::segment::LineSegment;

let line = LineSegment::new([0.0, 0.0], [2.0, 0.0], 0.0, 0).unwrap();

// Middle point of the segment
assert_eq!(line.segment_point(0.5), [1.0, 0.0]);
assert_eq!(line.segment_point(-1.0), [0.0, 0.0]); // Start point
assert_eq!(line.segment_point(1.5), [2.0, 0.0]); // Stop point
Source

pub fn centroid(&self) -> [f64; 2]

Returns the centroid / center of mass of self. In case of the LineSegment, this is equal to the middle of the segment.

§Examples
use planar_geo::segment::LineSegment;

let line = LineSegment::new([0.0, 0.0], [2.0, 0.0], 0.0, 0).unwrap();

assert_eq!(line.centroid(), [1.0, 0.0]);
assert_eq!(line.centroid(), line.segment_point(0.5)); // Middle of the segment
Source

pub fn polygonize<'a>( &'a self, polygonizer: SegmentPolygonizer, ) -> PolygonPointsIterator<'a>

Returns the points of a polygon chain which approximates self. The number of points is defined by the SegmentPolygonizer (see its docstring). The points are regularily distributed over the segment, which means that two subsequent points always have the same euclidian distance from each other.

§Examples
use planar_geo::prelude::*;

let line = LineSegment::new([0.0, 0.0], [2.0, 0.0], 0.0, 0).unwrap();

let mut iter = line.polygonize(SegmentPolygonizer::InnerSegments(4));

assert_eq!(iter.next(), Some(line.segment_point(0.0)));
assert_eq!(iter.next(), Some(line.segment_point(0.25)));
assert_eq!(iter.next(), Some(line.segment_point(0.5)));
assert_eq!(iter.next(), Some(line.segment_point(0.75)));
assert_eq!(iter.next(), Some(line.segment_point(1.0)));
assert!(iter.next().is_none());
Source

pub fn points<'a>(&'a self) -> PolygonPointsIterator<'a>

Returns an iterator over the start and stop point of self

This is a shorthand for self.polygonize( Polygonizer::InnerSegments(1)).

§Examples
use planar_geo::segment::LineSegment;

let line = LineSegment::new([0.0, 0.0], [2.0, 0.0], 0.0, 0).unwrap();

let mut iter = line.points();

assert_eq!(iter.next(), Some(line.segment_point(0.0)));
assert_eq!(iter.next(), Some(line.segment_point(1.0)));
assert!(iter.next().is_none());
Source§

impl LineSegment

Source

pub fn draw(&self, style: &Style, context: &Context) -> Result<(), Error>

Draws the LineSegment onto the cairo::Context with the given Style. See the module level documentation.

Trait Implementations§

Source§

impl Clone for LineSegment

Source§

fn clone(&self) -> LineSegment

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 LineSegment

Source§

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

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

impl<'de> Deserialize<'de> for LineSegment

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 Display for LineSegment

Source§

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

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

impl From<&LineSegment> for BoundingBox

Source§

fn from(value: &LineSegment) -> BoundingBox

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 From<LineSegment> for Line

Source§

fn from(l: LineSegment) -> Self

Converts to this type from the input type.
Source§

impl From<LineSegment> for Segment

Source§

fn from(value: LineSegment) -> Self

Converts to this type from the input type.
Source§

impl From<LineSegment> for SegmentChain

Source§

fn from(value: LineSegment) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for LineSegment

Source§

fn eq(&self, other: &LineSegment) -> 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 LineSegment

Source§

fn contains_point(&self, p: [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 LineSegment

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 LineSegment

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 StructuralPartialEq for LineSegment

Auto Trait Implementations§

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> ToBoundingBox for T
where &'a T: for<'a> Into<BoundingBox>,

Source§

fn bounding_box(&self) -> BoundingBox

Returns a bounding box for the implementor. 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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>,