Skip to main content

Segment

Enum Segment 

Source
pub enum Segment {
    LineSegment(LineSegment),
    ArcSegment(ArcSegment),
}
Expand description

A directed connection between a start and an end point, which can take different paths depending on the variant. All methods of Segment delegate to the corresponding function of the underlying variant.

Segments example

See the module-level docstring for more.

§Serialization and deserialization

When the serde feature is enabled, segments can be serialized and deserialized using the serde crate. The enum is treated as untagged, meaning that the serialized representation of a segment is identical to that of the segment variant it contains (LineSegment or ArcSegment). See serdes documentation for more.

Variants§

§

LineSegment(LineSegment)

A straight line segment between a start and an end point. See the docstring of LineSegment.

§

ArcSegment(ArcSegment)

An arc segment between a start and an end point. See the docstring of ArcSegment.

Implementations§

Source§

impl Segment

Source

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

Returns the start point of the underlying segment variant.

Source

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

Returns the end / stop point of the underlying segment variant.

Source

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

Returns the end / stop point of the underlying segment variant. This is an alias for Segment::end.

Source

pub fn number_points(&self) -> usize

Returns the number of points of the underlying variant.

Source

pub fn length(&self) -> f64

Returns the length of the underlying line / arc segment.

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

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

let arc = ArcSegment::from_center_radius_start_offset_angle(
        [0.0, 0.0],
        2.0,
        0.0,
        PI,
        DEFAULT_EPSILON,
        DEFAULT_MAX_ULPS,
    )
    .unwrap();
approx::assert_abs_diff_eq!(arc.length(), 2.0 * PI);
let s = Segment::from(arc);
approx::assert_abs_diff_eq!(s.length(), 2.0 * PI);
Source

pub fn reverse(&mut self)

Reverses the underlying segment, i.e. switching the segment direction.

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::prelude::*;
let ls = LineSegment::new([0.0, 0.0], [0.0, 2.0], 0.0, 0).unwrap();
let s = Segment::from(ls);

assert_eq!(s.segment_point(0.0), s.start());
assert_eq!(s.segment_point(-10.0), s.start());

assert_eq!(s.segment_point(1.0), s.stop());
assert_eq!(s.segment_point(10.0), s.stop());

assert_eq!(s.segment_point(0.5), [0.0, 1.0]);
Source

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

Returns the points of a polygon polysegment 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 std::f64::consts::FRAC_PI_2;
use planar_geo::prelude::*;

// Approximate an arc segment from 0 to 90 degrees by four straight
// segments (five points). The returned points are regularily distributed
// over the arc.
let arc = ArcSegment::from_center_radius_start_offset_angle(
        [0.0, 0.0],
        1.0,
        0.0,
        FRAC_PI_2,
        DEFAULT_EPSILON,
        DEFAULT_MAX_ULPS,
    )
    .unwrap();
let s = Segment::from(arc);
let mut iter = s.polygonize(SegmentPolygonizer::InnerSegments(4));

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

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

Returns the centroid (center of mass) of the segment.

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

let arc = ArcSegment::from_center_radius_start_offset_angle(
        [0.0, 0.0],
        2.0,
        0.0,
        PI,
        DEFAULT_EPSILON,
        DEFAULT_MAX_ULPS,
    )
    .unwrap();
let s = Segment::from(arc);
approx::assert_abs_diff_eq!(s.centroid(), [0.0, 0.8488263631567751]);
Source

pub fn invert(&mut self)

Switches start and end / stop points of self.

§Examples
use planar_geo::prelude::*;

let mut s: Segment = LineSegment::new([0.0, 0.0], [2.0, 0.0], 0.0, 0).unwrap().into();
assert_eq!(s.start(), [0.0, 0.0]);
assert_eq!(s.stop(), [2.0, 0.0]);

s.invert();
assert_eq!(s.start(), [2.0, 0.0]);
assert_eq!(s.stop(), [0.0, 0.0]);

s.invert();
assert_eq!(s.start(), [0.0, 0.0]);
assert_eq!(s.stop(), [2.0, 0.0]);
Source

pub fn touches_segment<'a, T: Into<SegmentRef<'a>>>( &self, other: T, epsilon: f64, max_ulps: u32, ) -> bool

Returns whether self and other are touching.

Two segments are touching if they are intersecting but not dividing each other. Depending on the type of self, this function forwards to LineSegment::touches_segment or ArcSegment::touches_segment, see their respective docstrings for further explanation and examples.

Source§

impl Segment

Source

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

Draws the Segment onto the cairo::Context with the given Style.

See the module level documentation for more.

Trait Implementations§

Source§

impl Clone for Segment

Source§

fn clone(&self) -> Segment

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 Segment

Source§

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

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

impl<'de> Deserialize<'de> for Segment

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<'a> From<&'a Segment> for GeometryCow<'a>

Source§

fn from(value: &'a Segment) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a Segment> for GeometryRef<'a>

Source§

fn from(value: &'a Segment) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a Segment> for SegmentRef<'a>

Source§

fn from(value: &'a Segment) -> Self

Converts to this type from the input type.
Source§

impl From<ArcSegment> for Segment

Source§

fn from(value: ArcSegment) -> 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<Segment> for Contour

Source§

fn from(value: Segment) -> Self

Converts to this type from the input type.
Source§

impl From<Segment> for Geometry

Source§

fn from(value: Segment) -> Self

Converts to this type from the input type.
Source§

impl From<Segment> for GeometryCow<'_>

Source§

fn from(value: Segment) -> Self

Converts to this type from the input type.
Source§

impl From<Segment> for Polysegment

Source§

fn from(value: Segment) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Segment> for Polysegment

Source§

fn from_iter<T: IntoIterator<Item = Segment>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl PartialEq for Segment

Source§

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

Source§

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

Returns true if self covers the given point. Read more
Source§

fn covers_arc_segment( &self, arc_segment: &ArcSegment, epsilon: f64, max_ulps: u32, ) -> bool

Returns if self covers the given ArcSegment. Read more
Source§

fn covers_line_segment( &self, line_segment: &LineSegment, epsilon: f64, max_ulps: u32, ) -> bool

Returns if self covers the given LineSegment. Read more
Source§

fn covers_line(&self, _line: &Line, _epsilon: f64, _max_ulps: u32) -> bool

Returns if self covers the given Line. 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 covers<'a, T>(&self, other: T, epsilon: f64, max_ulps: u32) -> bool
where Self: Sized, for<'b> &'b Self: Into<GeometryRef<'b>>, T: Into<GeometryRef<'a>>,

Returns if self covers other (which can be any geometric type). 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] Read more
Source§

fn intersections_segment<'a, T>( &self, segment: T, epsilon: f64, max_ulps: u32, ) -> PrimitiveIntersections
where T: Into<SegmentRef<'a>>,

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

fn intersections<'a, T>( &self, other: T, epsilon: f64, max_ulps: u32, ) -> Vec<Intersection>
where Self: Sized, T: Into<GeometryRef<'a>>,

Returns the intersections between a Primitive and any other geometric type. Read more
Source§

impl Serialize for Segment

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 ToBoundingBox for Segment

Source§

fn bounding_box(&self) -> BoundingBox

Returns a bounding box for the implementor. Read more
Source§

impl Transformation for Segment

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 Segment

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> 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>,