@@ -225,3 +225,44 @@ def intRange (m n : Int) : List Int :=
225
225
#guard intRange 1 1 == []
226
226
227
227
instance hashableChar : Hashable Char where hash c := c.toNat |> hash
228
+
229
+ structure Point where
230
+ x : Int
231
+ y : Int
232
+ deriving BEq, Hashable, Repr, Inhabited
233
+
234
+ def Point.toPair (p : Point) : (Int × Int) := (p.x, p.y)
235
+
236
+ def Point.origin : Point := { x := 0 , y := 0 }
237
+
238
+ structure Vector where
239
+ x : Int
240
+ y : Int
241
+ deriving BEq, Hashable, Repr, Inhabited
242
+
243
+ def Point.vectorTo (from_ to : Point) : Vector := { x := to.x - from_.x, y := to.y - from_.y }
244
+ #guard ({ x := 1 , y := 1 } : Point).vectorTo { x := 2 , y := 2 } == { x := 1 , y := 1 }
245
+
246
+ def Point.add (p : Point) (v : Vector) : Point := { x := p.x + v.x, y := p.y + v.y }
247
+ #guard Point.add { x := 1 , y := 1 } { x := 2 , y := 2 } == { x := 3 , y := 3 }
248
+
249
+ structure Rectangle where
250
+ topLeft : Point
251
+ width: Nat
252
+ height: Nat
253
+ deriving Repr, BEq, Hashable, Inhabited
254
+
255
+ def Rectangle.contains (r : Rectangle) (p : Point) : Bool :=
256
+ r.topLeft.x ≤ p.x && p.x < r.topLeft.x + r.width &&
257
+ r.topLeft.y ≤ p.y && p.y < r.topLeft.y + r.height
258
+
259
+ #guard Rectangle.contains { topLeft := Point.origin, width := 2 , height := 2 } { x := 1 , y := 1 }
260
+ #guard !Rectangle.contains { topLeft := Point.origin, width := 2 , height := 2 } { x := 2 , y := 2 }
261
+
262
+ def Rectangle.allPoints (r : Rectangle) : List Point := do
263
+ let x <- intRange r.topLeft.x (r.topLeft.x + r.width)
264
+ let y <- intRange r.topLeft.y (r.topLeft.y + r.height)
265
+ return { x := x, y := y }
266
+
267
+ #guard Rectangle.allPoints { topLeft := Point.origin, width := 2 , height := 2 } ==
268
+ [{ x := 0 , y := 0 }, { x := 0 , y := 1 }, { x := 1 , y := 0 }, { x := 1 , y := 1 }]
0 commit comments