Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule PGS.Circle do
- def new(pos, rad) do
- %{:position=>pos,:radius=>rad}
- end
- end
- defmodule PGS.Vector do
- def zero() do
- new(0, 0, 0)
- end
- def new(a, b, c) do
- %{:x => a, :y => b, :z => c}
- end
- def add(v1, v2) do
- %{:x => v1.x + v2.x, :y => v1.y + v2.y, :z => v1.z + v2.z}
- end
- def sub(v1, v2) do
- %{:x => v1.x - v2.x, :y => v1.y - v2.y, :z => v1.z - v2.z}
- end
- def scale(v, s) do
- %{:x => v.x * s, :y => v.y * s, :z => v.z * s}
- end
- def dot(v1, v2) do
- v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
- end
- def distance(v1, v2) do
- sub(v1, v2) |> PGS.Vector.len
- end
- def normalize(v) do
- m = len(v)
- if m == 0 do
- v
- else
- %{:x => v.x / m, :y => v.y / m, :z => v.z / m}
- end
- end
- def len(v) do
- Sys.Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement