class Point {
let x: Int
let y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
class Rectangle {
let nw: Point
let se: Point
init(nw: Point, se: Point) {
self.nw = nw
self.se = se
}
func area() -> Int {
return (se.y - nw.y) * (se.x - nw.x)
}
func containsPoint(_ p: Point) -> Bool {
let isContainHorizontal = (nw.x <= p.x) && (p.x <= se.x )
let isContainVertical = (nw.y <= p.y) && (p.y <= se.y)
return isContainHorizontal && isContainVertical
}
func combine(_ rect: Rectangle) -> Rectangle {
return Rectangle(nw: Point(x: max(nw.x, rect.nw.x), y: max(nw.y, rect.nw.y)), se: Point(x: min(se.x, rect.se.x), y: min(se.y, rect.se.y)))
}
}
output
2. 6
3. false, true
4. 1
1
solved How to create rectangle form point x and y