Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Curve.java
1461 views
1
package org.newdawn.slick.geom;
2
3
/**
4
* A beizer curve implementation. The curve is defined by a start point, an end point
5
* and two control points that it will tend towards. This is implementation is fixed
6
* segmenting meaning it doesn't scale too well.
7
*
8
* @author kevin
9
*/
10
public class Curve extends Shape {
11
/** The start point of the curve */
12
private Vector2f p1;
13
/** The first control point */
14
private Vector2f c1;
15
/** The second control point */
16
private Vector2f c2;
17
/** The end point of the curve */
18
private Vector2f p2;
19
/** The number of lines segments the curve is built out of */
20
private int segments;
21
22
/**
23
* Create a new curve with the default segments (20)
24
*
25
* @param p1 The start of the curve
26
* @param c1 The first control point
27
* @param c2 The second control point
28
* @param p2 The end of the curve
29
*/
30
public Curve(Vector2f p1, Vector2f c1, Vector2f c2, Vector2f p2) {
31
this(p1,c1,c2,p2,20);
32
}
33
34
/**
35
* Create a new curve
36
*
37
* @param p1 The start of the curve
38
* @param c1 The first control point
39
* @param c2 The second control point
40
* @param p2 The end of the curve
41
* @param segments The number of segments to use
42
*/
43
public Curve(Vector2f p1, Vector2f c1, Vector2f c2, Vector2f p2, int segments) {
44
this.p1 = new Vector2f(p1);
45
this.c1 = new Vector2f(c1);
46
this.c2 = new Vector2f(c2);
47
this.p2 = new Vector2f(p2);
48
49
this.segments = segments;
50
pointsDirty = true;
51
}
52
53
/**
54
* Get the point at a particular location on the curve
55
*
56
* @param t A value between 0 and 1 defining the location of the curve the point is at
57
* @return The point on the curve
58
*/
59
public Vector2f pointAt(float t) {
60
float a = 1 - t;
61
float b = t;
62
63
float f1 = a * a * a;
64
float f2 = 3 * a * a * b;
65
float f3 = 3 * a * b * b;
66
float f4 = b * b * b;
67
68
float nx = (p1.x * f1) + (c1.x * f2) + (c2.x * f3) + (p2.x * f4);
69
float ny = (p1.y * f1) + (c1.y * f2) + (c2.y * f3) + (p2.y * f4);
70
71
return new Vector2f(nx,ny);
72
}
73
74
/**
75
* @see org.newdawn.slick.geom.Shape#createPoints()
76
*/
77
protected void createPoints() {
78
float step = 1.0f / segments;
79
points = new float[(segments+1) * 2];
80
for (int i=0;i<segments+1;i++) {
81
float t = i * step;
82
83
Vector2f p = pointAt(t);
84
points[i*2] = p.x;
85
points[(i*2)+1] = p.y;
86
}
87
}
88
89
/**
90
* @see org.newdawn.slick.geom.Shape#transform(org.newdawn.slick.geom.Transform)
91
*/
92
public Shape transform(Transform transform) {
93
float[] pts = new float[8];
94
float[] dest = new float[8];
95
pts[0] = p1.x; pts[1] = p1.y;
96
pts[2] = c1.x; pts[3] = c1.y;
97
pts[4] = c2.x; pts[5] = c2.y;
98
pts[6] = p2.x; pts[7] = p2.y;
99
transform.transform(pts, 0, dest, 0, 4);
100
101
return new Curve(new Vector2f(dest[0],dest[1]), new Vector2f(dest[2],dest[3]),
102
new Vector2f(dest[4],dest[5]), new Vector2f(dest[6],dest[7]));
103
}
104
105
/**
106
* True if this is a closed shape
107
*
108
* @return True if this is a closed shape
109
*/
110
public boolean closed() {
111
return false;
112
}
113
}
114
115