Hi,
I need some help in finding the control points in cubic spline interpolation. I am following the article Draw a Smooth Curve through a Set of 2D Points with Bezier Primitives - CodeProject .But I ended up with the weird output for the X and Y coordinates shown below
[X=0 Y=2.7
X=1 Y=1
X=2 Y=1.9
X=11 Y=3.8
X=12 Y=3.6
X=13 Y=2.7]
Can anyone help me how to find the control points?
Regards,
MayaSmitha
7 replies to this topic
#1
Posted 11 October 2011 - 10:56 PM
|
|
|
#2
Posted 12 October 2011 - 05:26 AM
What's your code?
#3
Posted 12 October 2011 - 10:13 PM
Code is as follows,
I refered this article Draw a Smooth Curve through a Set of 2D Points with Bezier Primitives - CodeProject
Regards,
Mayasmitha
I refered this article Draw a Smooth Curve through a Set of 2D Points with Bezier Primitives - CodeProject
public static class BezierSpline
{
/// <summary>
/// Get open-ended Bezier Spline Control Points.
/// </summary>
/// <param name="knots">Input Knot Bezier spline points.</param>
/// <param name="firstControlPoints">Output First Control points
/// array of knots.Length - 1 length.</param>
/// <param name="secondControlPoints">Output Second Control points
/// array of knots.Length - 1 length.</param>
/// <exception cref="ArgumentNullException"><paramref name="knots"/>
/// parameter must be not null.</exception>
/// <exception cref="ArgumentException"><paramref name="knots"/>
/// array must contain at least two points.</exception>
public static void GetCurveControlPoints(Point[] knots,
out Point[] firstControlPoints, out Point[] secondControlPoints)
{
if (knots == null)
throw new ArgumentNullException("knots");
int n = knots.Length - 1;
if (n < 1)
throw new ArgumentException
("At least two knot points required", "knots");
if (n == 1)
{ // Special case: Bezier curve should be a straight line.
firstControlPoints = new Point[1];
// 3P1 = 2P0 + P3
firstControlPoints[0].X = (2 * knots[0].X + knots[1].X) / 3;
firstControlPoints[0].Y = (2 * knots[0].Y + knots[1].Y) / 3;
secondControlPoints = new Point[1];
// P2 = 2P1 – P0
secondControlPoints[0].X = 2 *
firstControlPoints[0].X - knots[0].X;
secondControlPoints[0].Y = 2 *
firstControlPoints[0].Y - knots[0].Y;
return;
}
// Calculate first Bezier control points
// Right hand side vector
double[] rhs = new double[n];
// Set right hand side X values
for (int i = 1; i < n - 1; ++i)
rhs[i] = 4 * knots[i].X + 2 * knots[i + 1].X;
rhs[0] = knots[0].X + 2 * knots[1].X;
rhs[n - 1] = (8 * knots[n - 1].X + knots[n].X) / 2.0;
// Get first control points X-values
double[] x = GetFirstControlPoints(rhs);
// Set right hand side Y values
for (int i = 1; i < n - 1; ++i)
rhs[i] = 4 * knots[i].Y + 2 * knots[i + 1].Y;
rhs[0] = knots[0].Y + 2 * knots[1].Y;
rhs[n - 1] = (8 * knots[n - 1].Y + knots[n].Y) / 2.0;
// Get first control points Y-values
double[] y = GetFirstControlPoints(rhs);
// Fill output arrays.
firstControlPoints = new Point[n];
secondControlPoints = new Point[n];
for (int i = 0; i < n; ++i)
{
// First control point
firstControlPoints[i] = new Point(x[i], y[i]);
// Second control point
if (i < n - 1)
secondControlPoints[i] = new Point(2 * knots
[i + 1].X - x[i + 1], 2 *
knots[i + 1].Y - y[i + 1]);
else
secondControlPoints[i] = new Point((knots
[n].X + x[n - 1]) / 2,
(knots[n].Y + y[n - 1]) / 2);
}
}
/// <summary>
/// Solves a tridiagonal system for one of coordinates (x or y)
/// of first Bezier control points.
/// </summary>
/// <param name="rhs">Right hand side vector.</param>
/// <returns>Solution vector.</returns>
private static double[] GetFirstControlPoints(double[] rhs)
{
int n = rhs.Length;
double[] x = new double[n]; // Solution vector.
double[] tmp = new double[n]; // Temp workspace.
double b = 2.0;
x[0] = rhs[0] / b;
for (int i = 1; i < n; i++) // Decomposition and forward substitution.
{
tmp[i] = 1 / b;
b = (i < n - 1 ? 4.0 : 3.5) - tmp[i];
x[i] = (rhs[i] - x[i - 1]) / b;
}
for (int i = 1; i < n; i++)
x[n - i - 1] -= tmp[n - i] * x[n - i]; // Backsubstitution.
return x;
}
}
Regards,
Mayasmitha
Edited by mayasmitha, 12 October 2011 - 11:17 PM.
#4
Posted 13 October 2011 - 04:52 AM
The above doesn't have a main method. It's not clear what data you're calling your methods with. Everything on your output is going to depend on your input.
#5
Posted 13 October 2011 - 05:01 AM
Hi,
Thank you very much for your reply :) I actually in need of generating control points which constitutes the spline curve. I followed the article mentioned above. Is there any alternative way how we can find the control points to get smooth 2D spine curve? I have a collection of points with x,y co ordinates.
Regards,
Mayasmitha
Thank you very much for your reply :) I actually in need of generating control points which constitutes the spline curve. I followed the article mentioned above. Is there any alternative way how we can find the control points to get smooth 2D spine curve? I have a collection of points with x,y co ordinates.
Regards,
Mayasmitha
#6
Posted 13 October 2011 - 09:42 AM
Perhaps I'm just confused. Is your goal to use the code you cited, or write your own code? Is the goal to to use the standard method of Cubic splines, or something else?
I glanced at the BezierSplineTest class and it seemed like it made using the code pretty simple. I'm not sure what about the output you got (which you haven't posted) seemed weird, but that could be a correct answer seeming non-intuitive.
I glanced at the BezierSplineTest class and it seemed like it made using the code pretty simple. I'm not sure what about the output you got (which you haven't posted) seemed weird, but that could be a correct answer seeming non-intuitive.
#7
Posted 13 October 2011 - 08:13 PM
Hi,
Thanks again :) the above code works fine for almost all cases. But it results in curl between two knots (going backwards) when there is big difference exist in the point collection.For example:
[X=0 Y=2.7
X=1 Y=1
X=2 Y=1.9
X=11 Y=3.8
X=12 Y=3.6
X=13 Y=2.7]
In the above point collection the points there is much difference between the 3rd and 4th point i.e(x=2 and x=11) which results in the curl. Can anyone help me how can we avoid this behavior using the algorithm i provided in the last post?
Thanks and regards,
Mayasmitha
Thanks again :) the above code works fine for almost all cases. But it results in curl between two knots (going backwards) when there is big difference exist in the point collection.For example:
[X=0 Y=2.7
X=1 Y=1
X=2 Y=1.9
X=11 Y=3.8
X=12 Y=3.6
X=13 Y=2.7]
In the above point collection the points there is much difference between the 3rd and 4th point i.e(x=2 and x=11) which results in the curl. Can anyone help me how can we avoid this behavior using the algorithm i provided in the last post?
Thanks and regards,
Mayasmitha
#8
Posted 14 October 2011 - 09:58 AM
At that point, you are no longer using the cubic spline algorithm. It wasn't really designed for handling large gaps.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









