1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
import robotrace.Vector;
import static java.lang.Math.*;
/**
* Implementation of a race track that is made from Bezier segments.
*/
class RaceTrack {
/**
* Halfwidth of the ellipse.
*/
protected static final int ELLIPSE_A = 10;
/**
* Halfheight of the ellipse.
*/
protected static final int ELLIPSE_B = 14;
/**
* Array with control points for the O-track.
*/
private Vector[] controlPointsOTrack;
/**
* Array with control points for the L-track.
*/
private Vector[] controlPointsLTrack;
/**
* Array with control points for the C-track.
*/
private Vector[] controlPointsCTrack;
/**
* Array with control points for the custom track.
*/
private Vector[] controlPointsCustomTrack;
/**
* Constructs the race track, sets up display lists.
*/
public RaceTrack() {
// code goes here ...
}
/**
* Draws this track, based on the selected track number.
*/
public void draw(int trackNr) {
// The test track is selected
if (0 == trackNr) {
// code goes here ...
} else if (1 == trackNr) { // The O-track is selected
// code goes here ...
} else if (2 == trackNr) { // The L-track is selected
// code goes here ...
} else if (3 == trackNr) { // The C-track is selected
// code goes here ...
} else if (4 == trackNr) { // The custom track is selected
// code goes here ...
}
}
/**
* Returns the position of the curve at 0 <= {@code t} <= 1.
*/
public Vector getPoint(double t) {
return new Vector(ELLIPSE_A * cos(2 * PI * t),
ELLIPSE_B * sin(2 * PI * t),
1);
}
/**
* Returns the tangent of the curve at 0 <= {@code t} <= 1.
*/
public Vector getTangent(double t) {
/*
* 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();
}
}
|