![]() |
Beiser Curves
I've recently been trying to figure how to correctly implement this into Graal. I've also been trying to improve my math-based scripting. :D This is a sample script I had to test the outcome of the 4 given points. The solution I get is x1 and x2 = 5.82375. I just need to find out if this is correct? And if it isn't what is the correct way of doing it, and how can I improve onto my knowledge of scripting? In case your wondering how I got the variable t. And yes I spelled Beiser wrong it should be Bezier.
The t parameter (varying from 0 to 1) cuts the P1-P4 segment to intervals, according to the wanted accuracy. When t = 0 results are t(x1) = P1(x1) and t(x2)= P1(x2) When t = 1 results are t(x1) = P4(x1) and t(x2)= P4(x2) PHP Code:
PHP Code:
|
Well first off, throw temp in front of variables that are only going to be used locally otherwise they're declared globally which is just annoying.
You should modify it so you can call the function and get the result from it. I.e: PHP Code:
PHP Code:
PHP Code:
http://en.wikipedia.org/wiki/Bézier_curve |
A point is not a number, it is a pair of numbers. Which means how you wrote a, b, c, and d is wrong.
Points also have slightly different multiplication and addition rules than numbers... given two points P1 = (X1, Y1) and P2 = (X2, Y2): P1 + P2 = (X1, Y1) + (X2, Y2) = (X1 + X2, Y1 + Y2) Given a number C and a point P = (X,Y) C*P = C*(X,Y) = (C*X,C*Y) In GS, you can represent a point as an array with two numbers in it. Like {1,2} or {3.5,10}. So, let's investigate the problem at hand: This is how I describe the function Beizer in terms of what it wants and what it gives back: Given 3 points and 1 number, I'll give you back a point. So, translated into GS2... PHP Code:
The formula for a cubic beizer curve is this: B(t) = (1 - t)^3 * a + (1 - t)^2 * 3 * t * b + (1 - t) * 3 * t^2 * c + t^3 * d This almost translates directly into GS, but we need to take into account the fact that we are working with points (a, b, c, and d), and numbers (t), instead of only numbers. To get the coordinates of the points out of the array, for example with a, do this: a[0] would give you the first coordinate, and a[1] would give you the second. So (1 - t)^3 * a in the original formula is actually {(1 -t)^3 * a[0], (1 - t)^3 * a[1]} in GS2. Thankfully, GS already has functions which do these operations for us: vectorscale(point, number) and vectoradd(point, point). So (1 - t)^3 * a in the original formula can be written as vectorscale(a, (1-t)^3). Now it is just a matter of putting this all together... PHP Code:
PHP Code:
The alternative to the vector functions is to write out the formula twice for each coordinate of the point, but that means you'll have duplicate code which gets annoying to maintain. Edit: Okay this post took me forever to write, fowlplay beat me to it :(. Hopefully this is still useful since it shows another way of writing the code. |
Thanks a ton. Yeah I had the thought in the back of my head saying use arrays but this has been a good experience. I also have another question I heard some type or form of Bezier Curves can be used in ways of Interpolation which I got the idea from looking at Downsider's Interpolation script. I can't really jot out what it would look like in GS form. Any ideas?
|
You can't really plug-and-play Bezier curves into downsider's interpolation script.
When you draw a Bezier curve through a set of control points, it does not need to touch the all control points, only the start and end points. For interpolation, this is obviously useless because you won't even be hitting the points that you know the object has been at. However, it is possible to calculate control points from a set of points that you want the Bezier curve to pass through. But this takes a good amount of math, and is also more difficult to get running efficiently than simple interpolation like linear or cosine. |
Im assuming your referring to Linear Bezier Spline, which is obtained by linear interpolation between two control points P0 , P1?
|
This idea is called "spline interpolation", where you use piecewise polynomials to interpolation. Normally this is done with basis functions which results in B-splines. But it is also possible to use Bernstein polynomials which results in Bezier splines.
Linear spline interpolation gets you nothing more than normal linear interpolation, you need to go into quadratic and cubic spline interpolation to see the differences. |
Alright I'll take a look into it, thanks.
|
| All times are GMT +2. The time now is 05:12 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.