examples/RenderStack/example.CurveTool/CubicBezierCurve.cs
/// <summary>
/// Compute control points q1 and q2 for cubic bezier curve
/// which goes through give points p0, p1, p2 and p3.
/// </summary>
/// <param name="p0">Curve start point</param>
/// <param name="p1">Point on curve</param>
/// <param name="p2">Point on curve</param>
/// <param name="p3">Curve end point</param>
/// <param name="q1">Computed control point</param>
/// <param name="q2">Computed control point</param>
/// <param name="u">t for first control point</param>
/// <param name="v">t for second control point</param>
Compute control points q1 and q2 for cubic bezier curve
which goes through give points p0, p1, p2 and p3.
Compute control points q1 and q2 for cubic bezier curve which goes through give points p0 p1 p2 and p3 .
public static void ApportionedChords(
            Vector3 p0,
            Vector3 p1,
            Vector3 p2,
            Vector3 p3,
            out Vector3 q1,
            out Vector3 q2,
            out float u,
            out float v
        )
        {
            Vector3 D1 = p1 - p0;
            Vector3 D2 = p2 - p1;
            Vector3 D3 = p3 - p2;
            float d1 = D1.Length;
            float d2 = D2.Length;
            float d3 = D3.Length;
            u = d1 / (d1 + d2 + d3);
            v = (d1 + d2) / (d1 + d2 + d3);
            float a   = 3.0f * (1.0f - u) * (1.0f - u) * u; 
            float b   = 3.0f * (1.0f - u) * u * u;
            float c   = 3.0f * (1.0f - v) * (1.0f - v) * v; 
            float d   = 3.0f * (1.0f - v) * v * v;
            float det = a * d - b * c;
            if(det == 0.0f)
            {
                throw new InvalidOperationException();
            }
            Vector3 t1;
            Vector3 t2;
            t1.X = p1.X - ((1.0f - u) * (1.0f - u) * (1.0f - u) * p0.X + u * u * u * p3.X);
            t1.Y = p1.Y - ((1.0f - u) * (1.0f - u) * (1.0f - u) * p0.Y + u * u * u * p3.Y);
            t1.Z = p1.Z - ((1.0f - u) * (1.0f - u) * (1.0f - u) * p0.Z + u * u * u * p3.Z);
            t2.X = p2.X - ((1.0f - v) * (1.0f - v) * (1.0f - v) * p0.X + v * v * v * p3.X);
            t2.Y = p2.Y - ((1.0f - v) * (1.0f - v) * (1.0f - v) * p0.Y + v * v * v * p3.Y);
            t2.Z = p2.Z - ((1.0f - v) * (1.0f - v) * (1.0f - v) * p0.Z + v * v * v * p3.Z);
            q1.X = d * t1.X - b * t2.X;
            q1.Y = d * t1.Y - b * t2.Y;
            q1.Z = d * t1.Z - b * t2.Z;
            q1.X /= det;
            q1.Y /= det;
            q1.Z /= det;
            q2.X = (-c) * t1.X + a * t2.X;
            q2.Y = (-c) * t1.Y + a * t2.Y;
            q2.Z = (-c) * t1.Z + a * t2.Z;
            q2.X /= det;
            q2.Y /= det;
            q2.Z /= det;
        }
public static void ApportionedChords ( Vector3 p0 , Vector3 p1 , Vector3 p2 , Vector3 p3 , out Vector3 q1 , out Vector3 q2 , out float u , out float v ) { Vector3 D1 = p1 - p0 ; Vector3 D2 = p2 - p1 ; Vector3 D3 = p3 - p2 ; float d1 = D1 . Length ; float d2 = D2 . Length ; float d3 = D3 . Length ; u = d1 / ( d1 + d2 + d3 ) ; v = ( d1 + d2 ) / ( d1 + d2 + d3 ) ; float a = 3.0f * ( 1.0f - u ) * ( 1.0f - u ) * u ; float b = 3.0f * ( 1.0f - u ) * u * u ; float c = 3.0f * ( 1.0f - v ) * ( 1.0f - v ) * v ; float d = 3.0f * ( 1.0f - v ) * v * v ; float det = a * d - b * c ; if ( det == 0.0f ) { throw new InvalidOperationException ( ) ; } Vector3 t1 ; Vector3 t2 ; t1 . X = p1 . X - ( ( 1.0f - u ) * ( 1.0f - u ) * ( 1.0f - u ) * p0 . X + u * u * u * p3 . X ) ; t1 . Y = p1 . Y - ( ( 1.0f - u ) * ( 1.0f - u ) * ( 1.0f - u ) * p0 . Y + u * u * u * p3 . Y ) ; t1 . Z = p1 . Z - ( ( 1.0f - u ) * ( 1.0f - u ) * ( 1.0f - u ) * p0 . Z + u * u * u * p3 . Z ) ; t2 . X = p2 . X - ( ( 1.0f - v ) * ( 1.0f - v ) * ( 1.0f - v ) * p0 . X + v * v * v * p3 . X ) ; t2 . Y = p2 . Y - ( ( 1.0f - v ) * ( 1.0f - v ) * ( 1.0f - v ) * p0 . Y + v * v * v * p3 . Y ) ; t2 . Z = p2 . Z - ( ( 1.0f - v ) * ( 1.0f - v ) * ( 1.0f - v ) * p0 . Z + v * v * v * p3 . Z ) ; q1 . X = d * t1 . X - b * t2 . X ; q1 . Y = d * t1 . Y - b * t2 . Y ; q1 . Z = d * t1 . Z - b * t2 . Z ; q1 . X /= det ; q1 . Y /= det ; q1 . Z /= det ; q2 . X = ( - c ) * t1 . X + a * t2 . X ; q2 . Y = ( - c ) * t1 . Y + a * t2 . Y ; q2 . Z = ( - c ) * t1 . Z + a * t2 . Z ; q2 . X /= det ; q2 . Y /= det ; q2 . Z /= det ; }
Compute control points q1 and q2 for cubic bezier curve
which goes through give points p0, p1, p2 and p3.
Compute control points q1 and q2 for cubic bezier curve which goes through give points p0 p1 p2 and p3 .
p0
Vector3
Curve start point
Curve start point
p1
Vector3
Point on curve
Point on curve
p2
Vector3
Point on curve
Point on curve
p3
Vector3
Curve end point
Curve end point
Vector3
out
float
out
q1
Computed control point
Computed control point
q2
Computed control point
Computed control point
u
t for first control point
t for first control point
v
t for second control point
t for second control point