From 6a58fcca7e517714fd53543f194944d920867e2e Mon Sep 17 00:00:00 2001 From: Frank v/d Haterd Date: Wed, 18 Dec 2013 15:39:06 +0100 Subject: getTangent 2.1 finished --- src/RaceTrack.java | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/RaceTrack.java b/src/RaceTrack.java index 26119ab..c973fb5 100644 --- a/src/RaceTrack.java +++ b/src/RaceTrack.java @@ -6,6 +6,8 @@ import static java.lang.Math.*; * Implementation of a race track that is made from Bezier segments. */ class RaceTrack { + protected static final int ellipse_a = 10; + protected static final int ellipse_b = 14; /** * Array with control points for the O-track. @@ -32,6 +34,8 @@ class RaceTrack { */ public RaceTrack() { // code goes here ... + + } /** @@ -57,8 +61,8 @@ class RaceTrack { * Returns the position of the curve at 0 <= {@code t} <= 1. */ public Vector getPoint(double t) { - return new Vector(10 * cos(2 * PI * t), - 14 * sin(2 * PI * t), + return new Vector(ellipse_a * cos(2 * PI * t), + ellipse_b * sin(2 * PI * t), 1); } @@ -66,9 +70,26 @@ class RaceTrack { * Returns the tangent of the curve at 0 <= {@code t} <= 1. */ public Vector getTangent(double t) { - // robot looks forward - return new Vector(0, - 1, - 1); + /* + * 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), + 0).normalized(); } } -- cgit v1.2.1