1#![allow(dead_code)]
2use crate::geometry::Geometry;
3use crate::point::Point;
4
5pub struct Rectangle {
6 pub min_point: Point,
7 pub max_point: Point,
8}
9
10impl Rectangle {
11 fn area(&self) -> f64 {
12 let Point { x: x1, y: y1 } = self.min_point;
13 let Point { x: x2, y: y2 } = self.max_point;
14 ((x1 - x2) * (y1 - y2)).abs()
15 }
16
17 fn perimeter(&self) -> f64 {
18 let Point { x: x1, y: y1 } = self.min_point;
19 let Point { x: x2, y: y2 } = self.max_point;
20
21 2.0 * ((x1 - x2).abs() + (y1 - y2).abs())
22 }
23
24 fn translate(&mut self, x: f64, y: f64) {
25 self.min_point.x += x;
26 self.min_point.x += x;
27
28 self.max_point.y += y;
29 self.max_point.y += y;
30 }
31}
32impl Geometry for Rectangle {
34 fn describe(&self) -> String {
35 String::from("rectangle")
36 }
37}