From 0681ec7ba5ee4807591929d07f8a37488f986396 Mon Sep 17 00:00:00 2001 From: nihilazo Date: Fri, 1 Apr 2022 23:11:42 +0100 Subject: [PATCH] add count-square function --- main-test.rkt | 14 +++++++++----- main.rkt | 14 +++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/main-test.rkt b/main-test.rkt index bdcf749..f709c0e 100644 --- a/main-test.rkt +++ b/main-test.rkt @@ -56,7 +56,12 @@ (check-true (forwards? (line (point 0 1) (point 1 1) 1))) (check-false (forwards? (line (point 1 1) (point 0 0) 0)))) -; TODO +(test-case + "point+" + (check-equal? (point+ (point 0 0) (point 1 2)) (point 1 2)) + (check-equal? (point+ (point 2 4) (point 5 0)) (point 7 4)) + (check-equal? (point+ (point 0 0) (point 0 0)) (point 0 0))) + (test-case "count-square" (define grid (list @@ -70,11 +75,10 @@ (line (point 3 3) (point 3 4) 0) (line (point 3 3) (point 4 3) 0) (line (point 4 3) (point 4 4) 0) - (line (point 3 4) (point 4 4) 0) - )) + (line (point 3 4) (point 4 4) 0))) (check-equal? (count-square (point 0 0) grid) 4) - (check-equal? (count-square (point 0 1) grid) 3) - (check-equal? (count-square (point 0 2) grid) 2) + (check-equal? (count-square (point 1 0) grid) 3) + (check-equal? (count-square (point 2 0) grid) 2) (check-equal? (count-square (point 3 2) grid) 1) (check-equal? (count-square (point 0 4) grid) 0) ) \ No newline at end of file diff --git a/main.rkt b/main.rkt index 80bbf58..c89fe0b 100644 --- a/main.rkt +++ b/main.rkt @@ -40,9 +40,21 @@ (equal? (line-to l1) (line-from l2)) (equal? (line-from l1) (line-to l2))))) +; Point Point -> Point +; adds two points together. +(define (point+ p1 p2) + (point (+ (point-x p1) (point-x p2)) (+ (point-y p1) (point-y p2)))) + ; Point Grid -> Number ; given a point that is the top-left corner of a square on the grid, returns the amount of edges surrounding that square. -(define (count-square p g) 0) +(define (count-square p g) + ; explanation: + ; for every item in the grid (g), test it against every edge of the box + (length + (filter (lambda (item) (foldl (lambda (x y) (or x y)) #f + (for/list ([x (list (point 0 0) (point 0 1) (point 1 0) (point 0 0))] + [y (list (point 0 1) (point 1 1) (point 1 1) (point 1 0))]) + (same-position? item (line (point+ x p) (point+ y p) 0))))) g))) ; for every position in the square, check if there's a line there ; Line -> Bool ; returns #t if the line is forwards (with forwards being moving right and down, higher end coord than start)