//****************************************************************************//
//******************** Bezier Curves - March 25th, 2019 *********************//
//**************************************************************************//

- Alright, today we're going to be shifting gears into a different world of graphics: curves!
    - The book chapter on this section is quite good, so make sure to take a peek at it if you're interested

- First off, let's start with a definition: a CONVEX HULL for a set of points is the smallest convex shape that contains all those points
    - This is a simple concept, but it's important!
    - A CONVEX COMBINATION is a point that can be expressed as a "convex" combination of other points in a set
        - For a line segment p1/p2, for instance, a point "a" is a convex combination of points p1/p2 if it can be written as:

                a = w1*p1 + w2*p2

            - Where w1 + w2 = 1 and the weights are non-negative, meaning a is on the line segment
        - For a triangle defined by points p1/p2/p3, we say a point "b" is a convex combination of those points if it can be expressed as:

                b = w1*p1 + w2*p2 + w3*p3

            - Where w1 + w2 + w3 = 1 and all the weights are non-negative, meaning b is inside the triangle
        - "Think of a convex combination like this: each of the points has a rope attached to our point, and they're all pulling their ropes tight. If we can get our point by pulling on the ropes like this, then it's a convex combo!"
    - Generalizing this combination definition, we can say that for a set of "n" points p1...pn, and a given point "c" written as:

            c = w1*p1 + ... + wn*pn

        - ...we can say that c is INSIDE the convex hull if all the weights are non-negative and sum up to 1!
            - Hold on to your eyeballs, because that's an important fact!

- Building a bit weirdly off of this, let's talk about some mysterious thing called "barycentric coordinates"
    - For a triangle ONLY, the convex weights w1/w2/w3 for a given point "g" are called the BARYCENTRIC COORDINATES of that point
        - If we know where our point is, then we can figure out these weights based on the area of the different triangle and it's interior sub-sections:

          p1 --------------- p2
            \      a3      /
             \    \   /   /
              \ a2  *  a1/
               \    |   /
                \   |  /
                 \  | /
                  \ |/
                   \/
                   p3

            - Where the total area A = a1 + a2 + a3, and the individual weights'll be:
                - W1 = a1/A
                - W2 = a2/A
                - W3 = a3/A

    - These coordinates have a lot of nice properties we won't get into right now, but trust me, they're there!

- Now, let's start using these points to talk about a really common, useful type of curve, called BEZIER CURVES
    - These are a whole family of curves named after the mathematician Pierre Bezier, but we'll mostly be talking about CUBIC, or "degree 3," Bezier curves
    - For this curve, we'll have a start point, an end point, and 2 intermediate "control points" that define how the curve is shaped as it goes from the start to the endpoint
        - What're a few properties of this curve?
            - Bezier curves INTERPOLATE through p1/p4 (the start/end point)
            - The curve is tangent at its endpoints to the line segments P1/P2 and P3/P4
            - ALL points on the curve are inside the convex hull of p1, p2, p3, and p4
            - Cubic bezier curves give independent control of the endpoints and tangents (i.e. we can change where the endpoint is without affecting the tangent, and vice-versa)
                - We can't do that with a line segment, or even with quadratic Bezier curves - it's the lowest-degree curve that gives us this control! This is a big reason cubic Bezier curves specifically are popular among CG people
        - If we want to, we can then chain multiple of these curves together into multiple, longer curves
    - "Now, equations sometimes strike fear into people's hearts, but we've got to look at them - so here we go"
        - Recall that the parametric line segment equation was where we had 2 points P1/P2, and a point on that line q(t) was defined:

                q(t) = (x(t), y(t)) =>
                     = (x1*(1-t) + x2*t, 
                        y1*(1-t) + y2*t)
                     = P1*L1(t) + P2*L2(t)

            - Where L1(t) = 1-t, and L2(t) = t (our "basis functions")
                - Notice that L1(t) + L2(t) will add up to 1 for ANY value of t - and remember how convex combination weights should add up to 1 as well? We can consider L1 and L2 to be the weights of this equations!
                    - We can use this general idea of control points, multiplied by weights, to define our Bezier curve itself
                    - ...and remember: technically, this IS a degree-1 Bezier curve!
        - For cubic Bezier curves, then, what can we do?
            - Well, we've got four total control points (including the endpoints), and we will have four basis functions/"weights" B1 to B4
            - Therefore, a point on our curve q(t) will look like:

                    q(t) = B1(t)*P1 + B2(t)*P2 + B3(t)*P3 + B4(t)*P4

                - What ARE these basis functions, though? We need them to add up to 1 and be non-negative for values of t between 0 and 1, so in general:
                    - B1 will start at 1, then sharply drops off to 0 
                    - B2/B3 will be "humps" in the middle values of t, indicating the curve will be tugged towards them
                    - B4 will be a flipped version of B1, starting off at 0 and curving exponentially to 1

- So, the big ideas for today are the idea of convex combinations and their non-negative weights, and the idea of using basis functions that meet this requirement to define curves. We'll go over what EXACTLY these equations look like for a Bezier curve...next time!