17 2d and 3d geometry with fastmath.vector
- DRAFT 🛠
authors: Nedeljko Radovanovic, Epidiah Ravachol, Daniel Slutsky
17.1 Setup
ns noj-book.fastmath-vector-geom2d3d
(:require [fastmath.vector :as vec]
(:as math]
[clojure.math :as mafs]
[emmy.mafs :as kind])) [scicloj.kindly.v4.kind
17.2 a few operations
17.2.1 rounding
-> (vec/vec2 1/3 2/3)
(5)) (vec/approx
0.33333 0.66667] [
17.2.2 addition
2 -1)
(vec/add (vec/vec2 -1 4)) (vec/vec2
1.0 3.0] [-
17.2.3 rotation
Pi radians = 180 degress
-> (vec/vec2 1 0)
( (vec/rotate math/PI))
1.0 1.224646799076922E-16] [-
-> (vec/vec2 1 0)
(
(vec/rotate math/PI)5)) (vec/approx
1.0 0.0] [-
rotating by Pi ratians (90 degrees) around the z axis:
-> (vec/vec3 1 0 1)
(0 0 (/ math/PI 2))
(vec/rotate 5)) (vec/approx
0.0 1.0 1.0] [
17.3 visualizing 2d addition
(the parallelogram rule)
(mafs/mafs:viewBox {:x [-5 5 ]
{:y [-5 5]}}
(mafs/cartesian)2 -1] {:color :blue})
(mafs/vector [-1 4] {:color :blue})
(mafs/vector [vec
(mafs/vector (2 -1)
(vec/add (vec/vec2 -1 4)))
(vec/vec2 :color :red})) {
17.4 visualizing 2d rotation
(mafs/mafs:viewBox {:x [-5 5 ]
{:y [-5 5]}}
(mafs/cartesian)vec (vec/vec2 -2 -1))
(mafs/vector (:color :blue})
{vec (vec/rotate
(mafs/vector (2 -1)
(vec/vec2 -/ math/PI 2)))
(:color :red})) {
17.5 visualizing 3d rotation
defn vec3d->plotly-coords [v]
(:x [0 (v 0)]
{:y [0 (v 1)]
:z [0 (v 2)]
:type :scatter3d
:mode :lines+markers
:line {:width 10}
:marker {:size 4}})
let [orig-v (vec/vec3 1 0 0)]
(
(kind/plotly:data [(vec3d->plotly-coords orig-v)
{
(vec3d->plotly-coords (vec/rotate orig-v0
/ math/PI 10)
(0))]}))
def random-shape
(repeatedly 6
(fn []
(+ 1 (rand))
(vec/vec3 (+ 2 (rand))
(0))))
defn shape->plotly-coords [shape]
(:x (mapv #(% 0) shape)
{:y (mapv #(% 1) shape)
:z (mapv #(% 2) shape)
:type :scatter3d
:mode :markers+lines
:line {:width 10}
:marker {:size 4}})
(kind/plotly:data [(shape->plotly-coords random-shape)
{
(shape->plotly-coordsmap #(vec/rotate %
(0
0
Math/PI)
random-shape))0 0 0]])]}) (shape->plotly-coords [[
random-shape
1.3299890332130637 2.0603884935690666 0.0]
([1.255112249799006 2.6291213048553415 0.0]
[1.23528803371401 2.903983406996838 0.0]
[1.1379303499201914 2.8099353419469946 0.0]
[1.781007147394681 2.2988765988930506 0.0]
[1.5315826533853718 2.3067714906273387 0.0]) [