mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
vectors: added a few math primitives and made most functions work
This commit is contained in:
parent
6de836d545
commit
6dc31855d8
@ -1,5 +1,7 @@
|
||||
(defmodule Double
|
||||
(def π 3.1415926536)
|
||||
(register < (Fn [Double Double] Bool))
|
||||
(register > (Fn [Double Double] Bool))
|
||||
(register + (Fn [Double Double] Double))
|
||||
(register - (Fn [Double Double] Double))
|
||||
(register * (Fn [Double Double] Double))
|
||||
@ -8,6 +10,8 @@
|
||||
(register from-int (Fn [Int] Double))
|
||||
(register sin (Fn [Double] Double))
|
||||
(register cos (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))
|
||||
)
|
||||
|
@ -34,40 +34,40 @@
|
||||
(V.init (Double./ (V.x a) n)
|
||||
(Double./ (V.y a) n)))
|
||||
|
||||
(defn doubeq [a, b]
|
||||
(defn doubleq [a, b]
|
||||
(if (Double.> a b)
|
||||
(Double.< (Double.- a b) 0.00001)
|
||||
(Double.< (Double.- b a) 0.00001)))
|
||||
|
||||
(defn = [a, b]
|
||||
(and (doubeq (V.x a) (V.x b))
|
||||
(doubeq (V.y a) (V.y b))))
|
||||
(and (doubleq (V.x a) (V.x b))
|
||||
(doubleq (V.y a) (V.y b))))
|
||||
|
||||
(defn mag-sq [o]
|
||||
(let [x (V.x o)
|
||||
y (V.y o)]
|
||||
(Double.+ (Double.* x x) (Double.* y y))))
|
||||
|
||||
;(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 dist [a, b]
|
||||
; (let [b (- b a)]
|
||||
; (mag b)))
|
||||
(defn dist [a, b]
|
||||
(let [s (- &b &a)]
|
||||
(mag &s)))
|
||||
|
||||
;(defn heading [a]
|
||||
; (Double.atan2 (V.y a) (V.x a)))
|
||||
(defn heading [a]
|
||||
(Double.atan2 (V.y a) (V.x a)))
|
||||
|
||||
;(defn rotate [a n]
|
||||
; (let [h (Double.+ (heading a) n)
|
||||
; m (mag a)]
|
||||
; (V.init (Double.* (Double.cos h) m) (Double.* (Double.sin h) m))))
|
||||
(defn rotate [a n]
|
||||
(let [h (Double.+ (heading a) n)
|
||||
m (mag a)]
|
||||
(V.init (Double.* (Double.cos h) m) (Double.* (Double.sin h) m))))
|
||||
|
||||
(defn dot [x, y]
|
||||
(Double.+ (Double.* (V.x x) (V.x y))
|
||||
|
@ -63,6 +63,8 @@ double Double_copy(double *x) { return *x; }
|
||||
#define Double__MINUS_(x, y) ((x) - (y))
|
||||
#define Double__MUL_(x, y) ((x) * (y))
|
||||
#define Double__DIV_(x, y) ((x) / (y))
|
||||
#define Double__LT_(x, y) ((x) < (y))
|
||||
#define Double__GT_(x, y) ((x) > (y))
|
||||
|
||||
#define Float__PLUS_(x, y) ((x) + (y))
|
||||
#define Float__MINUS_(x, y) ((x) - (y))
|
||||
@ -203,6 +205,14 @@ double Double_cos(double x) {
|
||||
return cos(x);
|
||||
}
|
||||
|
||||
double Double_sqrt(double x) {
|
||||
return sqrt(x);
|
||||
}
|
||||
|
||||
double Double_atan2(double x, double y) {
|
||||
return atan2(x, y);
|
||||
}
|
||||
|
||||
string Double_str(double x) {
|
||||
char *buffer = CARP_MALLOC(32);
|
||||
snprintf(buffer, 32, "%g", x);
|
||||
|
@ -2,9 +2,11 @@
|
||||
(use Vector2)
|
||||
|
||||
(defn main []
|
||||
(let [x (Vector2.init 1 2)
|
||||
y (Vector2.init 3 4)]
|
||||
(let [x (Vector2.init 1.0 2.0)
|
||||
y (Vector2.init 3.0 4.0)]
|
||||
(do
|
||||
(println &(Vector2.str &(+ &x &y)))
|
||||
(println &(Vector2.str &x))
|
||||
(println &(Vector2.str &y)))))
|
||||
(println &(Vector2.str &y))
|
||||
(println &(Double.str (Vector2.mag &y)))
|
||||
(println &(Vector2.str &(Vector2.rotate &x (Double./ Double.π 2.0)))))))
|
||||
|
Loading…
Reference in New Issue
Block a user