Skip to content

Instantly share code, notes, and snippets.

@mourner
Created September 7, 2017 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mourner/87e4659728374aef093fb834ec2227e7 to your computer and use it in GitHub Desktop.
Save mourner/87e4659728374aef093fb834ec2227e7 to your computer and use it in GitHub Desktop.
WIP WebAssembly simplify
(module
(func $segDist (export "segDist")
(param $x0 f64) (param $y0 f64)
(param $x1 f64) (param $y1 f64)
(param $x2 f64) (param $y2 f64)
(result f64)
(local $x f64) (local $y f64)
(local $dx f64) (local $dy f64)
(local $len f64)
(local $t f64)
(set_local $x (get_local $x1))
(set_local $y (get_local $y1))
(set_local $dx (f64.sub (get_local $x2) (get_local $x)))
(set_local $dy (f64.sub (get_local $y2) (get_local $y)))
(set_local $len (f64.add
(f64.mul (get_local $dx) (get_local $dx))
(f64.mul (get_local $dy) (get_local $dy))
))
(f64.gt (get_local $len) (f64.const 0)) if
(set_local $t (f64.add
(f64.mul (f64.sub (get_local $x0) (get_local $x)) (get_local $dx))
(f64.mul (f64.sub (get_local $y0) (get_local $y)) (get_local $dx))
))
(f64.gt (get_local $t) (get_local $len)) if
(set_local $x (get_local $x2))
(set_local $y (get_local $y2))
else
(f64.gt (get_local $t) (f64.const 0)) if
(set_local $t (f64.div (get_local $t) (get_local $len)))
(set_local $x (f64.add (get_local $x) (f64.mul (get_local $dx) (get_local $t))))
(set_local $y (f64.add (get_local $y) (f64.mul (get_local $dy) (get_local $t))))
end
end
end
(set_local $dx (f64.sub (get_local $x0) (get_local $x)))
(set_local $dy (f64.sub (get_local $y0) (get_local $y)))
(f64.add
(f64.mul (get_local $dx) (get_local $dx))
(f64.mul (get_local $dy) (get_local $dy))
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment