Ray-casting algorithm: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: used a better over/under statement construct.)
(→‎{{header|Racket}}: Added missing implementation details from algorithm.)
Line 2,993: Line 2,993:
(module pip racket
(module pip racket
(require racket/contract)
(require racket/contract)

(provide point)
(provide point)
(provide seg)
(provide seg)
Line 3,002: Line 3,002:


(struct point (x y) #:transparent)
(struct point (x y) #:transparent)
(struct seg (Ax Ay Bx By))
(struct seg (Ax Ay Bx By) #:transparent)
(define ε 0.000001)
(define ε 0.000001)
(define (neq? x y) (not (eq? x y)))
(define (neq? x y) (not (eq? x y)))
Line 3,013: Line 3,013:
(eq? Pyo By))
(eq? Pyo By))
ε 0))])
ε 0))])

(cond [(or (< Py Ay) (> Py By)) #f]
(define Ax2 (if (< Ay By) Ax Bx))
(define Ay2 (if (< Ay By) Ay By))
(define Bx2 (if (< Ay By) Bx Ax))
(define By2 (if (< Ay By) By Ay))

(cond [(or (> Py (max Ay By)) (< Py (min Ay By))) #f]
[(> Px (max Ax Bx)) #f]
[(> Px (max Ax Bx)) #f]
[(< Px (min Ax Bx)) #t]
[else (cond
[else
[(< Px (min Ax Bx)) #t]
(let ([red (if (neq? Ax Px)
[else
(/ (- By Ay) (- Bx Ax))
(let ([red (if (neq? Ax2 Bx2)
+inf.0)]
(/ (- By2 Ay2) (- Bx2 Ax2))
[blue (if (neq? Ax Px)
+inf.0)]
(/ (- Py Ax) (- Px Ax))
[blue (if (neq? Ax2 Px)
+inf.0)])
(/ (- Py Ay2) (- Px Ax2))
(if (>= blue red) #t #f))])))
+inf.0)])
(if (>= blue red) #t #f))])])))


(define (point-in-polygon? point polygon)
(define (point-in-polygon? point polygon)