Bezier Curve to Triangle Strip

Started by
9 comments, last by slynk 13 years, 2 months ago
Fixed it, the directional vector worked. Thanks so much ^^

Anyone who comes upon this, here's some working code to create a bezier curve and then convert it into a trianglestrip that is "thickness" wide. ^^

public const int SUBDIVISIONS = 30;
public const int THICKNESS = 30;
public static Color UNLIT = Color.Black;

/*
* Given 2 points and 2 control points, this function will calculate a curve
*/

public static VertexPositionColor[] bezierCurve(Vector2 start, Vector2 end, Vector2 c1, Vector2 c2)
{
VertexPositionColor[] points = new VertexPositionColor[SUBDIVISIONS + 2];

float fraction;

for (int i = 0; i < SUBDIVISIONS + 2; i++)
{
fraction = i * (1f / (float)SUBDIVISIONS);
points = new VertexPositionColor(new Vector3((float)((start.X * Math.Pow((1 - fraction), 3))
+(c1.X * 3 * fraction * Math.Pow(1-fraction, 2))
+(c2.X * 3 * Math.Pow(fraction,2) * (1-fraction))
+(end.X * Math.Pow(fraction,3))),

(float)((start.Y * Math.Pow((1 - fraction), 3))
+ (c1.Y * 3 * fraction * Math.Pow(1 - fraction, 2))
+ (c2.Y * 3 * Math.Pow(fraction, 2) * (1 - fraction))
+ (end.Y * Math.Pow(fraction, 3))), 0), UNLIT);
}

return points;
}

/*
* This function treats the curve as a serious of straight lines and calculates points on a line perpendicular to each point, resulting in two points THICKNESS appart.
* Requires THICKNESS to be set
*/
public static VertexPositionColor[] curveToStrip(VertexPositionColor[] curve)
{
VertexPositionColor[] strip = new VertexPositionColor[curve.Length * 2];
VertexPositionColor[] new1 = new VertexPositionColor[curve.Length];
VertexPositionColor[] new2 = new VertexPositionColor[curve.Length];

for (int i = 0; i < curve.Length; i++)
{
if (i < curve.Length-1)
{
Vector2 p1 = new Vector2(curve.Position.X, curve.Position.Y);
Vector2 p2 = new Vector2(curve[i + 1].Position.X, curve[i + 1].Position.Y);
Vector2 perpPoint = perpendicularPoint(p1, p2);

new1 = new VertexPositionColor(new Vector3(distanceToPoint(p1, perpPoint, THICKNESS / 2), 0), UNLIT);
new2 = new VertexPositionColor(new Vector3(distanceToPoint(p1, perpPoint, -1 * THICKNESS / 2), 0), UNLIT);
}
else
{
Vector2 p1 = new Vector2(curve.Position.X, curve.Position.Y);
Vector2 p2 = new Vector2(curve[i - 1].Position.X, curve[i - 1].Position.Y);
Vector2 perpPoint = perpendicularPoint(p1, p2);

new1 = new VertexPositionColor(new Vector3(distanceToPoint(p1, perpPoint, -1 * THICKNESS / 2), 0), UNLIT);
new2 = new VertexPositionColor(new Vector3(distanceToPoint(p1, perpPoint, THICKNESS / 2), 0), UNLIT);
}
}


//strip[0] = new1[0];
//strip[1] = new2[0];

for (int i = 0; i < curve.Length; i++)
{
strip[i * 2] = new2;
strip[(i * 2) + 1] = new1;
}

return strip;
}

public static Vector2 perpendicularPoint(Vector2 p1, Vector2 p2)
{
return new Vector2(p2.Y - p1.Y, -1 * (p2.X - p1.X));
}

public static Vector2 distanceToPoint(Vector2 MoveFrom, Vector2 Direction, int toTravel)
{
//Make a point
Vector2 Reference = new Vector2(MoveFrom.X + Direction.X, MoveFrom.Y + Direction.Y);

//get distance
//sqrt( (X2 - X1)^2 + (y2 - y1)^2 ) = d
float d = (int)Math.Sqrt((Math.Pow((MoveFrom.X - Reference.X), 2) + Math.Pow((MoveFrom.Y - Reference.Y), 2)));

//Get X and Y differences
float difX = MoveFrom.X - Reference.X;
float difY = MoveFrom.Y - Reference.Y;

//Get Ratio
float ratio = -1 *((float)toTravel / d);

//Apply ratio to differences
difX = difX * ratio;
difY = difY * ratio;

//Return new Vector
return new Vector2(MoveFrom.X + difX, MoveFrom.Y + difY);
} }

This topic is closed to new replies.

Advertisement