Class Spline2
A bezier spline is a sequence of beziers, where the start of one is the beginning of the other. A bezier spline may be open or closed. In a closed spline, the end of the last bezier is the beginning of the first (or in the case of a degenerate bezier, a bezier with the same beginning and end).
A single cubic bezier is represented by the four points. There are the two anchor points P1 and P2. These represent the start and end of the curve. In addition, each of these points has a tangent: T1 and T2. The curve is defined so that P1 has a right tangent of T1, and P2 has a left tangent of T2. The tangents themselves are given as points, not vectors (so the tangent vector is Tn-Pn). These four points are known as the control points. When we represent a bezier, we typically represent it as a list of four points in this order: P1, T1, T2, and P2.
In a bezier spline, the first anchor point of the next curve is the same as the last anchor point of the previous one. There is no need to duplicate this information. However, the tanget is not a duplicate, since anchor points on the interior of the spline have both a left and right tangent. Therefore, the control point list always has two tangents between any two anchors. Thus bezier spline of n beziers will contain 3n+1 control points.
This class does not contain any drawing functionality at all. If you wish to
draw a bezier, create a Path2 with the SplinePather factory.
This factory creates a line-segment approximation of the spline, in much the
same way that we approximate circles or ellipses when drawing them. You can
then use the many features for drawing a path object, such as extrusion or
wireframes.
This class has a lot of advanced methods to detect the nearest anchor, tanget, or curve location to a point. These are designed so that you can edit a bezier in a level editor for your game. These methods determine the part of the bezier closest to you mouse location, so that you can select and edit them.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSpline2()Creates an empty spline.Spline2(float[] points) Creates a spline from the given control points.Spline2(float[] points, int poff, int psize) Creates a spline from the given control points.Spline2(float x, float y) Creates a degenerate spline of one pointSpline2(float sx, float sy, float ex, float ey) Creates a spline of two pointsSpline2(com.badlogic.gdx.math.Vector2 point) Creates a degenerate spline of one pointSpline2(com.badlogic.gdx.math.Vector2 start, com.badlogic.gdx.math.Vector2 end) Creates a spline of two pointsCreates a copy of the given spline. -
Method Summary
Modifier and TypeMethodDescriptionintaddAnchor(float x, float y) Adds the given point to the end of the spline, creating a new segment.intaddAnchor(float px, float py, float tx, float ty) Adds the given point to the end of the spline, creating a new segment.intaddAnchor(com.badlogic.gdx.math.Vector2 point) Adds the given point to the end of the spline, creating a new segment.intaddAnchor(com.badlogic.gdx.math.Vector2 point, com.badlogic.gdx.math.Vector2 tang) Adds the given point to the end of the spline, creating a new segment.intaddBezier(float c1x, float c1y, float c2x, float c2y, float px, float py) Adds a (cubic) bezier path from the end of the spline to point.intaddBezier(com.badlogic.gdx.math.Vector2 control1, com.badlogic.gdx.math.Vector2 control2, com.badlogic.gdx.math.Vector2 point) Adds a (cubic) bezier path from the end of the spline to point.intaddQuad(float cx, float cy, float px, float py) Adds a (quadratic) bezier path from the end of the spline to point.intaddQuad(com.badlogic.gdx.math.Vector2 control, com.badlogic.gdx.math.Vector2 point) Adds a (quadratic) bezier path from the end of the spline to point.voidclear()Clears all control points, producing a degenerate spline.voiddeleteAnchor(int index) Deletes the anchor point at the given index.com.badlogic.gdx.utils.FloatArrayReturns the spline control points.com.badlogic.gdx.math.Vector2getPoint(float tp) Returns the spline point for parameter tp.com.badlogic.gdx.math.Vector2getPoint(float tp, com.badlogic.gdx.math.Vector2 point) Returns the spline point for parameter tp.com.badlogic.gdx.math.Vector2getTangent(int index) Returns the tangent at the given index.com.badlogic.gdx.math.Vector2getTangent(int index, com.badlogic.gdx.math.Vector2 point) Returns the tangent at the given index.voidinsertAnchor(float param) Inserts a new anchor point at parameter tp.booleanisClosed()Returns true if the spline is closed.booleanisSmooth(int index) Returns the smoothness for the anchor point at the given index.intnearestAnchor(float x, float y, float threshold) Returns the index of the anchor nearest the given point.intnearestAnchor(com.badlogic.gdx.math.Vector2 point, float threshold) Returns the index of the anchor nearest the given point.floatnearestParameter(float x, float y) Returns the parameterization of the nearest point on the spline.floatnearestParameter(com.badlogic.gdx.math.Vector2 point) Returns the parameterization of the nearest point on the spline.com.badlogic.gdx.math.Vector2nearestPoint(com.badlogic.gdx.math.Vector2 point) Returns the nearest point on the spline to the given point.com.badlogic.gdx.math.Vector2nearestPoint(com.badlogic.gdx.math.Vector2 point, com.badlogic.gdx.math.Vector2 result) Returns the nearest point on the spline to the given point.intnearestTangent(float x, float y, float threshold) Returns the index of the tangent nearest the given point.intnearestTangent(com.badlogic.gdx.math.Vector2 point, float threshold) Returns the index of the tangent nearest the given point.set(float[] points) Sets this spline to have the given control points.set(float[] points, int poff, int psize) Sets this spline to have the given control points.set(float x, float y) Sets this spline to be a degenerate degenerate of one pointset(float sx, float sy, float ex, float ey) Sets this spline to be a line between two pointsset(com.badlogic.gdx.math.Vector2 point) Sets this spline to be a degenerate degenerate of one pointset(com.badlogic.gdx.math.Vector2 start, com.badlogic.gdx.math.Vector2 end) Sets this spline to be a line between two pointsSets this spline to be a copy of the given spline.voidsetAnchor(int index, float x, float y) Sets the anchor point at the given index.voidsetAnchor(int index, com.badlogic.gdx.math.Vector2 point) Sets the anchor point at the given index.voidsetClosed(boolean flag) Sets whether the spline is closed.voidsetPoint(float tp, float x, float y) Sets the spline point at parameter tp.voidsetPoint(float tp, com.badlogic.gdx.math.Vector2 point) Sets the spline point at parameter tp.voidsetSmooth(int index, boolean flag) Sets the smoothness for the anchor point at the given index.voidsetTangent(int index, float x, float y, boolean symmetric) Sets the tangent at the given index.voidsetTangent(int index, com.badlogic.gdx.math.Vector2 tang, boolean symmetric) Sets the tangent at the given index.intsize()Returns the number of segments in this spline
-
Field Details
-
smooth
public com.badlogic.gdx.utils.BooleanArray smoothFor each anchor point in the spline, whether it is a smooth point or a hinge point. -
closed
public boolean closedWhether the spline is closed. This effects editing and polygon approximation
-
-
Constructor Details
-
Spline2
public Spline2()Creates an empty spline.This is a (super) degenerate spline with no control points; it is open.
-
Spline2
public Spline2(com.badlogic.gdx.math.Vector2 point) Creates a degenerate spline of one pointThis resulting spline consists of a single point, but it is still size 0. That is because it has no segments (and as a degenerate spline, it is open).
This constructor is useful for building up a spline incrementally.
- Parameters:
point- The bezier anchor point
-
Spline2
public Spline2(float x, float y) Creates a degenerate spline of one pointThis resulting spline consists of a single point, but it is still size 0. That is because it has no segments (and as a degenerate spline, it is open).
This constructor is useful for building up a spline incrementally.
- Parameters:
x- The x-coordiante of the bezier anchor pointy- The y-coordiante of the bezier anchor point
-
Spline2
public Spline2(com.badlogic.gdx.math.Vector2 start, com.badlogic.gdx.math.Vector2 end) Creates a spline of two pointsThe minimum spline possible has 4 points: two anchors and two tangents. This sets the start to be the first anchor point, and end to be the second. The tangents, are the same as the anchor points, which means that the tangents are degenerate. This has the effect of making the bezier a straight line from start to end. The spline is open, unless start and end are the same.
- Parameters:
start- The first bezier anchor pointend- The second bezier anchor point
-
Spline2
public Spline2(float sx, float sy, float ex, float ey) Creates a spline of two pointsThe minimum spline possible has 4 points: two anchors and two tangents. This sets the start to be the first anchor point, and end to be the second. The tangents, are the same as the anchor points, which means that the tangents are degenerate. This has the effect of making the bezier a straight line from start to end. The spline is open, unless start and end are the same.
- Parameters:
sx- The x-coordinate of the first bezier anchor pointsy- The y-coordinate of the first bezier anchor pointex- The x-coordinate of the second bezier anchor pointey- The y-coordinate of the second bezier anchor point
-
Spline2
public Spline2(float[] points) Creates a spline from the given control points.The control points must be specified in the form
anchor, tangent, tangent, anchor, tangent ... anchor
That is, starts and ends with anchors, and every two anchors have two tangents (right of the first, left of the second) in between. The size of this vector must be equal to 1 mod 3.The created spline is open.
- Parameters:
points- The vector of control points
-
Spline2
public Spline2(float[] points, int poff, int psize) Creates a spline from the given control points.The control points must be specified in the form
anchor, tangent, tangent, anchor, tangent ... anchor
That is, starts and ends with anchors, and every two anchors have two tangents (right of the first, left of the second) in between. The size of this vector must be equal to 1 mod 3.The created spline is open.
- Parameters:
points- The array of control pointspoff- The offset into the arraypsize- The lenght of the array
-
Spline2
Creates a copy of the given spline.- Parameters:
spline- The spline to copy
-
-
Method Details
-
set
Sets this spline to be a degenerate degenerate of one pointThis resulting spline consists of a single point, but it is still size 0. That is because it has no segments (and as a degenerate spline, it is open).
This assignment is useful for building up a spline incrementally.
- Parameters:
point- The bezier anchor point- Returns:
- This spline, returned for chaining
-
set
Sets this spline to be a degenerate degenerate of one pointThis resulting spline consists of a single point, but it is still size 0. That is because it has no segments (and as a degenerate spline, it is open).
This assignment is useful for building up a spline incrementally.
- Parameters:
x- The x-coordinate of the bezier anchor pointy- The y-coordinate of the bezier anchor point- Returns:
- This spline, returned for chaining
-
set
Sets this spline to be a line between two pointsThe minimum spline possible has 4 points: two anchors and two tangents. This sets the start to be the first anchor point, and end to be the second. The tangents, are the same as the anchor points, which means that the tangents are degenerate. This has the effect of making the bezier a straight line from start to end. The spline is open, unless start and end are the same.
- Parameters:
start- The first bezier anchor pointend- The second bezier anchor point- Returns:
- This spline, returned for chaining
-
set
Sets this spline to be a line between two pointsThe minimum spline possible has 4 points: two anchors and two tangents. This sets the start to be the first anchor point, and end to be the second. The tangents, are the same as the anchor points, which means that the tangents are degenerate. This has the effect of making the bezier a straight line from start to end. The spline is open, unless start and end are the same.
- Parameters:
sx- The x-coordinate of the first bezier anchor pointsy- The y-coordinate of the first bezier anchor pointex- The x-coordinate of the second bezier anchor pointey- The y-coordinate of the second bezier anchor point- Returns:
- This spline, returned for chaining
-
set
Sets this spline to have the given control points.The control points must be specified in the form
anchor, tangent, tangent, anchor, tangent ... anchor
That is, starts and ends with anchors, and every two anchors have two tangents (right of the first, left of the second) in between. The size of this vector must be equal to 1 mod 3.This method makes the spline is open.
- Parameters:
points- The vector of control points- Returns:
- This spline, returned for chaining
-
set
Sets this spline to have the given control points.The control points must be specified in the form
anchor, tangent, tangent, anchor, tangent ... anchor
That is, starts and ends with anchors, and every two anchors have two tangents (right of the first, left of the second) in between. The size of this vector must be equal to 1 mod 3.This method makes the spline is open.
- Parameters:
points- The vector of control pointspoff- The offset into the arraypsize- The lenght of the array- Returns:
- This spline, returned for chaining
-
set
Sets this spline to be a copy of the given spline.- Parameters:
spline- The spline to copy- Returns:
- This spline, returned for chaining
-
setClosed
public void setClosed(boolean flag) Sets whether the spline is closed.A closed spline is one where the first and last anchor are the same. Hence the first and last tangents are tangents (right, and left, respectively) of the same point. This is relevant for the setTangent() method, particularly if the change is meant to be symmetric.
When closing a spline, the end point is not a smooth point. This can be changed by calling
setSmooth(int, boolean).A closed spline has no end. Therefore, anchors cannot be added to a closed spline. They may only be inserted between two other anchors.
- Parameters:
flag- Whether the spline is closed
-
size
public int size()Returns the number of segments in this splineEach segment is a bezier. To use the bezier methods associated with this class, you will need to know the correct segment.
- Returns:
- the number of segments in this spline
-
isClosed
public boolean isClosed()Returns true if the spline is closed.A closed spline is one where the first and last anchor are the same. Hence the first and last tangents are tangents (right, and left, respectively) of the same point. This is relevant for the setTangent() method, particularly if the change is meant to be symmetric.
When closing a spline, the end point is not a smooth point. This can be changed by calling
setSmooth(int, boolean).A closed spline has no end. Therefore, anchors cannot be added to a closed spline. They may only be inserted between two other anchors.
- Returns:
- true if the spline is closed
-
getPoint
public com.badlogic.gdx.math.Vector2 getPoint(float tp) Returns the spline point for parameter tp.A bezier spline is a parameterized curve. For a single bezier, it is parameterized with tp in 0..1, with tp = 0 representing the first anchor and tp = 1 representing the second. In the spline, we generalize this idea, where tp is an anchor if it is an int, and is inbetween the anchors floor(tp) and ceil(tp) otherwise.
- Parameters:
tp- the parameterization value- Returns:
- the spline point for parameter tp
-
getPoint
public com.badlogic.gdx.math.Vector2 getPoint(float tp, com.badlogic.gdx.math.Vector2 point) Returns the spline point for parameter tp.A bezier spline is a parameterized curve. For a single bezier, it is parameterized with tp in 0..1, with tp = 0 representing the first anchor and tp = 1 representing the second. In the spline, we generalize this idea, where tp is an anchor if it is an int, and is inbetween the anchors floor(tp) and ceil(tp) otherwise.
The vector object returned is the one provided, unless it is null.
- Parameters:
tp- the parameterization valuepoint- the vector to store the result- Returns:
- the spline point for parameter tp
-
setPoint
public void setPoint(float tp, com.badlogic.gdx.math.Vector2 point) Sets the spline point at parameter tp.A bezier spline is a parameterized curve. For a single bezier, it is parameterized with tp in 0..1, with tp = 0 representing the first anchor and tp = 1 representing the second. In the spline, we generalize this idea, where tp is an anchor if it is an int, and is inbetween the anchors floor(tp) and ceil(tp) otherwise.
In this method, if tp is an int, it will just reassign the associated anchor value. Otherwise, this will insert a new anchor point at that parameter. This has a side-effect of changing the parameterization values for the curve, as the number of beziers has increased.
- Parameters:
tp- the parameterization valuepoint- the new value to assign
-
setPoint
public void setPoint(float tp, float x, float y) Sets the spline point at parameter tp.A bezier spline is a parameterized curve. For a single bezier, it is parameterized with tp in 0..1, with tp = 0 representing the first anchor and tp = 1 representing the second. In the spline, we generalize this idea, where tp is an anchor if it is an int, and is inbetween the anchors floor(tp) and ceil(tp) otherwise.
In this method, if tp is an int, it will just reassign the associated anchor value. Otherwise, this will insert a new anchor point at that parameter. This has a side-effect of changing the parameterization values for the curve, as the number of beziers has increased.
- Parameters:
tp- the parameterization valuex- the new x-coordinate to assigny- the new y-coordinate to assign
-
setAnchor
public void setAnchor(int index, com.badlogic.gdx.math.Vector2 point) Sets the anchor point at the given index.This method will change both the anchor and its associated tangets. The new tangents will have the same relative change in position. As a result, the bezier will still have the same shape locally. This is the natural behavior for changing an anchor, as seen in Adobe Illustrator.
If an open spline has n segments, then it has n+1 anchors. Similiarly, a closed spline had n anchors. The value index should be in the appropriate range.
- Parameters:
index- the anchor index (0..n+1 or 0..n)point- the new value to assign
-
setAnchor
public void setAnchor(int index, float x, float y) Sets the anchor point at the given index.This method will change both the anchor and its associated tangets. The new tangents will have the same relative change in position. As a result, the bezier will still have the same shape locally. This is the natural behavior for changing an anchor, as seen in Adobe Illustrator.
If an open spline has n segments, then it has n+1 anchors. Similiarly, a closed spline had n anchors. The value index should be in the appropriate range.
- Parameters:
index- the anchor index (0..n+1 or 0..n)x- the new x-coordinate to assigny- the new y-coordinate to assign
-
isSmooth
public boolean isSmooth(int index) Returns the smoothness for the anchor point at the given index.A smooth anchor is one in which the derivative of the curve at the anchor is continuous. Practically, this means that the left and right tangents are always parallel. Only a non-smooth anchor may form a "hinge".
If an open spline has n segments, then it has n+1 anchors. However, the two end anchors can never be smooth. A closed spline with the same number of segments has n anchors, but all anchors have the potential to be smooth.
- Parameters:
index- the anchor index (0..n+1 or 0..n)- Returns:
- the smoothness for the anchor point at the given index.
-
setSmooth
public void setSmooth(int index, boolean flag) Sets the smoothness for the anchor point at the given index.A smooth anchor is one in which the derivative of the curve at the anchor is continuous. Practically, this means that the left and right tangents are always parallel. Only a non-smooth anchor may form a "hinge".
If you set a non-smooth anchor to smooth, it will adjust the tangents accordingly. In particular, it will average the two tangents, making them parallel. This is the natural behavior for changing an smoothness, as seen in Adobe Illustrator.
If an open spline has n segments, then it has n+1 anchors. However, the two end anchors can never be smooth. A closed spline with the same number of segments has n anchors, but all anchors have the potential to be smooth.
- Parameters:
index- the anchor index (0..n+1 or 0..n)flag- the anchor smoothness
-
getTangent
public com.badlogic.gdx.math.Vector2 getTangent(int index) Returns the tangent at the given index.Tangents are specified as points, not vectors. To get the tangent vector for an anchor, you must subtract the anchor from its tangent point. Hence a curve is degenerate when the tangent and the anchor are the same.
If a spline has n segments, then it has 2n tangents. This is true regardless of whether it is open or closed. The value index should be in the appropriate range. An even index is a right tangent, while an odd index is a left tangent. If the spline is closed, then 2n-1 is the left tangent of the first point.
- Parameters:
index- the tangent index (0..2n)- Returns:
- the tangent at the given index.
-
getTangent
public com.badlogic.gdx.math.Vector2 getTangent(int index, com.badlogic.gdx.math.Vector2 point) Returns the tangent at the given index.Tangents are specified as points, not vectors. To get the tangent vector for an anchor, you must subtract the anchor from its tangent point. Hence a curve is degenerate when the tangent and the anchor are the same.
If a spline has n segments, then it has 2n tangents. This is true regardless of whether it is open or closed. The value index should be in the appropriate range. An even index is a right tangent, while an odd index is a left tangent. If the spline is closed, then 2n-1 is the left tangent of the first point. The vector object returned is the one provided, unless it is null.
- Parameters:
index- the tangent index (0..2n)point- the vector to store the result- Returns:
- the tangent at the given index.
-
setTangent
public void setTangent(int index, com.badlogic.gdx.math.Vector2 tang, boolean symmetric) Sets the tangent at the given index.Tangents are specified as points, not vectors. To get the tangent vector for an anchor, you must subtract the anchor from its tangent point. Hence a curve is degenerate when the tangent and the anchor are the same.
If the associated anchor point is smooth, changing the direction of the tangent vector will also change the direction of the other tangent vector (so that they remain parallel). However, changing only the magnitude will have no effect, unless symmetric is true. In that case, it will modify the other tangent so that it has the same magnitude and parallel direction. This is the natural behavior for changing a tangent, as seen in Adobe Illustrator.
If a spline has n segments, then it has 2n tangents. This is true regardless of whether it is open or closed. The value index should be in the appropriate range. An even index is a right tangent, while an odd index is a left tangent. If the spline is closed, then 2n-1 is the left tangent of the first point.
- Parameters:
index- the tangent index (0..2n)tang- the new value to assignsymmetric- whether to make the other tangent symmetric
-
setTangent
public void setTangent(int index, float x, float y, boolean symmetric) Sets the tangent at the given index.Tangents are specified as points, not vectors. To get the tangent vector for an anchor, you must subtract the anchor from its tangent point. Hence a curve is degenerate when the tangent and the anchor are the same.
If the associated anchor point is smooth, changing the direction of the tangent vector will also change the direction of the other tangent vector (so that they remain parallel). However, changing only the magnitude will have no effect, unless symmetric is true. In that case, it will modify the other tangent so that it has the same magnitude and parallel direction. This is the natural behavior for changing a tangent, as seen in Adobe Illustrator.
If a spline has n segments, then it has 2n tangents. This is true regardless of whether it is open or closed. The value index should be in the appropriate range. An even index is a right tangent, while an odd index is a left tangent. If the spline is closed, then 2n-1 is the left tangent of the first point.
- Parameters:
index- the tangent index (0..2n)x- the new x-coordinate to assigny- the new y-coordinate to assignsymmetric- whether to make the other tangent symmetric
-
getControlPoints
public com.badlogic.gdx.utils.FloatArray getControlPoints()Returns the spline control points.If the spline has n segments, then the list will have 6n+2 elements in it, representing the n+1 anchor points and the 2n tangents. The values will alternate
anchor, tangent, tangent, anchor, tangent ... anchor
This is true even if the curve is closed. In that case, the first and last anchor points will be the same.- Returns:
- the spline control points.
-
addAnchor
public int addAnchor(com.badlogic.gdx.math.Vector2 point, com.badlogic.gdx.math.Vector2 tang) Adds the given point to the end of the spline, creating a new segment.Assuming that the spline is open, the new segment will start at the end of the previous last segment and extend it to the given point. The value tang is the left tangent of the new anchor point.
As the previous end point could not have been smooth, it will remain that way (so the right tangent of that point will be degenerate -- equal to the anchor). Use
setSmooth(int, boolean)to change the characteristic of an interior point. If the spline was super degenerate (there were no control points at all), then this method will simply add the given anchor, but keep the segment size as 0.As closed splines have no end, this method will fail on closed splines. You should use
insertAnchor(int, float)instead for closed splines.- Parameters:
point- the new anchor point to add to the endtang- the left tangent of the new anchor point- Returns:
- the new number of segments in this spline
-
addAnchor
public int addAnchor(float px, float py, float tx, float ty) Adds the given point to the end of the spline, creating a new segment.Assuming that the spline is open, the new segment will start at the end of the previous last segment and extend it to the given point. The value tang is the left tangent of the new anchor point.
As the previous end point could not have been smooth, it will remain that way (so the right tangent of that point will be degenerate -- equal to the anchor). Use
setSmooth(int, boolean)to change the characteristic of an interior point. If the spline was super degenerate (there were no control points at all), then this method will simply add the given anchor, but keep the segment size as 0.As closed splines have no end, this method will fail on closed splines. You should use
insertAnchor(int, float)instead for closed splines.- Parameters:
px- the new anchor x-coordinate to add to the endpy- the new anchor y-coordinate to add to the endtx- the x-coordinate of the left tangentty- the y-coordinate of the left tangent- Returns:
- the new number of segments in this spline
-
addAnchor
public int addAnchor(com.badlogic.gdx.math.Vector2 point) Adds the given point to the end of the spline, creating a new segment.Assuming that the spline is open, the new segment will start at the end of the previous last segment and extend it to the given point. This method will add a degenerate left tangent (e.g the tangent point is the same as an anchor point).
As the previous end point could not have been smooth, it will remain that way (so the right tangent of that point will be degenerate -- equal to the anchor). Use
setSmooth(int, boolean)to change the characteristic of an interior point. If the spline was super degenerate (there were no control points at all), then this method will simply add the given anchor, but keep the segment size as 0.As closed splines have no end, this method will fail on closed splines. You should use
insertAnchor(int, float)instead for closed splines.- Parameters:
point- The new anchor point to add to the end- Returns:
- the new number of segments in this spline
-
addAnchor
public int addAnchor(float x, float y) Adds the given point to the end of the spline, creating a new segment.Assuming that the spline is open, the new segment will start at the end of the previous last segment and extend it to the given point. This method will add a degenerate left tangent (e.g the tangent point is the same as an anchor point).
As the previous end point could not have been smooth, it will remain that way (so the right tangent of that point will be degenerate -- equal to the anchor). Use
setSmooth(int, boolean)to change the characteristic of an interior point. If the spline was super degenerate (there were no control points at all), then this method will simply add the given anchor, but keep the segment size as 0.As closed splines have no end, this method will fail on closed splines. You should use
insertAnchor(int, float)instead for closed splines.- Parameters:
x- The new anchor x-coordinate to add to the endy- The new anchor y-coordinate to add to the end- Returns:
- the new number of segments in this spline
-
addBezier
public int addBezier(com.badlogic.gdx.math.Vector2 control1, com.badlogic.gdx.math.Vector2 control2, com.badlogic.gdx.math.Vector2 point) Adds a (cubic) bezier path from the end of the spline to point.Assuming that the spline is open, the new segment will start at the end of the previous last segment and extend it to the given point. The given control points will define the right tangent of the previous end point, and the left tangent of point, respectively.
This method will compute whether or not the previous end point is smooth from the new right tangent. If the spline was super degenerate (there were no control points at all), then this method will build the bezier from the origin.
As closed splines have no end, this method will fail on closed splines.
- Parameters:
control1- The first bezier control pointcontrol2- The second bezier control pointpoint- The new anchor point- Returns:
- the new number of segments in this spline
-
addBezier
public int addBezier(float c1x, float c1y, float c2x, float c2y, float px, float py) Adds a (cubic) bezier path from the end of the spline to point.Assuming that the spline is open, the new segment will start at the end of the previous last segment and extend it to the given point. The given control points will define the right tangent of the previous end point, and the left tangent of point, respectively.
This method will compute whether or not the previous end point is smooth from the new right tangent. If the spline was super degenerate (there were no control points at all), then this method will build the bezier from the origin.
As closed splines have no end, this method will fail on closed splines.
- Parameters:
c1x- The x-coordiante of the first bezier control pointc1y- The y-coordiante of the first bezier control pointc2x- The x-coordiante of the second bezier control pointc2y- The y-coordiante of the second bezier control pointpx- The x-coordinate of the new anchor pointpy- The y-coordinate of the new anchor point- Returns:
- the new number of segments in this spline
-
addQuad
public int addQuad(com.badlogic.gdx.math.Vector2 control, com.badlogic.gdx.math.Vector2 point) Adds a (quadratic) bezier path from the end of the spline to point.Assuming that the spline is open, the new segment will start at the end of the previous last segment and extend it to the given point. The new right and left tangents will be computed from the given quadratic control point.
This method will compute whether or not the previous end point is smooth from the new right tangent. If the spline was super degenerate (there were no control points at all), then this method will build the bezier from the origin.
As closed splines have no end, this method will fail on closed splines.
- Parameters:
control- The quadratic control pointpoint- The new anchor point- Returns:
- the new number of segments in this spline
-
addQuad
public int addQuad(float cx, float cy, float px, float py) Adds a (quadratic) bezier path from the end of the spline to point.Assuming that the spline is open, the new segment will start at the end of the previous last segment and extend it to the given point. The new right and left tangents will be computed from the given quadratic control point.
This method will compute whether or not the previous end point is smooth from the new right tangent. If the spline was super degenerate (there were no control points at all), then this method will build the bezier from the origin.
As closed splines have no end, this method will fail on closed splines.
- Parameters:
cx- The x-coordinate of the quadratic control pointcy- The y-coordinate of the quadratic control pointpx- The x-coordinate of the new anchor pointpy- The y-coordinate of the new anchor point- Returns:
- the new number of segments in this spline
-
deleteAnchor
public void deleteAnchor(int index) Deletes the anchor point at the given index.The point is deleted as well as both of its tangents (left and right). All remaining anchors after the deleted one will shift their indices down by one. Deletion is allowed on closed splines; the spline will remain closed after deletion.
If an open spline has n segments, then it has n+1 anchors. Similiarly, a closed spline had n anchors. The value index should be in the appropriate range.
- Parameters:
index- the anchor index to delete
-
insertAnchor
public void insertAnchor(float param) Inserts a new anchor point at parameter tp.Inserting an anchor point does not change the curve. It just makes an existing point that was not an anchor, now an anchor. This is the natural behavior for inserting an index, as seen in Adobe Illustrator.
A bezier spline is a parameterized curve. For a single bezier, it is parameterized with tp in 0..1, with tp = 0 representing the first anchor and tp = 1 representing the second. In the spline, we generalize this idea, where tp is an anchor if it is an int, and is inbetween the anchors floor(tp) and ceil(tp) otherwise.
The tangents of the new anchor point will be determined by de Castlejau's. This is the natural behavior for inserting an anchor mid bezier, as seen in Adobe Illustrator.
- Parameters:
param- the parameterization value
-
nearestPoint
public com.badlogic.gdx.math.Vector2 nearestPoint(com.badlogic.gdx.math.Vector2 point) Returns the nearest point on the spline to the given point.The value is effectively the projection of the point onto the curve. We compute this point using the projection polynomial, described at
http://jazzros.blogspot.com/2011/03/projecting-point-on-bezier-curve.html
The point returned does not need to be an anchor point. It can be anywhere on the curve. This allows us a way to select a non-anchor point with the mouse (such as to add a new anchor point) in a level editor or other program.- Parameters:
point- the point to project- Returns:
- the nearest point on the spline to the given point.
-
nearestPoint
public com.badlogic.gdx.math.Vector2 nearestPoint(com.badlogic.gdx.math.Vector2 point, com.badlogic.gdx.math.Vector2 result) Returns the nearest point on the spline to the given point.The value is effectively the projection of the point onto the curve. We compute this point using the projection polynomial, described at
http://jazzros.blogspot.com/2011/03/projecting-point-on-bezier-curve.html
The point returned does not need to be an anchor point. It can be anywhere on the curve. This allows us a way to select a non-anchor point with the mouse (such as to add a new anchor point) in a level editor or other program. The vector object returned is the one provided, unless it is null.- Parameters:
point- the point to projectresult- the object to store the result- Returns:
- the nearest point on the spline to the given point.
-
nearestParameter
public float nearestParameter(com.badlogic.gdx.math.Vector2 point) Returns the parameterization of the nearest point on the spline.The value is effectively the projection of the point onto the parametrized curve. See getPoint() for an explanation of how the parameterization work. We compute this value using the projection polynomial, described at
http://jazzros.blogspot.com/2011/03/projecting-point-on-bezier-curve.html
- Parameters:
point- the point to project- Returns:
- the parameterization of the nearest point on the spline.
-
nearestParameter
public float nearestParameter(float x, float y) Returns the parameterization of the nearest point on the spline.The value is effectively the projection of the point onto the parametrized curve. See getPoint() for an explanation of how the parameterization work. We compute this value using the projection polynomial, described at
http://jazzros.blogspot.com/2011/03/projecting-point-on-bezier-curve.html
- Parameters:
x- the x-coordinate of the point to projecty- the y-coordinate of the point to project- Returns:
- the parameterization of the nearest point on the spline.
-
nearestAnchor
public int nearestAnchor(com.badlogic.gdx.math.Vector2 point, float threshold) Returns the index of the anchor nearest the given point.If there is no anchor whose distance to point is less than the square root of threshold (we use lengthSquared for speed), then this method returns -1.
- Parameters:
point- the point to comparethreshold- the distance threshold for picking an anchor- Returns:
- the index of the anchor nearest the given point.
-
nearestAnchor
public int nearestAnchor(float x, float y, float threshold) Returns the index of the anchor nearest the given point.If there is no anchor whose distance to point is less than the square root of threshold (we use lengthSquared for speed), then this method returns -1.
- Parameters:
x- the x-coordinate of the point to comparey- the y-coordinate of the point to comparethreshold- the distance threshold for picking an anchor- Returns:
- the index of the anchor nearest the given point.
-
nearestTangent
public int nearestTangent(com.badlogic.gdx.math.Vector2 point, float threshold) Returns the index of the tangent nearest the given point.If there is no tangent whose distance to point is less than the square root of threshold (we use lengthSquared for speed), then this method returns -1.
- Parameters:
point- the point to comparethreshold- the distance threshold for picking a tangent- Returns:
- the index of the tangent nearest the given point.
-
nearestTangent
public int nearestTangent(float x, float y, float threshold) Returns the index of the tangent nearest the given point.If there is no tangent whose distance to point is less than the square root of threshold (we use lengthSquared for speed), then this method returns -1.
- Parameters:
x- the x-coordinate of the point to comparey- the y-coordinate of the point to comparethreshold- the distance threshold for picking a tangent- Returns:
- the index of the tangent nearest the given point.
-
clear
public void clear()Clears all control points, producing a degenerate spline.
-