# CURVE FUNCTIONS in Computer graphics

In computer graphics, curve functions are mathematical formulas used to represent and generate smooth curves. These curves are essential for creating various shapes and designs in computer-generated imagery (CGI), animation, and vector graphics.

There are different types of curve functions commonly used in computer graphics, including:

1. Bézier Curves: Bézier curves are widely used in computer graphics and are defined by control points. These curves are smooth and can be easily manipulated by adjusting the control points. Bézier curves can be of various orders, such as quadratic (degree 2), cubic (degree 3), and higher.
2. B-spline Curves: B-spline curves, short for basis spline curves, are defined by a set of control points and a knot vector. They provide more flexibility than Bézier curves and can generate curves with local control. B-spline curves are commonly used in computer-aided design (CAD) applications.
3. NURBS Curves: NURBS stands for non-uniform rational B-spline. NURBS curves are a generalization of B-spline curves and offer even greater flexibility. They allow for precise control over the shape and curvature of the curve. NURBS curves are commonly used in 3D modeling and computer-aided manufacturing (CAM) applications.
4. Catmull-Rom Splines: Catmull-Rom splines are a type of interpolating spline that passes through a set of control points. They are commonly used in computer graphics to create smooth curves that pass through specified points. Catmull-Rom splines are particularly useful for creating camera paths and smooth animations.

These curve functions are used in conjunction with algorithms and techniques to render and manipulate curves in computer graphics. They provide a way to represent and control complex shapes and animations with precision and smoothness.

## Bézier Curves

Bézier curves are a type of curve function commonly used in computer graphics. They are named after the French engineer Pierre Bézier, who developed these curves in the 1960s while working at Renault.

Bézier curves are defined by a set of control points that influence the shape of the curve. The curve itself is smooth and continuous, passing through the first and last control points, but not necessarily through the intermediate control points. The control points act as handles that allow the curve to be manipulated and adjusted.

The most commonly used Bézier curves are quadratic (degree 2) and cubic (degree 3) curves. Quadratic Bézier curves have three control points, while cubic Bézier curves have four control points. Higher degree Bézier curves can also be used, but they are less common.

To generate a Bézier curve, the position of points along the curve is determined by blending the control points using Bernstein polynomials. The blending is controlled by a parameter called t, which ranges from 0 to 1. By varying the value of t, different points along the curve can be calculated, allowing for smooth interpolation and animation.

Bézier curves are widely used in computer graphics for a variety of purposes, including creating smooth curves for shapes, defining paths for animations, and enabling precise control over curves in design applications. They are intuitive to work with and provide a flexible way to manipulate curves by adjusting the positions of the control points.

Here’s an example of Bézier curves implemented in C:

#include <stdio.h>

// Bézier Curve Calculation
float bezier(float t, float p0, float p1, float p2, float p3) {
float u = 1 - t;
float tt = t * t;
float uu = u * u;
float uuu = uu * u;
float ttt = tt * t;

float res = uuu * p0; // (1-t)^3 * p0
res += 3 * uu * t * p1; // 3 * (1-t)^2 * t * p1
res += 3 * u * tt * p2; // 3 * (1-t) * t^2 * p2
res += ttt * p3; // t^3 * p3

return res;
}

int main() {
float p0 = 0.0; // Start point
float p1 = 1.0; // Control point 1
float p2 = 2.0; // Control point 2
float p3 = 3.0; // End point

int numPoints = 10; // Number of points on the curve

for (int i = 0; i <= numPoints; i++) {
float t = (float)i / numPoints;
float point = bezier(t, p0, p1, p2, p3);
printf("Point at t=%f: %f\n", t, point);
}

return 0;
}



In this example, the bezier the function takes a parameter t (ranging from 0 to 1) and four control points (p0, p1, p2, p3) to calculate the position of the curve at the given t value. It uses the Bézier curve equation to perform the calculations.

In the main function, we define the control points (p0, p1, p2, p3) and specify the number of points (numPoints) We want on the curve. We then iterate over numPoints and calculate the position of the curve at each t value using the bezier function. Finally, we print the results to the console.

This is a basic implementation to demonstrate how Bézier curves can be calculated in C. You can modify the control points and a number of points to experiment with different curves. Remember to link any necessary libraries and compile the code appropriately.

B-spline Curves

B-spline curves, short for basis spline curves, are another type of curve function widely used in computer graphics. B-spline curves provide more flexibility and local control compared to Bézier curves.

A B-spline curve is defined by a set of control points and a knot vector. The control points determine the shape of the curve, while the knot vector controls the continuity and interpolation properties of the curve. The knot vector consists of a sequence of values that define the parameter space of the curve.

To calculate a point on a B-spline curve, we use the Cox-de-Boor recursion formula. This formula recursively calculates the blending functions, also known as basis functions, at a given parameter value t. These blending functions are used to combine the control points and determine the position of the curve at t.

B-spline curves can be of various orders, which depend on the number of control points and the degree of the curve. The degree of a B-spline curve is one less than the order. For example, a quadratic B-spline curve has a degree of 2 and a cubic B-spline curve has a degree of 3.

One of the advantages of B-spline curves is their local control property. Modifying the position of a control point affects only a localized section of the curve, unlike Bézier curves, where changing a control point affects the entire curve. This local control allows for more precise manipulation of the curve’s shape.

B-spline curves are commonly used in computer-aided design (CAD), computer-aided manufacturing (CAM), and computer graphics applications. They are particularly useful for creating smooth curves with localized control, such as designing complex shapes, creating animations, and generating smooth paths for camera movements.

Implementations of B-spline curves in various programming languages are available, providing the ability to create and manipulate these curves within computer graphics applications.

B-spline Curves in C

#include <stdio.h>
#include <math.h>

#define MAX_CONTROL_POINTS 10

// B-spline Curve Calculation
float bSpline(float t, int numControlPoints, float controlPoints[]) {
int degree = 3; // B-spline curve degree (cubic curve)
int numKnots = numControlPoints + degree + 1;
int numCurveSegments = numControlPoints - degree;

int span = floor(t * numCurveSegments);
if (span >= numCurveSegments) {
span = numCurveSegments - 1;
}

float u = t * numCurveSegments - span;
float basis[4];

// Initialize basis functions
for (int i = 0; i <= degree; i++) {
if (span + i < numKnots) {
basis[i] = controlPoints[span + i];
} else {
basis[i] = 0.0;
}
}

// Cox-de Boor recursion
for (int k = 1; k <= degree; k++) {
for (int i = degree; i >= k; i--) {
float alpha = (t - span + i) / (degree + 1 - k);
basis[i] = (1 - alpha) * basis[i - 1] + alpha * basis[i];
}
}

return basis[degree];
}

int main() {
int numControlPoints = 7; // Number of control points
float controlPoints[MAX_CONTROL_POINTS] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };

int numPoints = 10; // Number of points on the curve

for (int i = 0; i <= numPoints; i++) {
float t = (float)i / numPoints;
float point = bSpline(t, numControlPoints, controlPoints);
printf("Point at t=%f: %f\n", t, point);
}

return 0;
}



## NURBS Curves

NURBS, which stands for Non-Uniform Rational B-Spline, is a type of curve function commonly used in computer graphics and 3D modeling. NURBS curves are an extension of B-spline curves and offer even greater flexibility and control.

NURBS curves are defined by a set of control points, a knot vector, and a set of weights. The control points determine the shape of the curve, the knot vector controls the parameterization of the curve, and the weights determine the influence of each control point on the curve. The weights allow for control over the curve’s intensity at each control point, enabling precise control over the shape and curvature.

Unlike B-spline curves, NURBS curves are defined by non-uniform knot vectors, meaning the spacing between consecutive knots can vary. This property allows NURBS curves to have varying levels of detail in different regions, providing more control over the curve’s shape.

To calculate a point on a NURBS curve, we use a weighted sum of the control points, where each control point is multiplied by a blending function called the NURBS basis function. The NURBS basis function combines the influence of the control point’s weight, the knot vector, and the parameter value to calculate the final position of the curve.

NURBS curves are widely used in computer graphics and 3D modeling applications. They are particularly well-suited for creating smooth and precise curves, as well as complex shapes and surfaces. NURBS curves provide greater control over the shape and continuity of curves compared to other curve types, making them an essential tool in areas such as computer-aided design (CAD), animation, and virtual reality (VR) applications.

Implementations of NURBS curves are available in various computer graphics libraries and software packages, providing the ability to create, manipulate, and render NURBS curves within your projects.

NURBS Curves in C

Implementing NURBS curves in C involves calculating the weighted sum of control points using the NURBS basis functions. Here’s an example of NURBS curves implemented in C:

#include <stdio.h>
#include <math.h>

#define MAX_CONTROL_POINTS 10

// NURBS Curve Calculation
float nurbs(float t, int numControlPoints, float controlPoints[], float weights[], float knotVector[]) {
float x = 0.0;
float wSum = 0.0;

for (int i = 0; i < numControlPoints; i++) {
float basis = 0.0;

// Calculate NURBS basis function
for (int j = 0; j < numControlPoints; j++) {
float temp = 0.0;

if (knotVector[j] <= t && t < knotVector[j + 1]) {
temp = 1.0;
}

if (j == 0 && t == knotVector[numControlPoints]) {
temp = 1.0;
}

float alpha = (t - knotVector[j]) / (knotVector[j + 1] - knotVector[j]);
float blend = temp * pow(alpha, 3);
basis += blend;
}

// Weighted sum of control points
float weightedPoint = controlPoints[i] * weights[i] * basis;
x += weightedPoint;
wSum += weights[i] * basis;
}

return x / wSum;
}

int main() {
int numControlPoints = 7; // Number of control points
float controlPoints[MAX_CONTROL_POINTS] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
float weights[MAX_CONTROL_POINTS] = { 1.0, 2.0, 1.0, 1.0, 2.0, 1.0, 1.0 };
float knotVector[MAX_CONTROL_POINTS + 4] = { 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 4.0 };

int numPoints = 10; // Number of points on the curve

for (int i = 0; i <= numPoints; i++) {
float t = (float)i / numPoints;
float point = nurbs(t, numControlPoints, controlPoints, weights, knotVector);
printf("Point at t=%f: %f\n", t, point);
}

return 0;
}



## Catmull-Rom Splines

Catmull-Rom splines are a type of spline interpolation commonly used in computer graphics and animation. They are named after the computer scientist Edwin Catmull and the engineer Raphael Rom, who introduced this interpolation method in the 1970s.

Catmull-Rom splines are defined by a set of control points that the curve passes through. Unlike Bézier curves or B-spline curves, Catmull-Rom splines do not require explicit control handles or knot vectors. Instead, they interpolate smoothly between consecutive control points, creating a smooth and continuous curve.

The formula to calculate a point on a Catmull-Rom spline depends on four control points: P0, P1, P2, and P3. To calculate the position at a given parameter t, the following formula is used:

P(t) = 0.5 * ((2 * P1) + (-P0 + P2) * t + (2 * P0 – 5 * P1 + 4 * P2 – P3) * t^2 + (-P0 + 3 * P1 – 3 * P2 + P3) * t^3)

Here, t ranges from 0 to 1, indicating the position on the curve. The parameter t determines the position between control points P1 and P2. By varying t, different points along the Catmull-Rom spline can be calculated.

Catmull-Rom splines have several advantages, including local control, which means modifying a control point only affects a localized section of the curve. They also maintain shape preservation, meaning that the curve passes through the control points, ensuring accurate interpolation.

Catmull-Rom splines are widely used in computer graphics and animation for creating smooth paths, camera movements, and character animations. They offer a convenient way to create and manipulate smooth curves with predictable and intuitive behavior.

Catmull-Rom Splines in C

#include <stdio.h>

typedef struct {
float x;
float y;
} Point;

// Catmull-Rom Spline Calculation
Point catmullRomSpline(float t, Point p0, Point p1, Point p2, Point p3) {
Point point;
float t2 = t * t;
float t3 = t2 * t;

point.x = 0.5 * ((2 * p1.x) +
(-p0.x + p2.x) * t +
(2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 +
(-p0.x + 3 * p1.x - 3 * p2.x + p3.x) * t3);

point.y = 0.5 * ((2 * p1.y) +
(-p0.y + p2.y) * t +
(2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 +
(-p0.y + 3 * p1.y - 3 * p2.y + p3.y) * t3);

return point;
}

int main() {
Point p0 = { 1.0, 1.0 };
Point p1 = { 2.0, 3.0 };
Point p2 = { 4.0, 2.0 };
Point p3 = { 5.0, 4.0 };

int numPoints = 10; // Number of points on the curve

for (int i = 0; i <= numPoints; i++) {
float t = (float)i / numPoints;
Point point = catmullRomSpline(t, p0, p1, p2, p3);
printf("Point at t=%f: (%f, %f)\n", t, point.x, point.y);
}

return 0;
}