pub fn constrain_aspect_ratio(&self, aspect_ratio: f64, width: f64) -> Size {
        // Minimizing/maximizing based on aspect ratio seems complicated, but in reality everything
        // is linear, so the amount of work to do is low.
        let ideal_size = Size {
            width,
            height: width * aspect_ratio,
        };

        // It may be possible to remove these in the future if the invariant is checked elsewhere.
        let aspect_ratio = aspect_ratio.abs();
        let width = width.abs();

        // Firstly check if we can simply return the exact requested
        if self.contains(ideal_size) {
            return ideal_size;
        }

        // Then we check if any `Size`s with our desired aspect ratio are inside the constraints.
        // TODO this currently outputs garbage when things are < 0.
        let min_w_min_h = self.min.height / self.min.width;
        let max_w_min_h = self.min.height / self.max.width;
        let min_w_max_h = self.max.height / self.min.width;
        let max_w_max_h = self.max.height / self.max.width;

        // When the aspect ratio line crosses the constraints, the closest point must be one of the
        // two points where the aspect ratio enters/exits.

        // When the aspect ratio line doesn't intersect the box of possible sizes, the closest
        // point must be either (max width, min height) or (max height, min width). So all we have
        // to do is check which one of these has the closest aspect ratio.

        // Check each possible intersection (or not) of the aspect ratio line with the constraints
        if aspect_ratio > min_w_max_h {
            // outside max height min width
            Size {
                width: self.min.width,
                height: self.max.height,
            }
        } else if aspect_ratio < max_w_min_h {
            // outside min height max width
            Size {
                width: self.max.width,
                height: self.min.height,
            }
        } else if aspect_ratio > min_w_min_h {
            // hits the constraints on the min width line
            if width < self.min.width {
                // we take the point on the min width
                Size {
                    width: self.min.width,
                    height: self.min.width * aspect_ratio,
                }
            } else if aspect_ratio < max_w_max_h {
                // exits through max.width
                Size {
                    width: self.max.width,
                    height: self.max.width * aspect_ratio,
                }
            } else {
                // exits through max.height
                Size {
                    width: self.max.height * aspect_ratio.recip(),
                    height: self.max.height,
                }
            }
        } else {
            // final case is where we hit constraints on the min height line
            if width < self.min.width {
                // take the point on the min height
                Size {
                    width: self.min.height * aspect_ratio.recip(),
                    height: self.min.height,
                }
            } else if aspect_ratio > max_w_max_h {
                // exit thru max height
                Size {
                    width: self.max.height * aspect_ratio.recip(),
                    height: self.max.height,
                }
            } else {
                // exit thru max width
                Size {
                    width: self.max.width,
                    height: self.max.width * aspect_ratio,
                }
            }
        }
    }
/// Find the `Size` within these `BoxConstraint`s that minimises the difference between the
/// returned `Size`'s aspect ratio and `aspect_ratio`, where *aspect ratio* is defined as
/// `height / width`.
///
/// If multiple `Size`s give the optimal `aspect_ratio`, then the one with the `width` nearest
/// the supplied width will be used. Specifically, if `width == 0.0` then the smallest possible
/// `Size` will be chosen, and likewise if `width == f64::INFINITY`, then the largest `Size`
/// will be chosen.
///
/// Use this function when maintaining an aspect ratio is more important than minimizing the
/// distance between input and output size width and height.




Use this function when maintaining an aspect ratio is more important than minimizing the
distance between input and output size width and height.
Use this function when maintaining an aspect ratio is more important than minimizing the distance between input and output size width and height .
pub fn constrain_aspect_ratio(&self, aspect_ratio: f64, width: f64) -> Size {
        // Minimizing/maximizing based on aspect ratio seems complicated, but in reality everything
        // is linear, so the amount of work to do is low.
        let ideal_size = Size {
            width,
            height: width * aspect_ratio,
        };
        // It may be possible to remove these in the future if the invariant is checked elsewhere.
        let aspect_ratio = aspect_ratio.abs();
        let width = width.abs();
        // Firstly check if we can simply return the exact requested
        if self.contains(ideal_size) {
            return ideal_size;
        }
        // Then we check if any `Size`s with our desired aspect ratio are inside the constraints.
        // TODO this currently outputs garbage when things are < 0.
        let min_w_min_h = self.min.height / self.min.width;
        let max_w_min_h = self.min.height / self.max.width;
        let min_w_max_h = self.max.height / self.min.width;
        let max_w_max_h = self.max.height / self.max.width;
        // When the aspect ratio line crosses the constraints, the closest point must be one of the
        // two points where the aspect ratio enters/exits.
        // When the aspect ratio line doesn't intersect the box of possible sizes, the closest
        // point must be either (max width, min height) or (max height, min width). So all we have
        // to do is check which one of these has the closest aspect ratio.
        // Check each possible intersection (or not) of the aspect ratio line with the constraints
        if aspect_ratio > min_w_max_h {
            // outside max height min width
            Size {
                width: self.min.width,
                height: self.max.height,
            }
        } else if aspect_ratio < max_w_min_h {
            // outside min height max width
            Size {
                width: self.max.width,
                height: self.min.height,
            }
        } else if aspect_ratio > min_w_min_h {
            // hits the constraints on the min width line
            if width < self.min.width {
                // we take the point on the min width
                Size {
                    width: self.min.width,
                    height: self.min.width * aspect_ratio,
                }
            } else if aspect_ratio < max_w_max_h {
                // exits through max.width
                Size {
                    width: self.max.width,
                    height: self.max.width * aspect_ratio,
                }
            } else {
                // exits through max.height
                Size {
                    width: self.max.height * aspect_ratio.recip(),
                    height: self.max.height,
                }
            }
        } else {
            // final case is where we hit constraints on the min height line
            if width < self.min.width {
                // take the point on the min height
                Size {
                    width: self.min.height * aspect_ratio.recip(),
                    height: self.min.height,
                }
            } else if aspect_ratio > max_w_max_h {
                // exit thru max height
                Size {
                    width: self.max.height * aspect_ratio.recip(),
                    height: self.max.height,
                }
            } else {
                // exit thru max width
                Size {
                    width: self.max.width,
                    height: self.max.width * aspect_ratio,
                }
            }
        }
    }
pub fn constrain_aspect_ratio ( & self , aspect_ratio : f64 , width : f64 ) -> Size { // Minimizing/maximizing based on aspect ratio seems complicated, but in reality everything // is linear, so the amount of work to do is low. let ideal_size = Size { width , height : width * aspect_ratio , } ; // It may be possible to remove these in the future if the invariant is checked elsewhere. let aspect_ratio = aspect_ratio . abs ( ) ; let width = width . abs ( ) ; // Firstly check if we can simply return the exact requested if self . contains ( ideal_size ) { return ideal_size ; } // Then we check if any `Size`s with our desired aspect ratio are inside the constraints. // TODO this currently outputs garbage when things are < 0. let min_w_min_h = self . min . height / self . min . width ; let max_w_min_h = self . min . height / self . max . width ; let min_w_max_h = self . max . height / self . min . width ; let max_w_max_h = self . max . height / self . max . width ; // When the aspect ratio line crosses the constraints, the closest point must be one of the // two points where the aspect ratio enters/exits. // When the aspect ratio line doesn't intersect the box of possible sizes, the closest // point must be either (max width, min height) or (max height, min width). So all we have // to do is check which one of these has the closest aspect ratio. // Check each possible intersection (or not) of the aspect ratio line with the constraints if aspect_ratio > min_w_max_h { // outside max height min width Size { width : self . min . width , height : self . max . height , } } else if aspect_ratio < max_w_min_h { // outside min height max width Size { width : self . max . width , height : self . min . height , } } else if aspect_ratio > min_w_min_h { // hits the constraints on the min width line if width < self . min . width { // we take the point on the min width Size { width : self . min . width , height : self . min . width * aspect_ratio , } } else if aspect_ratio < max_w_max_h { // exits through max.width Size { width : self . max . width , height : self . max . width * aspect_ratio , } } else { // exits through max.height Size { width : self . max . height * aspect_ratio . recip ( ) , height : self . max . height , } } } else { // final case is where we hit constraints on the min height line if width < self . min . width { // take the point on the min height Size { width : self . min . height * aspect_ratio . recip ( ) , height : self . min . height , } } else if aspect_ratio > max_w_max_h { // exit thru max height Size { width : self . max . height * aspect_ratio . recip ( ) , height : self . max . height , } } else { // exit thru max width Size { width : self . max . width , height : self . max . width * aspect_ratio , } } } }
Find the `Size` within these `BoxConstraint`s that minimises the difference between the
returned `Size`'s aspect ratio and `aspect_ratio`, where *aspect ratio* is defined as
`height / width`.
Find the ` Size ` within these ` BoxConstraint ` s that minimises the difference between the returned ` Size ` ' s aspect ratio and ` aspect_ratio ` where * aspect ratio * is defined as ` height / width ` .
&self
aspect_ratio
f64
width
f64
&self
aspect_ratio
f64
width
f64