-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#lang racket/base | ||
|
||
(require racket/contract | ||
racket/math) | ||
|
||
(provide | ||
(contract-out | ||
[tau radians?] | ||
[degrees? predicate/c] | ||
[radians? predicate/c] | ||
[modulo-degrees (-> real? degrees?)] | ||
[modulo-radians (-> real? radians?)])) | ||
|
||
|
||
(define tau (* 2 pi)) | ||
|
||
(define (degrees? x) | ||
(and (real? x) | ||
(<= 0 x 360))) | ||
|
||
(define (radians? x) | ||
(and (real? x) | ||
(<= 0 x tau))) | ||
|
||
(define (modulo-degrees x) | ||
(modulo x 360)) | ||
|
||
(define (modulo-radians x) | ||
(modulo x tau)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#lang sweet-exp racket/base | ||
|
||
require racket/contract/base | ||
|
||
provide | ||
contract-out | ||
posn-rotate-origin-ccw-90 (-> posn? posn?) | ||
posn-rotate-origin-ccw-180 (-> posn? posn?) | ||
posn-rotate-origin-ccw-270 (-> posn? posn?) | ||
posn-rotate-origin-ccw (-> degrees? posn? posn?) | ||
|
||
require fancy-app | ||
racket/match | ||
racket/math | ||
"base.rkt" | ||
"degrees.rkt" | ||
|
||
module+ test | ||
require rackunit | ||
|
||
|
||
module+ test | ||
(define current-tolerance (make-parameter 0.000000001)) | ||
(define (tolerance=? a b) | ||
(<= (abs (- a b)) (current-tolerance))) | ||
(define-check (check-posn=? actual-posn expected-posn) | ||
(let () | ||
(match-define (posn actual-x actual-y) actual-posn) | ||
(match-define (posn expected-x expected-y) expected-posn) | ||
(and (tolerance=? actual-x expected-x) | ||
(tolerance=? actual-y expected-y)))) | ||
|
||
|
||
(define (posn-rotate-origin-ccw-90 a-posn) | ||
(match-define (posn x y) a-posn) | ||
(posn (- y) x)) | ||
|
||
module+ test | ||
(check-equal? (posn-rotate-origin-ccw-90 (posn 2 1)) (posn -1 2)) | ||
|
||
|
||
(define (posn-rotate-origin-ccw-180 a-posn) | ||
(match-define (posn x y) a-posn) | ||
(posn (- x) (- y))) | ||
|
||
module+ test | ||
(check-equal? (posn-rotate-origin-ccw-180 (posn 2 1)) (posn -2 -1)) | ||
|
||
|
||
(define (posn-rotate-origin-ccw-270 a-posn) | ||
(match-define (posn x y) a-posn) | ||
(posn y (- x))) | ||
|
||
module+ test | ||
(check-equal? (posn-rotate-origin-ccw-270 (posn 2 1)) (posn 1 -2)) | ||
|
||
|
||
(define (posn-rotate-origin-ccw angle a-posn) | ||
(define theta (degrees->radians angle)) | ||
(match-define (posn x y) a-posn) | ||
(define sin-t (sin theta)) | ||
(define cos-t (cos theta)) | ||
(define -sin-t (- sin-t)) | ||
(define x2 (+ (* cos-t x) (* -sin-t y))) | ||
(define y2 (+ (* sin-t x) (* cos-t y))) | ||
(posn x2 y2)) | ||
|
||
module+ test | ||
(check-posn=? (posn-rotate-origin-ccw 90 (posn 1 2)) | ||
(posn-rotate-origin-ccw-90 (posn 1 2))) | ||
(check-posn=? (posn-rotate-origin-ccw 180 (posn 1 2)) | ||
(posn-rotate-origin-ccw-180 (posn 1 2))) | ||
(check-posn=? (posn-rotate-origin-ccw 270 (posn 1 2)) | ||
(posn-rotate-origin-ccw-270 (posn 1 2))) | ||
(check-posn=? (posn-rotate-origin-ccw 45 (posn 1 1)) | ||
(posn 0 (sqrt 2))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#lang scribble/manual | ||
|
||
@(require "util/doc.rkt") | ||
|
||
|
||
@defproc[(posn-rotate-origin-ccw-90 [a-posn posn?]) posn?]{ | ||
Returns @racket[a-posn] rotated ninety degrees counter | ||
clockwise around the origin. | ||
@posn-examples[ | ||
(posn-rotate-origin-ccw-90 (posn 2 1)) | ||
]} | ||
|
||
@defproc[(posn-rotate-origin-ccw-180 [a-posn posn?]) posn?]{ | ||
Returns @racket[a-posn] rotated one hundred and eighty | ||
degrees counter clockwise around the origin. | ||
@posn-examples[ | ||
(posn-rotate-origin-ccw-180 (posn 2 1)) | ||
]} | ||
|
||
@defproc[(posn-rotate-origin-ccw-270 [a-posn posn?]) posn?]{ | ||
Returns @racket[a-posn] rotated two hundred and seventy | ||
degrees counter clockwise around the origin. | ||
@posn-examples[ | ||
(posn-rotate-origin-ccw-270 (posn 2 1)) | ||
]} | ||
|
||
@defproc[(posn-rotate-origin-ccw [degrees (between/c 0 360)] | ||
[a-posn posn?]) | ||
posn?]{ | ||
Returns @racket[a-posn] rotated @racket[degrees] counter | ||
clockwise around the origin. | ||
@posn-examples[ | ||
(posn-rotate-origin-ccw 70 (posn 1 1)) | ||
]} |