daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Apr 13th, 2013 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calculatePoint(rw,rh,cx,cy,p1,p2) {
  2.         assert(rh >= rw);
  3.  
  4.         // Calculate rd*(x^2) + y^2 = rr
  5.         rr = rh^2
  6.                 ==> 4
  7.         rd = (rh / rw)^2
  8.  
  9.         // Translate the points to the center so the ellipse is at 0,0
  10.         p1.x -= cx;
  11.         p1.y -= cy;
  12.         p2.x -= cx;
  13.         p2.y -= cy;
  14.  
  15.         // Calculate the vector for p1 - p2 (from the tanget to the source)
  16.         v.x = p1.x - p2.x
  17.         v.y = p1.y - p2.y
  18.  
  19.         // Calculate the vector of the tangent at p2
  20.         n.x = -rd * p2.x
  21.         n.y = p2.y
  22.  
  23.         // Normalise that vector
  24.         l = sqrt((n.x*n.x)+(n.y*n.y));
  25.         n.x /= l;
  26.         n.y /= l;
  27.  
  28.         // Calculate the dot product (used in the reflection calculation)
  29.         d = (v.x*n.x) + (v.y*n.y)
  30.  
  31.         // Calculate the reflection
  32.         // Thanks to http://www.actionscript.org/forums/showthread.php3?t=176052 for this
  33.         r.x = v.x - (2 * d * n.x)
  34.         r.y = v.y - (2 * d * n.y)
  35.  
  36.         // Calculate how much y will increase by every time x increases by 1
  37.         r.y /= r.x;
  38.         r.x = 1;
  39.  
  40.         // now y = p2.y - (r.y)x
  41.         // plug this into rd*(x^2) + y^2 = rr
  42.         // doing so ends up with the following ax2 + bx + c
  43.         // Valules worked out by expanding rdx2 + (b + cx)(b + cx) - rr = 0
  44.  
  45.         a = (rd + (r.y^2))
  46.         b = (2 * r.y * p2.y)
  47.         c = ((p2.y^2) - rr)
  48.  
  49.         // Preform half the quadratic equation to get x
  50.         p3.x = (-b + sqrt(b^2 - (4*a*c))) / (2*a)
  51.  
  52.         // Plug this new x in to rd*(x^2) + y^2 = rr to get y
  53.         p3.y = sqrt(rr - rd*(p3.x^2))
  54.  
  55.         /// Translate the points back so the ellipse is not in the center anymore.
  56.         p3.x += cx;
  57.         p3.y += cy;
  58.  
  59.         // Return the newly calculated point.
  60.         return p3;
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top