Advertisement
zack_falcon

Elixir Circle Collision

Apr 7th, 2021 (edited)
2,849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.82 KB | None | 0 0
  1. defmodule PGS.Circle do
  2.     def new(pos, rad) do
  3.         %{:position=>pos,:radius=>rad}
  4.     end
  5. end
  6.  
  7. defmodule PGS.Vector do
  8.     def zero() do
  9.         new(0, 0, 0)
  10.     end
  11.  
  12.     def new(a, b, c) do
  13.         %{:x => a, :y => b, :z => c}
  14.     end
  15.  
  16.     def add(v1, v2) do
  17.         %{:x => v1.x + v2.x, :y => v1.y + v2.y, :z => v1.z + v2.z}
  18.     end
  19.  
  20.     def sub(v1, v2) do
  21.         %{:x => v1.x - v2.x, :y => v1.y - v2.y, :z => v1.z - v2.z}
  22.     end
  23.  
  24.     def scale(v, s) do
  25.         %{:x => v.x * s, :y => v.y * s, :z => v.z * s}
  26.     end
  27.  
  28.     def dot(v1, v2) do
  29.         v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
  30.     end
  31.  
  32.     def distance(v1, v2) do
  33.         sub(v1, v2) |> PGS.Vector.len
  34.     end
  35.  
  36.     def normalize(v) do
  37.         m = len(v)
  38.         if m == 0 do
  39.             v
  40.         else
  41.             %{:x => v.x / m, :y => v.y / m, :z => v.z / m}
  42.         end
  43.     end
  44.  
  45.     def len(v) do
  46.         Sys.Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
  47.     end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement