Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Curve.java
1461 views
package org.newdawn.slick.geom;12/**3* A beizer curve implementation. The curve is defined by a start point, an end point4* and two control points that it will tend towards. This is implementation is fixed5* segmenting meaning it doesn't scale too well.6*7* @author kevin8*/9public class Curve extends Shape {10/** The start point of the curve */11private Vector2f p1;12/** The first control point */13private Vector2f c1;14/** The second control point */15private Vector2f c2;16/** The end point of the curve */17private Vector2f p2;18/** The number of lines segments the curve is built out of */19private int segments;2021/**22* Create a new curve with the default segments (20)23*24* @param p1 The start of the curve25* @param c1 The first control point26* @param c2 The second control point27* @param p2 The end of the curve28*/29public Curve(Vector2f p1, Vector2f c1, Vector2f c2, Vector2f p2) {30this(p1,c1,c2,p2,20);31}3233/**34* Create a new curve35*36* @param p1 The start of the curve37* @param c1 The first control point38* @param c2 The second control point39* @param p2 The end of the curve40* @param segments The number of segments to use41*/42public Curve(Vector2f p1, Vector2f c1, Vector2f c2, Vector2f p2, int segments) {43this.p1 = new Vector2f(p1);44this.c1 = new Vector2f(c1);45this.c2 = new Vector2f(c2);46this.p2 = new Vector2f(p2);4748this.segments = segments;49pointsDirty = true;50}5152/**53* Get the point at a particular location on the curve54*55* @param t A value between 0 and 1 defining the location of the curve the point is at56* @return The point on the curve57*/58public Vector2f pointAt(float t) {59float a = 1 - t;60float b = t;6162float f1 = a * a * a;63float f2 = 3 * a * a * b;64float f3 = 3 * a * b * b;65float f4 = b * b * b;6667float nx = (p1.x * f1) + (c1.x * f2) + (c2.x * f3) + (p2.x * f4);68float ny = (p1.y * f1) + (c1.y * f2) + (c2.y * f3) + (p2.y * f4);6970return new Vector2f(nx,ny);71}7273/**74* @see org.newdawn.slick.geom.Shape#createPoints()75*/76protected void createPoints() {77float step = 1.0f / segments;78points = new float[(segments+1) * 2];79for (int i=0;i<segments+1;i++) {80float t = i * step;8182Vector2f p = pointAt(t);83points[i*2] = p.x;84points[(i*2)+1] = p.y;85}86}8788/**89* @see org.newdawn.slick.geom.Shape#transform(org.newdawn.slick.geom.Transform)90*/91public Shape transform(Transform transform) {92float[] pts = new float[8];93float[] dest = new float[8];94pts[0] = p1.x; pts[1] = p1.y;95pts[2] = c1.x; pts[3] = c1.y;96pts[4] = c2.x; pts[5] = c2.y;97pts[6] = p2.x; pts[7] = p2.y;98transform.transform(pts, 0, dest, 0, 4);99100return new Curve(new Vector2f(dest[0],dest[1]), new Vector2f(dest[2],dest[3]),101new Vector2f(dest[4],dest[5]), new Vector2f(dest[6],dest[7]));102}103104/**105* True if this is a closed shape106*107* @return True if this is a closed shape108*/109public boolean closed() {110return false;111}112}113114115