aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m4/shapes/src/MyRectangle.java
blob: 591d7d15b8fe03fe680d160ffd8d1979b65aa75f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MyRectangle implements Shape2d{
	private MyPoint a, b;
	
	/**
	 * Creates a new rectangle where a, b are opposing corners.
	 */
	public MyRectangle(MyPoint a, MyPoint b) {
		this.a = a;
		this.b = b;
	}
	
	public double area() {
		MyPoint size = this.b.sub(this.a);
		return Math.abs(size.getX() * size.getY());
	}
	
	public double perimeter() {
		MyPoint size = this.b.sub(this.a).abs();
		return size.getX() * 2 + size.getY() * 2;
	}
}