pub struct LineSegment { /* private fields */ }Expand description
A straight, directed connection between a start and an end / stop point.
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
impl LineSegment
Sourcepub fn new(
start: [f64; 2],
stop: [f64; 2],
epsilon: f64,
max_ulps: u32,
) -> Result<Self>
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());Sourcepub fn from_start_angle_length(
start: [f64; 2],
angle: f64,
length: f64,
epsilon: f64,
max_ulps: u32,
) -> Result<Self>
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());Sourcepub fn xmin(&self) -> f64
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);Sourcepub fn xmax(&self) -> f64
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);Sourcepub fn ymin(&self) -> f64
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);Sourcepub fn ymax(&self) -> f64
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);Sourcepub fn slope(&self) -> f64
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());Sourcepub fn angle(&self) -> f64
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);Sourcepub fn euclidian_distance_to_point(&self, point: [f64; 2]) -> f64
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);Sourcepub fn length(&self) -> f64
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);Sourcepub fn length_sq(&self) -> f64
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());Sourcepub fn number_points(&self) -> usize
pub fn number_points(&self) -> usize
Returns the number of points in the segment. For a LineSegment, this is
always 2.
Sourcepub fn reverse(&mut self)
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]);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::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 pointSourcepub fn centroid(&self) -> [f64; 2]
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 segmentSourcepub 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 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());Sourcepub fn points<'a>(&'a self) -> PolygonPointsIterator<'a> ⓘ
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
impl LineSegment
Sourcepub fn draw(&self, style: &Style, context: &Context) -> Result<(), Error>
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
impl Clone for LineSegment
Source§fn clone(&self) -> LineSegment
fn clone(&self) -> LineSegment
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for LineSegment
impl Debug for LineSegment
Source§impl<'de> Deserialize<'de> for LineSegment
impl<'de> Deserialize<'de> for LineSegment
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 Display for LineSegment
impl Display for LineSegment
Source§impl From<&LineSegment> for BoundingBox
impl From<&LineSegment> for BoundingBox
Source§fn from(value: &LineSegment) -> BoundingBox
fn from(value: &LineSegment) -> BoundingBox
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 From<LineSegment> for Segment
impl From<LineSegment> for Segment
Source§fn from(value: LineSegment) -> Self
fn from(value: LineSegment) -> Self
Source§impl From<LineSegment> for SegmentChain
impl From<LineSegment> for SegmentChain
Source§fn from(value: LineSegment) -> Self
fn from(value: LineSegment) -> Self
Source§impl PartialEq for LineSegment
impl PartialEq for LineSegment
Source§impl Primitive for LineSegment
impl Primitive for LineSegment
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 Serialize for LineSegment
impl Serialize for LineSegment
Source§impl Transformation for LineSegment
impl Transformation for LineSegment
impl StructuralPartialEq for LineSegment
Auto Trait Implementations§
impl Freeze for LineSegment
impl RefUnwindSafe for LineSegment
impl Send for LineSegment
impl Sync for LineSegment
impl Unpin for LineSegment
impl UnsafeUnpin for LineSegment
impl UnwindSafe for LineSegment
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