Sign in to follow this  

Adding Thickness to Bezier Curve?

This topic is 2486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok so after a LOT of searching, I found a formula to calculate points along a bezier curve. The way it's set up is, given SUBDIVISIONS, it'll calculate SUBDIVISIONS + 2 points along the curve (plus 2 being the start and end points.)

Now I'm wanting to turn that curve into a triangle strip so I can control the thickens of the line essentially.

How... would I go about, essentially, making a parallel curve that would be THICKNESS distance from the orignal points? I already have the code to turn the two curves into a triangle strip.

Related functions currently using:

[code]

/*
* 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[i]= 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;
}

/*
* Given a bezier curve, two control points, and a point to scale by, this function will convert a curve into a triangle strip
* Requires THICKNESS to be set
*/
public static VertexPositionColor[] curveToStrip(VertexPositionColor[] curve, Vector2 c1, Vector2 c2, Vector2 scalePoint)
{
VertexPositionColor[] strip = new VertexPositionColor[curve.Length * 2];

Vector2 p1 = new Vector2(curve[0].Position.X, curve[0].Position.Y);
Vector2 p2 = new Vector2(curve[SUBDIVISIONS + 1].Position.X, curve[SUBDIVISIONS + 1].Position.Y);

p1 = distanceToPoint(p1, scalePoint, THICKNESS);
p2 = distanceToPoint(p2, scalePoint, THICKNESS);


VertexPositionColor[] curve2 = bezierCurve(p1, p2, c1, c2);

strip[0] = curve[0];
strip[1] = curve2[0];

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

return strip;
}

/*
* Given a point, a point of reference, and a distance to travel, this function will calculate a point on a line a certain distance from the MoveFrom point
*/

public static Vector2 distanceToPoint(Vector2 MoveFrom, Vector2 Reference, int toTravel)
{
//get distance
//sqrt( (X2 - X1)^2 + (y2 - y1)^2 ) = d
int d = (int)Math.Sqrt((Math.Pow((MoveFrom.X - Reference.X), 2) + Math.Pow((MoveFrom.Y - Reference.Y), 2)));

//Get X and Y differences
int difX = (int)MoveFrom.X - (int)Reference.X;
int difY = (int)MoveFrom.Y - (int)Reference.Y;

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

//Apply ratio to differences
difX = (int)((float)difX * ratio);
difY = (int)((float)difY * ratio);

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

The bezier curve function should be fine but the other two need some work. They don't display properly. I'm most likely going about it completely wrong. ^^

EDIT: Also this is in XNA/C#

Share this post


Link to post
Share on other sites
"They don't display properly"
Post an image.

I'd go about it this way
[attachment=1452:bezier_thick.JPG]
just make the parallel red segments (with the same length, it should be easy). You have to add the two extra triangles at the corners: blue and yellow.
bottom image: the segments are pulled apart to show the topology

Share this post


Link to post
Share on other sites

This topic is 2486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this