-
Notifications
You must be signed in to change notification settings - Fork 11
/
clip-segment-to-line.sls
70 lines (51 loc) · 2.05 KB
/
clip-segment-to-line.sls
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
69
70
;; Copyright 2016 Eduardo Cavazos
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
(library (box2d-lite clip-segment-to-line)
(export clip-segment-to-line)
(import (rnrs)
(dharmalab misc is-vector)
(box2d-lite util say)
(box2d-lite vec)
(box2d-lite edge-numbers)
(box2d-lite clip-vertex)
(box2d-lite edges))
(define (clip-segment-to-line v-out v-in normal offset clip-edge)
(define num-out 0)
(is-vector v-out num-out)
(is-clip-vertex v-out.num-out)
(is-edges v-out.num-out.e)
(define-syntax v-in.0 (identifier-syntax (vector-ref v-in 0)))
(define-syntax v-in.1 (identifier-syntax (vector-ref v-in 1)))
(is-clip-vertex v-in.0)
(is-clip-vertex v-in.1)
(let ((distance-0 (- (vec-dot normal v-in.0.v) offset))
(distance-1 (- (vec-dot normal v-in.1.v) offset)))
(if (<= distance-0 0.0)
(begin (v-out.num-out! v-in.0) (set! num-out (+ num-out 1))))
(if (<= distance-1 0.0)
(begin (v-out.num-out! v-in.1) (set! num-out (+ num-out 1))))
(if (< (* distance-0 distance-1) 0.0)
(let ((interp (/ distance-0 (- distance-0 distance-1))))
(v-out.num-out.v! (v+ v-in.0.v (n*v interp (v- v-in.1.v v-in.0.v))))
(cond ((> distance-0 0.0)
(v-out.num-out.e! v-in.0.e)
(v-out.num-out.e.in-edge-1! clip-edge)
(v-out.num-out.e.in-edge-2! NO-EDGE))
(else
(v-out.num-out.e! v-in.1.e)
(v-out.num-out.e.out-edge-1! clip-edge)
(v-out.num-out.e.out-edge-2! NO-EDGE)))
(set! num-out (+ num-out 1)))))
num-out)
)