kahlo 0.0.3

Optimized software rendering library.
Documentation
//! Types and operations for 2D mathematics.

/// Tag for pixel units
pub struct PixelSpace;

/// Represents a point in pixel space
pub type PixelPoint = euclid::Point2D<i32, PixelSpace>;
/// Represents a relative distance in pixel space
pub type PixelVector = euclid::Vector2D<i32, PixelSpace>;
/// Represents a 'size' in pixel space
pub type PixelSize = euclid::Size2D<i32, PixelSpace>;
/// Represents a rect (origin + size) in pixel space
pub type PixelRect = euclid::Rect<i32, PixelSpace>;
/// Represents a box (min + max) in pixel space
pub type PixelBox = euclid::Box2D<i32, PixelSpace>;
/// Represents a set of side offsets for a rect/box in pixel space
pub type PixelSideOffsets = euclid::SideOffsets2D<i32, PixelSpace>;

pub fn clip_to(
    img1_clip_box: PixelBox,
    img2_clip_box: PixelBox,
    img1_victim_box: PixelBox,
    img2_victim_box: PixelBox,
) -> Option<(PixelBox, PixelBox)> {
    if img1_victim_box.size() != img2_victim_box.size() || img1_victim_box.is_empty() {
        return None;
    }

    let adj = PixelSideOffsets {
        left: (img1_clip_box.min.x - img1_victim_box.min.x)
            .max(img2_clip_box.min.x - img2_victim_box.min.x)
            .max(0),
        top: (img1_clip_box.min.y - img1_victim_box.min.y)
            .max(img2_clip_box.min.y - img2_victim_box.min.y)
            .max(0),
        right: (img1_victim_box.max.x - img1_clip_box.max.x)
            .max(img2_victim_box.max.x - img2_clip_box.max.x)
            .max(0),
        bottom: (img1_victim_box.max.y - img1_clip_box.max.y)
            .max(img2_victim_box.max.y - img2_clip_box.max.y)
            .max(0),
        ..Default::default()
    };

    Some((
        img1_victim_box.inner_box(adj),
        img2_victim_box.inner_box(adj),
    ))
}