Make most geometry functions const
This commit is contained in:
parent
ba572f9af3
commit
ade8859701
|
@ -14,43 +14,43 @@ pub struct Rectangle {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Point {
|
impl Point {
|
||||||
pub fn new(x: u32, y: u32) -> Self {
|
pub const fn new(x: u32, y: u32) -> Self {
|
||||||
Self { x, y }
|
Self { x, y }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn origin() -> Self {
|
pub const fn origin() -> Self {
|
||||||
Self { x: 0, y: 0 }
|
Self { x: 0, y: 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_x(&self, x: u32) -> Self {
|
pub const fn with_x(&self, x: u32) -> Self {
|
||||||
Self { x, y: self.y }
|
Self { x, y: self.y }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_y(&self, y: u32) -> Self {
|
pub const fn with_y(&self, y: u32) -> Self {
|
||||||
Self { x: self.x, y }
|
Self { x: self.x, y }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn x(&self) -> u32 {
|
pub const fn x(&self) -> u32 {
|
||||||
self.x
|
self.x
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn y(&self) -> u32 {
|
pub const fn y(&self) -> u32 {
|
||||||
self.y
|
self.y
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn left_of(&self, other: &Self) -> bool {
|
pub const fn left_of(&self, other: &Self) -> bool {
|
||||||
self.x < other.x
|
self.x < other.x
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn right_of(&self, other: &Self) -> bool {
|
pub const fn right_of(&self, other: &Self) -> bool {
|
||||||
self.x > other.x
|
self.x > other.x
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn above(&self, other: &Self) -> bool {
|
pub const fn above(&self, other: &Self) -> bool {
|
||||||
self.y < other.y
|
self.y < other.y
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn below(&self, other: &Self) -> bool {
|
pub const fn below(&self, other: &Self) -> bool {
|
||||||
self.y > other.y
|
self.y > other.y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ impl PartialEq for Point {
|
||||||
impl Eq for Point {}
|
impl Eq for Point {}
|
||||||
|
|
||||||
impl Rectangle {
|
impl Rectangle {
|
||||||
pub fn from_corner_width_height(top_left: Point, width: u32, height: u32) -> Rectangle {
|
pub const fn from_corner_width_height(top_left: Point, width: u32, height: u32) -> Rectangle {
|
||||||
Rectangle {
|
Rectangle {
|
||||||
top_left,
|
top_left,
|
||||||
width,
|
width,
|
||||||
|
@ -95,19 +95,19 @@ impl Rectangle {
|
||||||
Rectangle::from_corner_width_height(Point::new(left, top), width, height)
|
Rectangle::from_corner_width_height(Point::new(left, top), width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn at_origin_with_size(width: u32, height: u32) -> Rectangle {
|
pub const fn at_origin_with_size(width: u32, height: u32) -> Rectangle {
|
||||||
Rectangle::from_corner_width_height(Point::origin(), width, height)
|
Rectangle::from_corner_width_height(Point::origin(), width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn width(&self) -> u32 {
|
pub const fn width(&self) -> u32 {
|
||||||
self.width
|
self.width
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn height(&self) -> u32 {
|
pub const fn height(&self) -> u32 {
|
||||||
self.height
|
self.height
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_width(&self, width: u32) -> Rectangle {
|
pub const fn with_width(&self, width: u32) -> Rectangle {
|
||||||
Rectangle {
|
Rectangle {
|
||||||
top_left: self.top_left,
|
top_left: self.top_left,
|
||||||
width,
|
width,
|
||||||
|
@ -115,7 +115,7 @@ impl Rectangle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_height(&self, height: u32) -> Rectangle {
|
pub const fn with_height(&self, height: u32) -> Rectangle {
|
||||||
Rectangle {
|
Rectangle {
|
||||||
top_left: self.top_left,
|
top_left: self.top_left,
|
||||||
width: self.width,
|
width: self.width,
|
||||||
|
@ -123,26 +123,26 @@ impl Rectangle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn top_left(&self) -> Point {
|
pub const fn top_left(&self) -> Point {
|
||||||
self.top_left
|
self.top_left
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn top_right(&self) -> Point {
|
pub const fn top_right(&self) -> Point {
|
||||||
Point::new(self.top_left.x() + self.width, self.top_left.y())
|
Point::new(self.top_left.x() + self.width, self.top_left.y())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bottom_right(&self) -> Point {
|
pub const fn bottom_right(&self) -> Point {
|
||||||
Point::new(
|
Point::new(
|
||||||
self.top_left.x() + self.width,
|
self.top_left.x() + self.width,
|
||||||
self.top_left.y() + self.height,
|
self.top_left.y() + self.height,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bottom_left(&self) -> Point {
|
pub const fn bottom_left(&self) -> Point {
|
||||||
Point::new(self.top_left.x(), self.top_left.y() + self.height)
|
Point::new(self.top_left.x(), self.top_left.y() + self.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn area(&self) -> u32 {
|
pub const fn area(&self) -> u32 {
|
||||||
self.width * self.height
|
self.width * self.height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue