mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
vectors: most things work
This commit is contained in:
parent
6dc31855d8
commit
81c2af23f1
@ -158,6 +158,7 @@ preludeModules carpDir = map (\s -> carpDir ++ "/core/" ++ s ++ ".carp") [ "Macr
|
||||
, "System"
|
||||
, "Test"
|
||||
, "Vector"
|
||||
, "Geometry"
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
|
@ -10,8 +10,16 @@
|
||||
(register from-int (Fn [Int] Double))
|
||||
(register sin (Fn [Double] Double))
|
||||
(register cos (Fn [Double] Double))
|
||||
(register acos (Fn [Double] Double))
|
||||
(register atan2 (Fn [Double Double] Double))
|
||||
(register sqrt (Fn [Double] Double))
|
||||
(register str (Fn [Double] String))
|
||||
(register copy (Fn [(Ref Double)] Double))
|
||||
|
||||
(defn clamp [min, max, val]
|
||||
(if (> val max)
|
||||
max
|
||||
(if (< val min)
|
||||
min
|
||||
val)))
|
||||
)
|
||||
|
@ -1,6 +1,5 @@
|
||||
; missing: lerp
|
||||
|
||||
|
||||
(defdynamic append-multi- [strs]
|
||||
(if (= (count strs) 0)
|
||||
'@""
|
||||
@ -9,6 +8,11 @@
|
||||
(defmacro append-multi [:rest strs]
|
||||
(append-multi- strs))
|
||||
|
||||
(defn doubleq [a, b]
|
||||
(if (Double.> a b)
|
||||
(Double.< (Double.- a b) 0.00001)
|
||||
(Double.< (Double.- b a) 0.00001)))
|
||||
|
||||
(defmodule Vector2
|
||||
(deftype V [x Double, y Double])
|
||||
|
||||
@ -34,11 +38,6 @@
|
||||
(V.init (Double./ (V.x a) n)
|
||||
(Double./ (V.y a) n)))
|
||||
|
||||
(defn doubleq [a, b]
|
||||
(if (Double.> a b)
|
||||
(Double.< (Double.- a b) 0.00001)
|
||||
(Double.< (Double.- b a) 0.00001)))
|
||||
|
||||
(defn = [a, b]
|
||||
(and (doubleq (V.x a) (V.x b))
|
||||
(doubleq (V.y a) (V.y b))))
|
||||
@ -73,9 +72,9 @@
|
||||
(Double.+ (Double.* (V.x x) (V.x y))
|
||||
(Double.* (V.y x) (V.y y))))
|
||||
|
||||
;(defn angle-between [a b]
|
||||
; (let [dmm (Double./ (dot a b) (Double.* (mag a) (mag b)))]
|
||||
; (Double.acos (Double.clamp -1.0 1.0 dmm))))
|
||||
(defn angle-between [a b]
|
||||
(let [dmm (Double./ (dot a b) (Double.* (mag a) (mag b)))]
|
||||
(Double.acos (Double.clamp -1.0 1.0 dmm))))
|
||||
)
|
||||
|
||||
(defmodule Vector3
|
||||
@ -113,14 +112,14 @@
|
||||
z (V.z o)]
|
||||
(Double.+ (Double.* x x) (Double.+ (Double.* y y) (Double.* z z)))))
|
||||
|
||||
;(defn mag [o]
|
||||
; (Double.sqrt (mag-sq o)))
|
||||
(defn mag [o]
|
||||
(Double.sqrt (mag-sq o)))
|
||||
|
||||
;(defn normalize [o]
|
||||
; (let [m (mag o)]
|
||||
; (if (= m 0)
|
||||
; o
|
||||
; (div o n))))
|
||||
(defn normalize [o]
|
||||
(let [m (mag o)]
|
||||
(if (doubleq m 0.0)
|
||||
(V.copy o)
|
||||
(/ o m))))
|
||||
|
||||
(defn cross [x, y]
|
||||
(V.init
|
||||
@ -136,9 +135,9 @@
|
||||
(Double.+ (Double.* (V.y x) (V.y y))
|
||||
(Double.* (V.z x) (V.z y)))))
|
||||
|
||||
;(defn angle-between [a b]
|
||||
; (let [dmm (Double./ (dot a b) (Double.* (mag a) (mag b)))]
|
||||
; (Double.acos (Double.clamp -1.0 1.0 dmm))))
|
||||
(defn angle-between [a b]
|
||||
(let [dmm (Double./ (dot a b) (Double.* (mag a) (mag b)))]
|
||||
(Double.acos (Double.clamp -1.0 1.0 dmm))))
|
||||
)
|
||||
|
||||
;(defmodule VectorN
|
||||
|
6
core/geometry.carp
Normal file
6
core/geometry.carp
Normal file
@ -0,0 +1,6 @@
|
||||
(defmodule Geometry
|
||||
(defn degree-to-radians [n]
|
||||
(Double./ (Double.* 2.0 (Double.* n Double.π)) 360.0))
|
||||
|
||||
(defn radians-to-degree [n]
|
||||
(Double./ (Double.* 360.0 n) (Double.* 2.0 Double.π))))
|
@ -209,6 +209,10 @@ double Double_sqrt(double x) {
|
||||
return sqrt(x);
|
||||
}
|
||||
|
||||
double Double_acos(double x) {
|
||||
return acos(x);
|
||||
}
|
||||
|
||||
double Double_atan2(double x, double y) {
|
||||
return atan2(x, y);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
(use IO)
|
||||
(use Vector2)
|
||||
(use Geometry)
|
||||
|
||||
(defn main []
|
||||
(let [x (Vector2.init 1.0 2.0)
|
||||
@ -9,4 +10,5 @@
|
||||
(println &(Vector2.str &x))
|
||||
(println &(Vector2.str &y))
|
||||
(println &(Double.str (Vector2.mag &y)))
|
||||
(println &(Vector2.str &(Vector2.rotate &x (Double./ Double.π 2.0)))))))
|
||||
(println &(Vector2.str &(Vector2.rotate &x (Geometry.degree-to-radians 90.0))))
|
||||
(println &(Double.str (Geometry.radians-to-degree (Vector2.angle-between &x &y)))))))
|
||||
|
Loading…
Reference in New Issue
Block a user