Difference between revisions of "Script:Bézier curves"
m (link "custom script") |
(cat) |
||
Line 1: | Line 1: | ||
+ | [[Category:Scripts]] | ||
Bézier curves is a [[custom script]] written by [[User:apollolux|NeoLogiX]]. It is a script that ultimately takes four points and draws a Bézier curve. A later update which has not yet been posted stores the points on the line into a sorted array for future use, i.e. a curved path to make an object travel. | Bézier curves is a [[custom script]] written by [[User:apollolux|NeoLogiX]]. It is a script that ultimately takes four points and draws a Bézier curve. A later update which has not yet been posted stores the points on the line into a sorted array for future use, i.e. a curved path to make an object travel. | ||
Revision as of 20:19, 16 March 2013
Bézier curves is a custom script written by NeoLogiX. It is a script that ultimately takes four points and draws a Bézier curve. A later update which has not yet been posted stores the points on the line into a sorted array for future use, i.e. a curved path to make an object travel.
Contents
Download and Usage
Getting Started
The source code to generate the curve is at the bottom of this page. Simply copy and paste into the script file of your choice and include it.
API
- Object NPoint(int x, int y)
- a VERY necessary object for convenience in this code. You COULD write this code using separate variables for each point's x and y, but it would look very messy.
- NPoint midpoint(NPoint p1, NPoint p2)
- returns the midpoint of two NPoints as an NPoint.
- Array killseqduppts(Array arr)
- returns an array of NPoints with sequential duplicates removed.
- Array BezierLine(NPoint p1, NPoint p2, NPoint p3, NPoint p4, Color col, bool dot, int num)
- blits the actual Bézier curve on the screen. p2 is the control point for p1, p3 is the control point for p4, col is the color of the line, dot determines whether the curve is a solid curve or a dotted line curve, and num is an extra level of infinite recursion protection; returns the line as an array of NPoints as well.
About Bézier Curves
There are a number of sources on the subject of Bézier curves, way too many for me to remember or list, so I'll point you to Bézier curve's Wikipedia entry and you can go from there. The short of it is a Bézier curve (at least, for the purposes of this script) is a curved line recursively generated by two endpoints, each with a control point, using "de Casteljau's algorithm" to split a curve into two separate curves and eventually connect the dots.
The Curve Process
Here's the mathematical procedure used by this script to draw the curve:
- Take two endpoints, p1 & p4, and two control points, p2 for p1 & p3 for p4.
- Find the midpoints of three connecting lines (L12, L34, L23).
- Connect the midpoints of L12 & L34 to L23's midpoint to make lines L123 & L234.
- Connect the midpoints of those two lines to make line Q.
- Find the midpoint of Q, which will be the vertex point of the active curve.
- Recursively repeat the previous steps using the resulting point Q as the p4 of the resulting p1 side & as the p1 of the resulting p4 side.
Parameter num is an added measure against the possibility of a "too much recursion" error. Once num is less than or equal to zero, the recursion stops and starts returning control back to the calling function. Alternatively, one could use num to determine the level of detail in the curve's final line (works for both solid curves and dotted curves).