add count-square function

This commit is contained in:
Nico 2022-04-01 23:11:42 +01:00
parent 8e626a6058
commit 0681ec7ba5
2 changed files with 22 additions and 6 deletions

View File

@ -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)
)

View File

@ -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)