-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatterns.rkt
68 lines (59 loc) · 2.61 KB
/
patterns.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#lang typed/racket
(provide (except-out (all-defined-out) stripe gradient ring checker))
(require "tuples.rkt")
(require "color.rkt")
(require "matrix.rkt")
(struct _pattern ([color-at : (-> Point Color)] [transformation : Matrix] [inv-trans : Matrix])
#:prefab
#:type-name Pattern)
(:
pattern
(->* ((U 'stripe 'gradient 'ring 'checker 'plain) (Listof Color)) (#:transformation Matrix) Pattern))
(define (pattern type
colors
#:transformation [transformation id-mat-4])
(cond
[(eq? type 'stripe) (stripe (cast colors (List Color Color)) transformation)]
[(eq? type 'gradient) (gradient (cast colors (List Color Color)) transformation)]
[(eq? type 'ring) (ring (cast colors (List Color Color)) transformation)]
[(eq? type 'checker) (checker (cast colors (List Color Color)) transformation)]
[(eq? type 'plain) (_pattern (lambda (point) (car colors)) id-mat-4 id-mat-4)]
[else (error "Illegal operation: no pattern type: " type)]))
(define pattern-color-at _pattern-color-at)
(define pattern-transformation _pattern-transformation)
(define pattern-inv-trans _pattern-inv-trans)
(: stripe (-> (List Color Color) Matrix Pattern))
(define (stripe colors transformation)
(_pattern (lambda (point)
(if (= 0 (remainder (exact-floor (tuple-x point)) 2)) (first colors) (second colors))) transformation (inverse transformation)))
(: gradient (-> (List Color Color) Matrix Pattern))
(define (gradient colors transformation)
(_pattern (lambda (point)
(let ([delta : Color
(color- (second colors) (first colors))]
[frac : Float
(- (tuple-x point) (floor (tuple-x point)))])
(color+ (first colors) (color* delta frac))))
transformation
(inverse transformation)))
(: ring (-> (List Color Color) Matrix Pattern))
(define (ring colors transformation)
(_pattern
(lambda (point)
(if (= 0 (remainder (exact-floor (sqrt (+ (sqr (tuple-x point)) (sqr (tuple-z point))))) 2))
(first colors)
(second colors)))
transformation
(inverse transformation)))
(: checker (-> (List Color Color) Matrix Pattern))
(define (checker colors transformation)
(_pattern (lambda (point)
(if (= 0
(remainder (+ (exact-floor (tuple-x point))
(exact-floor (tuple-y point))
(exact-floor (tuple-z point)))
2))
(first colors)
(second colors)))
transformation
(inverse transformation)))