Search code examples
mathgeometry2dbeziercurves

Calculate the edge points of a surface defined by 2 bezier curves?


Given the 6 points defining 2 quadratic bezier curves, how do I calculate the points at which the tangents for both curves are the same?

The distance between tangents gets smaller and eventually reaches 0, when they coincide. How do I calculate this without looping a hundred times and checking each part? Is there a mathematical solution to this?

enter image description here


Solution

  • Conveniently wikipedia even shows gradient for a quadratic Bezier curve defined by P_0, P_1, P_2 so we don't even need to calculate it.

    gradient

    So, if we have 2 curves defined by B_1(t) and B_2(t), and we want to know the location in curve B_2(t) that has the same tangent as, for example, B_1(0.5). We need only to find t such that B_1'(0.5) = B_2'(t). Since the gradient is a linear equation it should be trivial to solve this equality.

    edit: this is such a cool question I had to write some code :)

    Consider this figure: enter image description here

    B = @(t,p0,p1,p2)((1-t)*((1-t)*p0 + t*p1) + t*((1-t)*p1 + t*p2)) 
    C = @(t,p0,p1,p2)(2*(1-t)*(p1-p0) + 2*t*(p2-p1));
    
    p0 = [1;2];
    p1 = [3;5];
    p2 = [6;-1];
    X = [];
    for t=0:0.05:1
        X = [X B(t,p0,p1,p2)];
    end
    
    q0 = [2;0];
    q1 = [4;7];
    q2 = [5;0];
    Y = [];
    for t=0:0.05:1
        Y = [Y B(t,q0,q1,q2)];
    end
    
    figure(1)
    clf;
    hold on;
    plot([p0(1) p1(1) p2(1)], [p0(2) p1(2) p2(2)], 'ro:')
    plot(X(1,:), X(2,:), 'g-');
    
    plot([q0(1) q1(1) q2(1)], [q0(2) q1(2) q2(2)], 'bo:')
    plot(Y(1,:), Y(2,:), 'm-');
    
    % Consider: t = 0.2
    % Note: B(0.2,p0,p1,p2) = [1.84; 2.84]
    %       C(0.2,p0,p1,p2) = [4.4; 2.4]
    %
    % 2*(1-t)*([4;7] - [2;0]) + 2*t*([5;0]-[4;7]) = [4-2*t; 14-28*t]
    % (4-2*t)/(14-28*t) = 4.4/2.4
    % 9.6 - 4.8t = 61.6 - 123.2t
    % 118.4t - 52 = 0
    % t = 0.439
    %
    % B(0.439,q0,q1,q2) = [3.56; 3.45]
    
    plot([1.84, 3.56], [2.84, 3.45], 'ks--')
    

    This is matlab code, I hope it's somewhat clear, if you don't use matlab just note that column vectors are denoted as [a;b;c;d;...]. So I define 2 curves with points p0,p1,p2 and q0,q1,q2, then create a function to compute the Bezier (B) and the gradient (C). I then plot the curves and consider the tangent at t=0.2, I hope the remaining math is clear, but just ask a question if not.

    Also, note that we do not solve for the gradients to be equal as I originally said! Instead we need to solve for the gradient that has the same ratio of x/y. And if there is no solution then there is no place where the tangents are equal.