-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2d-polar.scm
89 lines (70 loc) · 2.29 KB
/
2d-polar.scm
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
;;; Copyright 2013-2014 by Christian Jaeger <[email protected]>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require easy
cj-math
2d-shape
jclass)
(include "cj-standarddeclares.scm")
(def 90° (* 0.5 pi))
(jclass (2d-polar #(real? angle)
#(real? distance))
(def-method (point v)
(2d-point (* distance (cos angle))
(* distance (sin angle)))))
(TEST
> (.point (2d-polar 0 0))
#((2d-point) 0 0)
> (.point (2d-polar 0 1))
#((2d-point) 1 0)
;; 32 and 64 bit Gambit don't give the same values (huh?)
> (def (t v e64 e32)
(or (equal? v e64)
(equal? v e32)))
> (t (.point (2d-polar 90° 1))
'#((2d-point) 6.123233995736766e-17 1.)
'#((2d-point) 6.123031769111886e-17 1.))
> (t (.point (2d-polar (* 2 90°) 1))
'#((2d-point) -1. 1.2246467991473532e-16)
'#((2d-point) -1. 1.2246063538223773e-16))
> (t (.point (2d-polar (* 3 90°) 1))
'#((2d-point) -1.8369701987210297e-16 -1.)
'#((2d-point) -1.836909530733566e-16 -1.)))
(def. (2d-point.polar v)
(let-2d-point
((x y) v)
(let ((distance (sqrt (+ (square x)
(square y)))))
(2d-polar (atan y x)
distance))))
(TEST
> (.polar (2d-point 2 2))
#((2d-polar) .7853981633974483 2.8284271247461903)
> (.point #)
#((2d-point) 2.0000000000000004 2.)
> (.polar (2d-point 2 -2))
#((2d-polar) -.7853981633974483 2.8284271247461903)
> (.point #)
#((2d-point) 2.0000000000000004 -2.)
> (.polar (.point (2d-polar 0 0)))
#((2d-polar) 0 0)
> (.polar (.point (2d-polar 0 1)))
#((2d-polar) 0 1)
> (.polar (.point (2d-polar 90° 2)))
#((2d-polar) 1.5707963267948966 2.)
> (.polar (.point (2d-polar 1 2)))
#((2d-polar) 1. 2.)
> (.polar (2d-point 1 2000))
#((2d-polar) 1.5702963268365633 2000.0002499999844)
> (.polar (2d-point 1 -2000))
#((2d-polar) -1.5702963268365633 2000.0002499999844)
> (.polar (2d-point 0 2000))
#((2d-polar) 1.5707963267948966 2000)
> (.polar (2d-point -1 2000))
#((2d-polar) 1.57129632675323 2000.0002499999844)
> (.polar (.point (2d-polar -2 2)))
#((2d-polar) -2. 2.)
> (.polar (.point (2d-polar 2 2)))
#((2d-polar) 2. 2.))