### Install kirb Bezier Curve Library Source: https://github.com/jiwonme/kirb/blob/main/README.md Commands to install the kirb library using npm, pnpm, or yarn. ```bash npm install kirb ``` ```bash pnpm add kirb ``` ```bash yarn add kirb ``` -------------------------------- ### JavaScript Quick Start for Bezier Curves Source: https://github.com/jiwonme/kirb/blob/main/README.md Demonstrates basic usage of the Bezier class in JavaScript, including creating curves, getting points, calculating length, splitting curves, and finding normals and bounding boxes. ```javascript import { Bezier } from 'kirb'; // Create a cubic Bezier curve const curve = new Bezier(0, 0, 100, 25, 200, 75, 300, 100); // Get a point at t=0.5 const point = curve.get(0.5); // Get curve length const length = curve.length(); // Split curve at t=0.5 const { left, right } = curve.split(0.5); // Get curve normal at t=0.5 const normal = curve.normal(0.5); // Find extrema const extrema = curve.extrema(); // Get bounding box const bbox = curve.bbox(); ``` -------------------------------- ### JavaScript kirb Error Handling Example Source: https://github.com/jiwonme/kirb/blob/main/README.md Demonstrates explicit error handling in JavaScript using kirb's `KirbError` and `ErrorCodes`. This example shows how to catch errors, check the error type, and access the error code and details. ```javascript import { Bezier, KirbError, ErrorCodes } from 'kirb'; try { curve.offsetPoint(2, 10); // Out of range! } catch (e) { if (e instanceof KirbError) { console.log(e.code); // 'OUT_OF_RANGE' console.log(e.details); // { t: 2, validRange: [0, 1] } } } ``` -------------------------------- ### JavaScript Examples of kirb's New API Features Source: https://github.com/jiwonme/kirb/blob/main/README.md Showcases new and improved functionalities in kirb's JavaScript API, including a clearer offset API, enhanced search methods like `findParameter`, utility functions such as `sample` and `closestPoint`, and the `contains` method for point-on-curve checks. ```javascript // Clearer offset API const offsetPt = curve.offsetPoint(0.5, 10); console.log(offsetPt.point); // { x, y } console.log(offsetPt.normal); // Normal vector console.log(offsetPt.t); // 0.5 const offsetCurves = curve.offsetCurve(10); // Array of curves // Better search const result = curve.findParameter(point, { tolerance: 5 }); if (result) { console.log(result.t); // Parameter value console.log(result.hits); // Matching points } // Utility methods const points = curve.sample(20); // 20 evenly spaced points const closest = curve.closestPoint(point); // Closest point on curve const info = curve.getInfo(); // Curve metadata // Check if point is on curve if (curve.contains(point, { tolerance: 1 })) { console.log('Point is on curve!'); } ``` -------------------------------- ### Get Point on Bezier Curve Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates a point on the Bezier curve for a given `t` value (0 to 1). `.get(t)` is an alias for `.compute(t)`. This example highlights the point at `t=0.5` on the curve. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); this.drawPoint(curve.get(0.5)); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); this.drawPoint(curve.get(0.5)); } ``` -------------------------------- ### Draw Bezier Curve and Offset Lines Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html This snippet demonstrates drawing a Bezier curve and its offsets, including drawing lines and text labels for the arc length. It utilizes methods like `drawSkeleton`, `drawCurve`, `setColor`, `length`, `offset`, `forEach`, `get`, `drawLine`, and `drawText`. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var arclength = curve.length(); var offset = curve.offset(-10), last=offset.length-1; offset.forEach((c,idx) => { this.drawCurve(c); if(idx===last) { var p1 = curve.offset(0.95, -15); var p2 = c.get(1); var p3 = curve.offset(0.95, -5); this.drawLine(p1,p2); this.drawLine(p2,p3); var label = ((100*arclength)|0)/100 + "px"; this.drawText(label, {x:p2.x+7,y:p2.y-3}); } }); } ``` ```javascript new Bezier(100, 25, 10, 90, 110, 100, 132, 192 ); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var arclength = curve.length(); var offset = curve.offset(-10), last=offset.length-1; offset.forEach((c,idx) => { this.drawCurve(c); if(idx===last) { var p1 = curve.offset(0.95, -15); var p2 = c.get(1); var p3 = curve.offset(0.95, -5); this.drawLine(p1,p2); this.drawLine(p2,p3); var label = ((100*arclength)|0)/100 + "px"; this.drawText(label, {x:p2.x+7,y:p2.y-3}); } }); } ``` -------------------------------- ### Find Extrema of Bezier Curve Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Finds the extrema (local minima and maxima) of the Bezier curve. The `extrema()` method returns an object containing `values` which are the `t` values where extrema occur. The example draws circles at these `t` values. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); curve.extrema().values.forEach(t => this.drawCircle(curve.get(t),3)); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); curve.extrema().values.forEach(t => this.drawCircle(curve.get(t),3)); } ``` -------------------------------- ### Split Bezier Curve at t or t1, t2 Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Splits a Bezier curve into two new curves at a single `t` value, or into a segment defined by `t1` and `t2`. The example demonstrates splitting a curve and drawing the resulting segment in red, highlighting the split points. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.setColor("lightgrey"); this.drawCurve(curve); var c = curve.split(0.25, 0.75); this.setColor("red"); this.drawCurve(c); this.drawCircle(curve.get(0.25),3); this.drawCircle(curve.get(0.75),3); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); this.setColor("lightgrey"); this.drawCurve(curve); var c = curve.split(0.25, 0.75); this.setColor("red"); this.drawCurve(c); this.drawCircle(curve.get(0.25),3); this.drawCircle(curve.get(0.75),3); } ``` -------------------------------- ### Generate Graduated Curve Outlines (JavaScript) Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Creates graduated outlines for a Bezier curve using four distance values, representing offsets along the normal and anti-normal at the start and end of the curve. This method is useful for creating varying thickness outlines. Quadratic curves are automatically converted to cubic for this process. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var outline = curve.outline(5,5,25,25); outline.curves.forEach(c => this.drawCurve(c)); } ``` ```javascript new Bezier(102, 33, 16, 99, 101, 129, 132, 173 ); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var outline = curve.outline(5,5,25,25); outline.curves.forEach(c => this.drawCurve(c)); } ``` -------------------------------- ### Calculate Bezier Curve Tangent Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates the curve tangent at a specified `t` value. This method returns a non-normalized vector `{x: dx, y: dy}`. The example draws lines representing the tangent at various points along the curve. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); for(var t=0; t<=1; t+=0.1) { var pt = curve.get(t); var dv = curve.derivative(t); this.drawLine(pt, { x: pt.x + dv.x, y: pt.y + dv.y} ); } } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); for(var t=0; t<=1; t+=0.1) { var pt = curve.get(t); var dv = curve.derivative(t); this.drawLine(pt, { x: pt.x + dv.x, y: pt.y + dv.y} ); } } ``` -------------------------------- ### Calculate Bezier Curve Normal Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates the curve normal at a specified `t` value. This method returns a normalized vector `{x: nx, y: ny}`. The example draws lines representing the normal at various points along the curve, with a fixed length. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var pt, nv, d=20; for(var t=0; t<=1; t+=0.1) { var pt = curve.get(t); var nv = curve.normal(t); this.drawLine(pt, { x: pt.x + d*nv.x, y: pt.y + d*nv.y} ); } } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var pt, nv, d=20; for(var t=0; t<=1; t+=0.1) { var pt = curve.get(t); var nv = curve.normal(t); this.drawLine(pt, { x: pt.x + d*nv.x, y: pt.y + d*nv.y} ); } } ``` -------------------------------- ### Bezier.quadraticFromPoints and Bezier.cubicFromPoints Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Shows how to create quadratic and cubic Bezier curves from a set of points. Bezier.quadraticFromPoints takes three points, while Bezier.cubicFromPoints takes three points and an optional fourth parameter 'd1' for strut length. ```javascript false; var B = {x: 100, y: 50}; var tvalues = [0.2, 0.3, 0.4, 0.5]; var curves = tvalues.map(t => Bezier.quadraticFromPoints({x:150, y: 40}, B, {x:35, y:160}, t)); var draw = function() { var offset = {x:45,y:30}; curves.forEach((b,i) => { this.drawSkeleton(b, offset, true); this.setColor("rgba(0,0,0,0.2)"); this.drawCircle(b.points[1], 3, offset); this.drawText("t=" + tvalues[i], { x: b.points[1].x + offset.x - 15, y: b.points[1].y + offset.y - 10, }); this.setRandomColor(); this.drawCurve(b, offset); }); this.setColor("black"); this.drawCircle(curves[0].points[0], 3, offset); this.drawCircle(curves[0].points[2], 3, offset); this.drawCircle(B, 3, offset); } ``` ```javascript false; var p1 = {x:110, y: 50}, B = {x: 50, y: 80}, p3 = {x:135, y: 100}; var tvalues = [0.2, 0.3, 0.4, 0.5]; var curves = tvalues.map(t => Bezier.cubicFromPoints(p1, B, p3, t)); var draw = function() { var offset = {x:0,y:0}; curves.forEach((b,i) => { this.setRandomColor(); this.drawCurve(b, offset); }); this.setColor("black"); this.drawCircle(curves[0].points[0], 3, offset); this.drawCircle(curves[0].points[3], 3, offset); this.drawCircle(B, 3, offset); } ``` -------------------------------- ### Bezier.quadraticFromPoints and Bezier.cubicFromPoints Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Creates Bezier curves from a set of points. `quadraticFromPoints` takes three points, and `cubicFromPoints` takes three points and an optional parameter for defining the curve's shape. ```APIDOC ## Bezier.quadraticFromPoints(p1, p2, p3, t) / Bezier.cubicFromPoints(p1, p2, p3, t, d1) ### Description Create a curve through three points. ### Method `Bezier.quadraticFromPoints(p1, p2, p3, t)` `Bezier.cubicFromPoints(p1, p2, p3, t, d1)` ### Parameters #### Path Parameters - **p1** (Object) - The first point `{x:(num),y:(num),z:(num)}`. - **p2** (Object) - The second point `{x:(num),y:(num),z:(num)}`. - **p3** (Object) - The third point `{x:(num),y:(num),z:(num)}`. - **t** (number) - Optional. A value between 0 and 1 that influences the curve's shape. Defaults to 0.5. - **d1** (number) - Optional (for `cubicFromPoints` only). Controls the strut length for building a cubic curve. If omitted, a length based on `B--C` is used. ### Request Example (Quadratic) ```javascript const p1 = {x: 150, y: 40}; const B = {x: 100, y: 50}; const p3 = {x: 35, y: 160}; const curve = Bezier.quadraticFromPoints(p1, B, p3, 0.4); ``` ### Request Example (Cubic) ```javascript const p1 = {x: 110, y: 50}; const B = {x: 50, y: 80}; const p3 = {x: 135, y: 100}; const curve = Bezier.cubicFromPoints(p1, B, p3, 0.5, 20); ``` ### Response #### Success Response (200) - **Bezier** (Object) - A Bezier curve object representing the constructed curve. ``` -------------------------------- ### Curve Fitting Source: https://github.com/jiwonme/kirb/blob/main/README.md Methods for fitting Bezier curves to a set of points. ```APIDOC ## Curve Fitting ### `quadraticFromPoints(p1, p2, p3, t)` Fit a quadratic Bezier curve to three points `p1`, `p2`, and `p3`, potentially using parameter `t` for influence. **Parameters** - `p1` (object) - The first point. - `p2` (object) - The second point. - `p3` (object) - The third point. - `t` (number, optional) - Parameter influencing the fit. **Response** - Returns a new quadratic Bezier curve. ### `cubicFromPoints(S, B, E, t)` Fit a cubic Bezier curve to start point `S`, end point `E`, and control points `B`, potentially using parameter `t`. **Parameters** - `S` (object) - The start point. - `B` (object) - The control point(s). - `E` (object) - The end point. - `t` (number, optional) - Parameter influencing the fit. **Response** - Returns a new cubic Bezier curve. ``` -------------------------------- ### Core Methods Source: https://github.com/jiwonme/kirb/blob/main/README.md These methods provide fundamental operations for working with Bezier curves, such as retrieving points, calculating derivatives, and obtaining geometric properties. ```APIDOC ## Core Methods ### `get(t)` Get point at position t (0-1) on the curve. **Parameters** - `t` (number) - The parameter value between 0 and 1. ### `compute(t)` Compute point on the curve. **Parameters** - `t` (number) - The parameter value between 0 and 1. ### `derivative(t)` Get the first derivative of the curve at position t. **Parameters** - `t` (number) - The parameter value between 0 and 1. ### `normal(t)` Get the normal vector to the curve at position t. **Parameters** - `t` (number) - The parameter value between 0 and 1. ### `length()` Calculate the total length of the curve. ### `bbox()` Get the bounding box of the curve. ### `extrema()` Find the points where the curve reaches its extreme values (min/max x and y). ``` -------------------------------- ### Bezier Constructor: Quadratic and Cubic Curves Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Demonstrates the usage of the Bezier constructor for creating both quadratic and cubic Bezier curves. The constructor can accept numerical arguments or coordinate objects for 2D and 3D curves. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); } ``` -------------------------------- ### New & Improved Methods Source: https://github.com/jiwonme/kirb/blob/main/README.md This section details the newer and enhanced methods in the Kirb library, focusing on offset operations, search and analysis, and utility functions. ```APIDOC ## New & Improved Methods ### Offset Operations #### `offsetPoint(t, distance)` Get an offset point at a specific parameter `t` with a given `distance` from the curve. Returns the offset point, its normal, and the parameter `t`. **Parameters** - `t` (number) - The parameter value between 0 and 1. - `distance` (number) - The distance to offset from the curve. **Response Example** ```json { "point": {"x": 10, "y": 20}, "normal": {"x": 0.5, "y": 0.5}, "t": 0.5 } ``` #### `offsetCurve(distance)` Create a new curve that is parallel to the original curve at a specified `distance`. **Parameters** - `distance` (number) - The distance to offset. **Response** - Returns an array of Bezier curves representing the offset outline. #### `offset(t, d?)` Legacy method for offset operations. Still functional but `offsetPoint` and `offsetCurve` offer a clearer API. **Parameters** - `t` (number) - The parameter value between 0 and 1. - `d` (number, optional) - The distance to offset. ### Search & Analysis #### `findParameter(point, options)` Find the parameter value(s) `t` for a given `point` on the curve, with optional `tolerance`. **Parameters** - `point` (object) - The point to find the parameter for (e.g., `{x: 10, y: 20}`). - `options` (object, optional) - Configuration options. - `tolerance` (number) - The allowed tolerance for matching the point. **Response Example** ```json { "t": 0.5, "hits": [{"x": 10, "y": 20}] } ``` #### `closestPoint(point)` Find the point on the curve that is closest to the given `point`. **Parameters** - `point` (object) - The reference point (e.g., `{x: 10, y: 20}`). **Response** - Returns the closest point on the curve. #### `contains(point, options)` Check if a given `point` lies on the curve within a specified `tolerance`. **Parameters** - `point` (object) - The point to check (e.g., `{x: 10, y: 20}`). - `options` (object, optional) - Configuration options. - `tolerance` (number) - The allowed tolerance for the point to be considered on the curve. **Response** - Returns `true` if the point is on the curve, `false` otherwise. #### `on(point, error)` Legacy method to check if a point is on the curve. `contains` is recommended. **Parameters** - `point` (object) - The point to check. - `error` (number, optional) - The allowed error margin. ### Utility Methods #### `sample(count)` Generate an array of `count` evenly spaced points along the curve. **Parameters** - `count` (number) - The number of points to sample. **Response** - Returns an array of points. #### `getInfo()` Retrieve metadata about the curve, such as its type and control points. **Response** - Returns an object containing curve information. ``` -------------------------------- ### Bezier Constructor Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Constructs a Bezier curve. Supports quadratic and cubic curves in 2D and 3D, accepting either numerical arguments or coordinate objects. ```APIDOC ## Bezier(...) ### Description Quadratic and cubic 2D/3D Bezier curve constructor. For quadratic curves, the constructor can take either 6 or 9 numerical arguments (for 2d and 3d curves respectively) or 3 `{x:(num),y:(num),z:(num)}` coordinate objects. The `z` property for coordinates is optional, and controls whether the resulting curve is 2d or 3d. For cubic curves, the constructor can take either 8 or 12 numerical arguments (for 2d and 3d curves respectively) or 4 `{x:(num),y:(num),z:(num)}` coordinate objects. The `z` property for coordinates is optional, and controls whether the resulting curve is 2d or 3d. ### Method `new Bezier(...)` ### Parameters #### Request Body (for coordinate objects) - **points** (Array) - An array of objects, where each object represents a point with `x`, `y`, and optional `z` properties. - **x** (number) - The x-coordinate. - **y** (number) - The y-coordinate. - **z** (number) - Optional. The z-coordinate. ### Request Example (2D Quadratic with numbers) ```javascript new Bezier(150, 40, 80, 30, 105, 150); ``` ### Request Example (3D Cubic with coordinate objects) ```javascript new Bezier({x: 100, y: 25, z: 0}, {x: 10, y: 90, z: 5}, {x: 110, y: 100, z: 10}, {x: 150, y: 195, z: 20}); ``` ### Response #### Success Response (200) - **Bezier** (Object) - A Bezier curve object representing the constructed curve. ``` -------------------------------- ### Import Bezier.js Library Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Imports the Bezier class from the local 'bezier.js' file and makes it globally accessible as 'Bezier'. This is a common pattern for including library code in a web project. ```javascript import { Bezier } from "./js/bezier.js"; window.Bezier = Bezier; ``` -------------------------------- ### Error Handling Source: https://github.com/jiwonme/kirb/blob/main/README.md Information on how the Kirb library handles errors, including custom error types and codes. ```APIDOC ## Error Handling The Kirb library uses a custom `KirbError` class for exceptions. This allows for typed error handling and provides specific error codes and details. ### `KirbError` Custom error class used throughout the library. **Properties** - `code` (string) - An error code (e.g., `'OUT_OF_RANGE'`). - `details` (object) - Additional information about the error. ### Example Usage ```javascript import { Bezier, KirbError, ErrorCodes } from 'kirb'; try { const curve = new Bezier(0, 0, 1, 1, 0, 1, 1, 0); curve.offsetPoint(2, 10); // Example of an invalid parameter that might throw an error } catch (e) { if (e instanceof KirbError) { console.error(`Kirb Error: ${e.code}`); console.error('Details:', e.details); } else { console.error('An unexpected error occurred:', e); } } ``` **Common Error Codes** - `OUT_OF_RANGE`: Indicates a parameter value is outside the expected range (typically 0 to 1 for Bezier curve parameters). - `INVALID_ARGUMENT`: Indicates that one or more arguments provided to a function are invalid. ``` -------------------------------- ### TypeScript Usage of Bezier Curves and Error Handling Source: https://github.com/jiwonme/kirb/blob/main/README.md Illustrates how to use the Bezier class in TypeScript with full type safety, including type inference for points and bounding boxes, and demonstrates structured error handling for KirbError exceptions. ```typescript import { Bezier, KirbError, ErrorCodes, type Point, type BoundingBox } from 'kirb'; // Full type safety const curve = new Bezier(0, 0, 100, 25, 200, 75, 300, 100); // Type inference const point: Point = curve.get(0.5); const bbox: BoundingBox = curve.bbox(); // Error handling with types try { curve.offsetPoint(2, 10); } catch (e) { if (e instanceof KirbError) { console.log(e.code); // Typed! console.log(e.details); // Typed! } } ``` -------------------------------- ### Analysis Source: https://github.com/jiwonme/kirb/blob/main/README.md Perform advanced analysis on Bezier curves, including curvature, inflections, arc approximation, and simplification. ```APIDOC ## Analysis ### `curvature(t)` Get the curvature of the curve at parameter `t`. **Parameters** - `t` (number) - The parameter value between 0 and 1. **Response** - Returns the curvature value. ### `inflections()` Find all inflection points on the curve. **Response** - Returns an array of parameter values where inflections occur. ### `arcs(threshold)` Approximate segments of the curve with circular arcs, useful for rendering or simplification. The `threshold` parameter controls the approximation accuracy. **Parameters** - `threshold` (number) - The maximum allowed deviation from the curve for an arc segment. **Response** - Returns an array of arc segments. ### `reduce()` Simplify the curve by reducing it to the simplest possible Bezier segments (e.g., lines or lower-degree curves where applicable). **Response** - Returns an array of simplified Bezier curves. ``` -------------------------------- ### .getLUT(steps) Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Generates a Look Up Table (LUT) of coordinates on the curve, spaced at parametrically equidistance intervals. ```APIDOC ## .getLUT(steps) ### Description Generates a **L**ook**U**p **T**able of coordinates on the curve, spaced at parametrically equidistance intervals. If `steps` is given, the LUT will contain `steps+1` coordinates representing the coordinates from `t=0` to `t=1` at interval `1/steps`. ### Method `curve.getLUT(steps)` ### Parameters #### Query Parameters - **steps** (number) - Optional. The number of intervals to divide the curve into. If omitted, a default value of `steps=100` is used. ### Request Example ```javascript const curve = new Bezier(150, 40, 80, 30, 105, 150); const lut = curve.getLUT(16); ``` ### Response #### Success Response (200) - **LUT** (Array) - An array of coordinate objects `{x:(num),y:(num),z:(num)}` representing points along the curve. ``` -------------------------------- ### Bezier.getLUT(steps) Method Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Generates a Look Up Table (LUT) of coordinates along a Bezier curve. The LUT provides parametrically equidistant points, with the number of steps determined by the 'steps' argument. Defaults to 100 steps if not provided. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); var LUT = curve.getLUT(16); LUT.forEach(p => this.drawCircle(p,2)); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); var LUT = curve.getLUT(16); LUT.forEach(p => this.drawCircle(p,2)); } ``` -------------------------------- ### Geometric Operations Source: https://github.com/jiwonme/kirb/blob/main/README.md Perform various geometric transformations and analyses on Bezier curves, including splitting, outlining, projection, and intersection detection. ```APIDOC ## Geometric Operations ### `split(t1, t2)` Split the curve into segments defined by the parameter range [t1, t2]. **Parameters** - `t1` (number) - The starting parameter value (0-1). - `t2` (number) - The ending parameter value (0-1). **Response** - Returns a new Bezier curve representing the split segment. ### `outline(d1, d2)` Generate an outline (offset curve) with offsets `d1` and `d2` on either side of the original curve. **Parameters** - `d1` (number) - The offset distance on one side. - `d2` (number) - The offset distance on the other side. **Response** - Returns an array of Bezier curves representing the outline. ### `project(point)` Project a given `point` onto the closest point on the curve. **Parameters** - `point` (object) - The point to project (e.g., `{x: 10, y: 20}`). **Response** - Returns the projected point on the curve. ### `intersects(curve)` Find all intersection points between the current curve and another Bezier `curve`. **Parameters** - `curve` (Bezier) - The other Bezier curve to intersect with. **Response** - Returns an array of intersection points or parameter values. ``` -------------------------------- ### Generate Bezier Curve Outlines Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Generates outlines for a Bezier curve. This function can accept one or multiple distance arguments to define the offsets for the outlines. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var doc = c => this.drawCurve(c); var outline = curve.outline(25); outline.curves.forEach(doc); this.setColor("rgba(0,0,255,0.3)"); outline.offset(10).curves.forEach(doc); outline.offset(-10).curves.forEach(doc); } ``` -------------------------------- ### .length() Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates the total length of the Bezier curve. ```APIDOC ## .length() ### Description Calculates the total length of the Bezier curve. ### Method `curve.length()` ### Parameters None ### Request Example ```javascript const curve = new Bezier(150, 40, 80, 30, 105, 150); const len = curve.length(); ``` ### Response #### Success Response (200) - **length** (number) - The calculated length of the curve. ``` -------------------------------- ### Generate Curve Outline Shapes (JavaScript) Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Generates a Bezier curve's outline as a series of discrete shapes, each defined by startcap, forward, endcap, and back Bezier curves. This method is an alternative to generating a continuous path. It can take one or two distance values for uniform or differential offsetting. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); curve.outlineshapes(25).forEach(s => { this.setRandomFill(0.2); this.drawShape(s); }); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); curve.outlineshapes(25).forEach(s => { this.setRandomFill(0.2); this.drawShape(s); }); } ``` -------------------------------- ### Calculate Bezier Curve Length Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates the length of a Bezier curve using numerical approximation (Legendre-Gauss quadrature). The code instantiates a Bezier curve and then calls the `length()` method. It is intended for demonstrating the arc length calculation. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var arclength = curve.length(); // ... rest of the drawing logic ... ``` ```javascript new Bezier(100, 25, 10, 90, 110, 100, 132, 192 ); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); var arclength = curve.length(); // ... rest of the drawing logic ... ``` -------------------------------- ### Generate Curve Outline with Single Offset (JavaScript) Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Generates an outline for a Bezier curve at a specified distance along its normal and anti-normal. The result is a PolyBezier object containing outline segments. Dependencies include the Bezier and PolyBezier classes. Input is a distance value; output is a PolyBezier object. ```javascript new Bezier(102, 33, 16, 99, 101, 129, 132, 173 ); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); const drawCurve = c => this.drawCurve(c); var outline = curve.outline(25); outline.curves.forEach(drawCurve); this.setColor("rgba(0,0,255,0.3)"); outline.offset(10).curves.forEach(drawCurve); outline.offset(-10).curves.forEach(drawCurve); } ``` -------------------------------- ### Bounding Box Calculation Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates the bounding box for the Bezier curve. This is typically done once and cached for efficiency. ```APIDOC ## GET /curves/bbox ### Description Calculates the bounding box for this curve. ### Method GET ### Endpoint /curves/bbox ### Parameters None ### Response #### Success Response (200) - **xMin** (number) - The minimum x-coordinate of the bounding box. - **yMin** (number) - The minimum y-coordinate of the bounding box. - **xMax** (number) - The maximum x-coordinate of the bounding box. - **yMax** (number) - The maximum y-coordinate of the bounding box. #### Response Example ```json { "xMin": 10, "yMin": 20, "xMax": 150, "yMax": 180 } ``` ``` -------------------------------- ### Extrema Calculation Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates all extrema (local maxima and minima) for each dimension of a Bezier curve. The result includes the 't' values where extrema occur for x, y, and z dimensions, as well as an aggregated list of 't' values. ```APIDOC ## GET /curves/extrema ### Description Calculates all the extrema on a curve for each dimension. ### Method GET ### Endpoint /curves/extrema ### Parameters None ### Response #### Success Response (200) - **x** (array) - Array of 't' values where x-dimension extrema occur. - **y** (array) - Array of 't' values where y-dimension extrema occur. - **z** (array, optional) - Array of 't' values where z-dimension extrema occur (only if curve is 3D). - **values** (array) - Aggregate array of all 't' values where extrema occur across all dimensions. #### Response Example ```json { "x": [0.1, 0.5, 0.9], "y": [0.2, 0.6], "values": [0.1, 0.2, 0.5, 0.6, 0.9] } ``` ``` -------------------------------- ### Generate Hull Points for Bezier Curve (JavaScript) Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Generates all hull points for a Bezier curve at specified t-value across all iterations. The number of points generated depends on whether the curve is quadratic or cubic. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("rgb(255,100,100)"); var hull = curve.hull(0.5) this.drawHull(hull); this.drawCircle(hull.slice(-1)[0], 5); } ``` ```javascript new Bezier(100,25 , 10,90 , 50,185 , 170,175); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("rgb(255,100,100)"); var hull = curve.hull(0.5) this.drawHull(hull); this.drawCircle(hull.slice(-1)[0], 5); } ``` -------------------------------- ### Approximate Bezier Curve as Arcs Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Approximates a Bezier curve using a sequence of circular arcs. An optional threshold parameter can be provided to control the accuracy of the arc fit. This function is only supported in 2D. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); var arcs = curve.arcs(); this.setColor("black"); arcs.forEach(arc => { this.setRandomFill(0.1); this.drawArc(arc); }); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); var arcs = curve.arcs(); this.setColor("black"); arcs.forEach(arc => { this.setRandomFill(0.1); this.drawArc(arc); }); } ``` -------------------------------- ### Calculate Bounding Box for Bezier Curve (JavaScript) Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates the bounding box for a Bezier curve, based on its hull coordinates and extrema. If the bounding box is not cached, it will be computed. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("rgb(255,100,100)"); this.drawbbox(curve.bbox()); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 150,195); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("rgb(255,100,100)"); this.drawbbox(curve.bbox()); } ``` -------------------------------- ### Project Point onto Bezier Curve (JavaScript) Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Finds the closest on-curve point to a given off-curve point using a two-pass projection test. This method is useful for tasks like finding the nearest point on a curve to a mouse cursor. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function(evt) { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("rgb(255,100,100)"); if (evt) { var mouse = {x: evt.offsetX, y: evt.offsetY}; var p = curve.project(mouse); this.drawLine(p,mouse); } } ``` ```javascript new Bezier(100,25 , 10,90 , 50,185 , 170,175); var draw = function(evt) { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("rgb(255,100,100)"); if (evt) { var mouse = {x: evt.offsetX, y: evt.offsetY}; var p = curve.project(mouse); this.drawLine(p,mouse); } } ``` -------------------------------- ### Bezier.length() Method Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Calculates the total length of the Bezier curve. This method is useful for various applications like animation along a path or determining the physical extent of the curve. ```javascript new Bezier(150,40 , 80,30 , 105,150); // var length = curve.length(); ``` -------------------------------- ### Offset Bezier Curve Source: https://github.com/jiwonme/kirb/blob/main/docs/index.html Creates a new curve offset along the curve normals. If only a distance is provided, it returns an array of curves forming the offset. If a distance and a 't' value are given, it returns a coordinate point on the offset curve. ```javascript new Bezier(150,40 , 80,30 , 105,150); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); curve.offset(25).forEach(c => this.drawCurve(c)); this.drawPoint(curve.offset(0.5,25)); } ``` ```javascript new Bezier(100,25 , 10,90 , 110,100 , 145,179); var draw = function() { this.drawSkeleton(curve); this.drawCurve(curve); this.setColor("red"); curve.offset(25).forEach(c => this.drawCurve(c)); this.drawPoint(curve.offset(0.5,25)); } ```