Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement some missing functionalities that would be useful in WebRender. #170

Merged
merged 4 commits into from Dec 9, 2016
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Implement Rect::from_points and Matrix4D::transform_rect.

  • Loading branch information
nical committed Dec 9, 2016
commit 5dfd5c963da33484edbf362aa8e19c9e1f067007
@@ -11,7 +11,6 @@ use super::{UnknownUnit, Radians};
use num::{One, Zero};
use point::TypedPoint2D;
use rect::TypedRect;
use size::TypedSize2D;
use std::ops::{Add, Mul, Div, Sub};
use std::marker::PhantomData;
use approxeq::ApproxEq;
@@ -214,28 +213,12 @@ where T: Copy + Clone +
/// matrix.
#[inline]
pub fn transform_rect(&self, rect: &TypedRect<T, Src>) -> TypedRect<T, Dst> {
let top_left = self.transform_point(&rect.origin);
let top_right = self.transform_point(&rect.top_right());
let bottom_left = self.transform_point(&rect.bottom_left());
let bottom_right = self.transform_point(&rect.bottom_right());
let (mut min_x, mut min_y) = (top_left.x, top_left.y);
let (mut max_x, mut max_y) = (min_x, min_y);
for point in &[top_right, bottom_left, bottom_right] {
if point.x < min_x {
min_x = point.x
}
if point.x > max_x {
max_x = point.x
}
if point.y < min_y {
min_y = point.y
}
if point.y > max_y {
max_y = point.y
}
}
TypedRect::new(TypedPoint2D::new(min_x, min_y),
TypedSize2D::new(max_x - min_x, max_y - min_y))
TypedRect::from_points(&[
self.transform_point(&rect.origin),
self.transform_point(&rect.top_right()),
self.transform_point(&rect.bottom_left()),
self.transform_point(&rect.bottom_right()),
])
}

/// Computes and returns the determinant of this matrix.
@@ -11,6 +11,7 @@ use super::{UnknownUnit, Radians};
use approxeq::ApproxEq;
use trig::Trig;
use point::{TypedPoint2D, TypedPoint3D, TypedPoint4D};
use rect::TypedRect;
use matrix2d::TypedMatrix2D;
use scale_factor::ScaleFactor;
use num::{One, Zero};
@@ -398,6 +399,17 @@ where T: Copy + Clone +
TypedPoint4D::new(x, y, z, w)
}

/// Returns a rectangle that encompasses the result of transforming the given rectangle by this
/// matrix.
pub fn transform_rect(&self, rect: &TypedRect<T, Src>) -> TypedRect<T, Dst> {
TypedRect::from_points(&[
self.transform_point(&rect.origin),
self.transform_point(&rect.top_right()),
self.transform_point(&rect.bottom_left()),
self.transform_point(&rect.bottom_right()),
])
}

/// Create a 3d translation matrix
pub fn create_translation(x: T, y: T, z: T) -> TypedMatrix4D<T, Src, Dst> {
let (_0, _1): (T, T) = (Zero::zero(), One::one());
@@ -215,6 +215,31 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
pub fn translate_by_size(&self, size: &TypedSize2D<T, U>) -> TypedRect<T, U> {
self.translate(&TypedPoint2D::new(size.width, size.height))
}

/// Returns the smallest rectangle containing the four points.

This comment has been minimized.

@Ms2ger

Ms2ger Dec 9, 2016

Contributor

Four?

pub fn from_points(points: &[TypedPoint2D<T, U>]) -> Self {
if points.len() == 0 {
return TypedRect::zero();
}
let (mut min_x, mut min_y) = (points[0].x, points[0].y);
let (mut max_x, mut max_y) = (min_x, min_y);
for point in &points[1..] {
if point.x < min_x {
min_x = point.x
}
if point.x > max_x {
max_x = point.x
}
if point.y < min_y {
min_y = point.y
}
if point.y > max_y {
max_y = point.y
}
}
TypedRect::new(TypedPoint2D::new(min_x, min_y),
TypedSize2D::new(max_x - min_x, max_y - min_y))
}
}

impl<T, U> TypedRect<T, U>
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.