Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function calculatePoint(rw,rh,cx,cy,p1,p2) {
- assert(rh >= rw);
- // Calculate rd*(x^2) + y^2 = rr
- rr = rh^2
- ==> 4
- rd = (rh / rw)^2
- // Translate the points to the center so the ellipse is at 0,0
- p1.x -= cx;
- p1.y -= cy;
- p2.x -= cx;
- p2.y -= cy;
- // Calculate the vector for p1 - p2 (from the tanget to the source)
- v.x = p1.x - p2.x
- v.y = p1.y - p2.y
- // Calculate the vector of the tangent at p2
- n.x = -rd * p2.x
- n.y = p2.y
- // Normalise that vector
- l = sqrt((n.x*n.x)+(n.y*n.y));
- n.x /= l;
- n.y /= l;
- // Calculate the dot product (used in the reflection calculation)
- d = (v.x*n.x) + (v.y*n.y)
- // Calculate the reflection
- // Thanks to http://www.actionscript.org/forums/showthread.php3?t=176052 for this
- r.x = v.x - (2 * d * n.x)
- r.y = v.y - (2 * d * n.y)
- // Calculate how much y will increase by every time x increases by 1
- r.y /= r.x;
- r.x = 1;
- // now y = p2.y - (r.y)x
- // plug this into rd*(x^2) + y^2 = rr
- // doing so ends up with the following ax2 + bx + c
- // Valules worked out by expanding rdx2 + (b + cx)(b + cx) - rr = 0
- a = (rd + (r.y^2))
- b = (2 * r.y * p2.y)
- c = ((p2.y^2) - rr)
- // Preform half the quadratic equation to get x
- p3.x = (-b + sqrt(b^2 - (4*a*c))) / (2*a)
- // Plug this new x in to rd*(x^2) + y^2 = rr to get y
- p3.y = sqrt(rr - rd*(p3.x^2))
- /// Translate the points back so the ellipse is not in the center anymore.
- p3.x += cx;
- p3.y += cy;
- // Return the newly calculated point.
- return p3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement