From 499778373f172dad7b2d0a3eabf335283ae1e274 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 17 Jan 2014 15:36:28 +0100 Subject: Fix broken tangent calculation of ellipse --- src/RaceTrack.java | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/RaceTrack.java b/src/RaceTrack.java index 5c5b842..81fdc99 100644 --- a/src/RaceTrack.java +++ b/src/RaceTrack.java @@ -181,25 +181,11 @@ class RaceTrack extends BetterBase { return getCubicBezierTng(u, selectedControlPoints, bezier_start_i); } - /* - * Given a vector (-Y/B^2, X/A^2, 0) where X and Y are the coordinates - * of the point p on the ellipse. A is the HALFWIDTH of the ellipse and - * B is the HALFHEIGHT of the ellipse. - * - * Vector (X/A^2, Y/B^2, 0) is the normal vector through point p - * and the center of the ellipse. Because the X and Y coordinates - * are divided by the width and height of the ellipse, everything is - * "normalized" to a circle. Hence a line through the origin and a point - * p describes a normal vector for a point p on the ellipse. - * - * Since the dot product of the latter vector and the first (tangent) - * vector results in zero, we can say the normal vector is perpendicular - * to the tangent vector. And because of that, the first vector - * describes the tangent vector of point p. - */ Vector p = getPoint(t); - return new Vector(-p.y() / (ELLIPSE_A * ELLIPSE_A), - p.x() / (ELLIPSE_B * ELLIPSE_B), + // tangent is derivative of ellipse: + // d / dt (A cos(t)) / (B sin(t)) = (-A sin(t)) / (B cos(t)) + return new Vector(-ELLIPSE_A * sin(2 * PI * t), + ELLIPSE_B * cos(2 * PI * t), 0).normalized(); } -- cgit v1.2.1