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.
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
impl Segment
Sourcepub fn stop(&self) -> [f64; 2]
pub fn stop(&self) -> [f64; 2]
Returns the end / stop point of the underlying segment variant. This is an
alias for Segment::end.
Sourcepub fn number_points(&self) -> usize
pub fn number_points(&self) -> usize
Returns the number of points of the underlying variant.
Sourcepub fn length(&self) -> f64
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);Sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the underlying segment, i.e. switching the segment direction.
Sourcepub fn segment_point(&self, normalized: f64) -> [f64; 2]
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]);Sourcepub fn polygonize<'a>(
&'a self,
polygonizer: SegmentPolygonizer,
) -> PolygonPointsIterator<'a> ⓘ
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());Sourcepub fn centroid(&self) -> [f64; 2]
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]);Sourcepub fn invert(&mut self)
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]);Sourcepub fn touches_segment<'a, T: Into<SegmentRef<'a>>>(
&self,
other: T,
epsilon: f64,
max_ulps: u32,
) -> bool
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.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Segment
impl<'de> Deserialize<'de> for Segment
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<'a> From<&'a Segment> for GeometryCow<'a>
impl<'a> From<&'a Segment> for GeometryCow<'a>
Source§impl<'a> From<&'a Segment> for GeometryRef<'a>
impl<'a> From<&'a Segment> for GeometryRef<'a>
Source§impl<'a> From<&'a Segment> for SegmentRef<'a>
impl<'a> From<&'a Segment> for SegmentRef<'a>
Source§impl From<ArcSegment> for Segment
impl From<ArcSegment> for Segment
Source§fn from(value: ArcSegment) -> Self
fn from(value: ArcSegment) -> Self
Source§impl From<LineSegment> for Segment
impl From<LineSegment> for Segment
Source§fn from(value: LineSegment) -> Self
fn from(value: LineSegment) -> Self
Source§impl From<Segment> for GeometryCow<'_>
impl From<Segment> for GeometryCow<'_>
Source§impl From<Segment> for Polysegment
impl From<Segment> for Polysegment
Source§impl FromIterator<Segment> for Polysegment
impl FromIterator<Segment> for Polysegment
Source§impl Primitive for Segment
impl Primitive for Segment
Source§fn covers_arc_segment(
&self,
arc_segment: &ArcSegment,
epsilon: f64,
max_ulps: u32,
) -> bool
fn covers_arc_segment( &self, arc_segment: &ArcSegment, epsilon: f64, max_ulps: u32, ) -> bool
Source§fn covers_line_segment(
&self,
line_segment: &LineSegment,
epsilon: f64,
max_ulps: u32,
) -> bool
fn covers_line_segment( &self, line_segment: &LineSegment, epsilon: f64, max_ulps: u32, ) -> bool
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
Source§fn intersections_segment<'a, T>(
&self,
segment: T,
epsilon: f64,
max_ulps: u32,
) -> PrimitiveIntersectionswhere
T: Into<SegmentRef<'a>>,
fn intersections_segment<'a, T>(
&self,
segment: T,
epsilon: f64,
max_ulps: u32,
) -> PrimitiveIntersectionswhere
T: Into<SegmentRef<'a>>,
Source§fn intersections<'a, T>(
&self,
other: T,
epsilon: f64,
max_ulps: u32,
) -> Vec<Intersection>
fn intersections<'a, T>( &self, other: T, epsilon: f64, max_ulps: u32, ) -> Vec<Intersection>
Source§impl ToBoundingBox for Segment
impl ToBoundingBox for Segment
Source§fn bounding_box(&self) -> BoundingBox
fn bounding_box(&self) -> BoundingBox
Source§impl Transformation for Segment
impl Transformation for Segment
impl StructuralPartialEq for Segment
Auto Trait Implementations§
impl Freeze for Segment
impl RefUnwindSafe for Segment
impl Send for Segment
impl Sync for Segment
impl Unpin for Segment
impl UnsafeUnpin for Segment
impl UnwindSafe for Segment
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