@@ -641,6 +641,93 @@ impl Rect {
641641 self . x0 . is_nan ( ) || self . y0 . is_nan ( ) || self . x1 . is_nan ( ) || self . y1 . is_nan ( )
642642 }
643643
644+ /// Return the extent of this rectangle on the given axis.
645+ ///
646+ /// This is equivalent to [`Rect::width`] for [`Axis::Horizontal`] and
647+ /// [`Rect::height`] for [`Axis::Vertical`].
648+ ///
649+ /// Note: nothing forbids negative width or height.
650+ ///
651+ /// # Examples
652+ ///
653+ /// ```
654+ /// use kurbo::{Axis, Rect};
655+ ///
656+ /// let rect = Rect::new(1.0, 2.0, 4.0, 7.0);
657+ /// assert_eq!(rect.extent(Axis::Horizontal), 3.0);
658+ /// assert_eq!(rect.extent(Axis::Vertical), 5.0);
659+ /// ```
660+ #[ inline]
661+ pub const fn extent ( & self , axis : Axis ) -> f64 {
662+ match axis {
663+ Axis :: Horizontal => self . width ( ) ,
664+ Axis :: Vertical => self . height ( ) ,
665+ }
666+ }
667+
668+ /// Return the minimum coordinate on the given axis.
669+ ///
670+ /// This returns the lesser endpoint even if this rectangle has negative
671+ /// width or height.
672+ ///
673+ /// # Examples
674+ ///
675+ /// ```
676+ /// use kurbo::{Axis, Rect};
677+ ///
678+ /// let rect = Rect::new(8.0, 2.0, 4.0, 7.0);
679+ /// assert_eq!(rect.min_coord(Axis::Horizontal), 4.0);
680+ /// assert_eq!(rect.min_coord(Axis::Vertical), 2.0);
681+ /// ```
682+ #[ inline]
683+ pub const fn min_coord ( & self , axis : Axis ) -> f64 {
684+ match axis {
685+ Axis :: Horizontal => self . min_x ( ) ,
686+ Axis :: Vertical => self . min_y ( ) ,
687+ }
688+ }
689+
690+ /// Return the maximum coordinate on the given axis.
691+ ///
692+ /// This returns the greater endpoint even if this rectangle has negative
693+ /// width or height.
694+ ///
695+ /// # Examples
696+ ///
697+ /// ```
698+ /// use kurbo::{Axis, Rect};
699+ ///
700+ /// let rect = Rect::new(8.0, 2.0, 4.0, 7.0);
701+ /// assert_eq!(rect.max_coord(Axis::Horizontal), 8.0);
702+ /// assert_eq!(rect.max_coord(Axis::Vertical), 7.0);
703+ /// ```
704+ #[ inline]
705+ pub const fn max_coord ( & self , axis : Axis ) -> f64 {
706+ match axis {
707+ Axis :: Horizontal => self . max_x ( ) ,
708+ Axis :: Vertical => self . max_y ( ) ,
709+ }
710+ }
711+
712+ /// Return the center coordinate on the given axis.
713+ ///
714+ /// # Examples
715+ ///
716+ /// ```
717+ /// use kurbo::{Axis, Rect};
718+ ///
719+ /// let rect = Rect::new(1.0, 2.0, 4.0, 8.0);
720+ /// assert_eq!(rect.center_coord(Axis::Horizontal), 2.5);
721+ /// assert_eq!(rect.center_coord(Axis::Vertical), 5.0);
722+ /// ```
723+ #[ inline]
724+ pub const fn center_coord ( & self , axis : Axis ) -> f64 {
725+ match axis {
726+ Axis :: Horizontal => 0.5 * ( self . x0 + self . x1 ) ,
727+ Axis :: Vertical => 0.5 * ( self . y0 + self . y1 ) ,
728+ }
729+ }
730+
644731 /// Get the members matching the given axis.
645732 #[ inline]
646733 pub const fn get_coords ( self , axis : Axis ) -> ( f64 , f64 ) {
@@ -667,6 +754,32 @@ impl Rect {
667754 Axis :: Vertical => ( self . y0 , self . y1 ) = ( v0, v1) ,
668755 }
669756 }
757+
758+ /// Return a new `Rect` with the members matching the given axis set to
759+ /// the given values.
760+ ///
761+ /// This sets the raw endpoints for the axis and does not reorder or
762+ /// normalize them.
763+ ///
764+ /// # Examples
765+ ///
766+ /// ```
767+ /// use kurbo::{Axis, Rect};
768+ ///
769+ /// let rect = Rect::new(1.0, 2.0, 4.0, 7.0).with_coords(
770+ /// Axis::Horizontal,
771+ /// 10.0,
772+ /// 3.0,
773+ /// );
774+ /// assert_eq!(rect, Rect::new(10.0, 2.0, 3.0, 7.0));
775+ /// ```
776+ #[ inline]
777+ pub const fn with_coords ( self , axis : Axis , v0 : f64 , v1 : f64 ) -> Rect {
778+ match axis {
779+ Axis :: Horizontal => Rect :: new ( v0, self . y0 , v1, self . y1 ) ,
780+ Axis :: Vertical => Rect :: new ( self . x0 , v0, self . x1 , v1) ,
781+ }
782+ }
670783}
671784
672785impl From < ( Point , Point ) > for Rect {
0 commit comments