1

I've never been a trig champion and after about 4 hours of searching, I decided to ask here:

I use Javascript to draw a quadratic curve (not a cubic bezier curve) onto a HTML5 Canvas like so:

this.shape.moveTo(50,80).curveTo(100,120,40,190);

where moveTo specifies x and y of the first point, the first two parameters of curveTo specify the x and y of the control point, and the 3rd and 4th parameter of curveTo specify the x and y of the end point.

I need to craft a function that allows me to get the slope of an arbitrary point t on that curve, something like:

function getTangentSlope(P0,P1,P2,t) {
   blah blah blah
   return slope;
}

So far, the only solutions that I have found were either for cubic curves with two control points (Find the tangent of a point on a cubic bezier curve (on an iPhone)), and or I didn't understand the notation (https://www.math.usm.edu/lee/mathphysarchive/?p=542), or broken links meant that I was not able to review the whole solution (Quadratic Bezier Curve: Calculate Tangent).

Best still would be if the slope was given in degrees.

Can a brother help me out?

Marcel
  • 98
  • 10

2 Answers2

3

This will return the normalised tangent at the unit position for a 2nd and 3rd order curve.

See this answer for more detailed usage of the object below.

const geom = (()=>{

    function Vec(x,y){ 
        this.x = x;
        this.y = y;
    };
    function Bezier(p1,p2,cp1,cp2){  
        this.p1 =  p1;
        this.p2 =  p2;
        this.cp1 = cp1;
        this.cp2 = cp2;
    }    
    Bezier.prototype = {
        //======================================================================================
        // single dimension polynomials for 2nd (a,b,c) and 3rd (a,b,c,d) order bezier 
        //======================================================================================
        // for quadratic   f(t) = a(1-t)^2+2b(1-t)t+ct^2 
        //                      = a+2(-a+b)t+(a-2b+c)t^2
        // The derivative f'(t) =  2(1-t)(b-a)+2(c-b)t
        //======================================================================================
        // for cubic           f(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3 
        //                          = a+(-2a+3b)t+(2a-6b+3c)t^2+(-a+3b-3c+d)t^3
        // The derivative     f'(t) = -3a(1-t)^2+b(3(1-t)^2-6(1-t)t)+c(6(1-t)t-3t^2) +3dt^2
        // The 2nd derivative f"(t) = 6(1-t)(c-2b+a)+6t(d-2c+b)
        //======================================================================================        
        p1 : undefined,
        p2 : undefined,
        cp1 : undefined,
        cp2 : undefined,
        tangentAsVec (position, vec ) { 
            var a, b, c, u;
            if (vec === undefined) { vec = new Vec(); }

            if (this.cp2 === undefined) {
                a = (1-position) * 2;
                b = position * 2;
                vec.x = a * (this.cp1.x - this.p1.x) + b * (this.p2.x - this.cp1.x);
                vec.y = a * (this.cp1.y - this.p1.y) + b * (this.p2.y - this.cp1.y);
            }else{
                a  = (1-position)
                b  = 6 * a * position;        // (6*(1-t)*t)
                a *= 3 * a;                   // 3 * ( 1 - t) ^ 2
                c  = 3 * position * position; // 3 * t ^ 2
                vec.x  = -this.p1.x * a + this.cp1.x * (a - b) + this.cp2.x * (b - c) + this.p2.x * c;
                vec.y  = -this.p1.y * a + this.cp1.y * (a - b) + this.cp2.y * (b - c) + this.p2.y * c;
            }   
            u = Math.sqrt(vec.x * vec.x + vec.y * vec.y);
            vec.x /= u;
            vec.y /= u;
            return vec;                 
        },      
    }
    return { Vec, Bezier,}
})()

Usage for 2nd order bezier

   // ? represents any value 
   var p1 = new geom.Vec(?,?);  // start point
   var p2 = new geom.Vec(?,?);  // end point
   var cp1 = new geom.Vec(?,?); //control point

   var bez2 = new geom.Bezier(p1,p2,cp1); // create 2nd order bezier
   var t = ?;
   var tangent = bez2.tangentAsVec(t);

Usage for 3rd order bezier

   // ? represents any value 
   var p1 = new geom.Vec(?,?);  // start point
   var p2 = new geom.Vec(?,?);  // end point
   var cp1 = new geom.Vec(?,?); // 1st control point
   var cp2 = new geom.Vec(?,?); // 2nd control point

   var bez3 = new geom.Bezier(p1,p2,cp1,cp2); // create 3rd order bezier
   var t = ?;
   var tangent = bez2.tangentAsVec(t);
Blindman67
  • 51,134
  • 11
  • 73
  • 136
0

Hmmm I have no idea how this function might look, but I know where you can find useful examples :)

Some time ago I used a library to draw charts. In this library I found many functions to draw lines in line chart. Library is called D3. After digging the code I found an interesting files:

https://github.com/d3/d3-shape/tree/master/src/curve

Here you can see what effects you can get:

https://github.com/d3/d3-shape#curves

Good luck :)

Krzysztof Raciniewski
  • 4,735
  • 3
  • 21
  • 42