Несколько забавных экспериментов с эллиптическим столом. Код Delphi (без обработки ошибок!).
//calculates next ball position in ellipse
//ellipse semiaxes A, B, A2 = A * A, B2 = B * B
//center CX, CY
//PX,PY - old position, VX,VY - velocity components
//V - scalar velocity V = Sqrt(VX * Vx + VY * VY)
procedure TForm1.Calc;
var
t: Double;
eqA, eqB, eqC, DD: Double;
EX, EY, DX, DY, FX, FY: Double;
begin
//new position
NPX := PX + VX;
NPY := PY + VY;
//if new position is outside
if (B2 * Sqr(NPX) + A2 * Sqr(NPY) >= A2 * B2) then begin
//find intersection point of the ray in parametric form and ellipse
eqA := B2 * VX * VX + A2 * VY * VY;
eqB := 2 * (B2 * PX * VX + A2 * PY * VY);
eqC := -A2 * B2 + B2 * PX * PX + A2 * PY * PY;
DD := eqB * eqB - 4 * eqA * eqC;
DD := Sqrt(DD);
//we need only one bigger root
t := 0.5 * (DD - eqB) / eqA;
//intersection point
EX := PX + t * VX;
EY := PY + t * VY;
//mark intersection position by little circle
Canvas.Ellipse(Round(EX - 2 + CX), Round(EY - 2 + CY),
Round(EX + 3 + CX), Round(EY + 3 + CY));
//ellipse normal direction
DX := B2 * EX;
DY := A2 * EY;
DD := 1.0 / (DY * DY + DX * DX);
//helper point, projection onto the normal
FX := DD * (NPX * DX * DX + EX * DY * DY - DY * DX * EY + DX * DY * NPY);
FY := DD * (-DX * DY * EX + DX * DX * EY + DX * NPX * DY + DY * DY * NPY);
//mirrored point
NPX := NPX + 2 * (EX - FX);
NPY := NPY + 2 * (EY - FY);
//new velocity components
DD := V / Hypot(NPX - EX, NPY - EY);
VX := (NPX - EX) * DD;
VY := (NPY - EY) * DD;
end;
//new position
PX := NPX;
PY := NPY;
//mark new position
Canvas.Ellipse(Round(PX - 1 + CX), Round(PY - 1 + CY),
Round(PX + 1 + CX), Round(PY + 1 + CY));
end;
A = 125, B = 100
Начните с центра эллипса (левый снимок) и с правой точки фокусировки (правый снимок), мяч достигнет левого фокуса, затем вернется к правому фокусу