Advertisement
coder0xff

Ellipse.containsPoint

Feb 22nd, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # for Ellipse class, assuming rotation increases in clockwise direction (negate rotation if not!)
  2.  
  3. # UNTESTED!
  4. containsPoint: (p, withRotation=true) ->
  5.   # "ellipse space" is the cartesian space
  6.   # where the ellipse becomes the unit
  7.   # circle centered at (0, 0)
  8.   [x, y] = [p.x - @x, p.y - @y] # translate point into ellipse space
  9.   if withRotation and @rotation # optionally rotate point into ellipse space
  10.     c = Math.cos(@rotation)
  11.     s = Math.sin(@rotation)
  12.     [x, y] = [x*c + y*s, y*c + x*s]
  13.   x = x / @width # scale point into ellipse space
  14.   y = y / @height
  15.   x*x + y*y <= 1 #if the resulting point falls on/in the unit circle at 0, 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement