MODULE : AGS Bezier curves

Started by Monsieur OUXX, Tue 14/04/2015 16:07:52

Previous topic - Next topic

Monsieur OUXX

Beziers curves are a (not too slow) way of representing curves onto a screen. Look them up in Wikipedia. Not to be confused with Splines.
This module lets you define the settings of a curve and then render it onto a drawing surface.
The demo lets you see in real time how different settings render the curves.

Code: ags

//
//  AGS BEZIER MODULE
//
//  Summary : Draws Bezier curves onto the drawingSurface of your choice. (see http://en.wikipedia.org/wiki/B%C3%A9zier_curve )
//
//  AGS version: tested on AGS 3.3.0 RC1 (but also surely works with 3.2+ and 3.4+)
//  Author : 'Indiana Jones and the Seven Cities of Gold' development team. Support contact : 'Monsieur Ouxx' on the AGS forums
//  AGS was originaly created by Chris Jones, greatest benefactor of mankind. There, I said it. Special thanks to the newer support team!
//
//  LICENSE
//  This code free of rights, provided you credit Tolga Birdal on one hand, and the 'Indiana Jones and the Seven Cities of Gold' development team on the other hand.
//
//  HOW TO USE:
//  - Either leave the 'Bezier.Init(50);' present in the module, or call it yourself from some other place.
//  - Call Bezier.AddPoint(x,y); each time you want to add a point to the curve
//  - Call Bezier.Render(drawingSurface); to render the curve onto that DrawingSurface.
//  - Call Bezier.ClearAll(); to erase all previously-defined points and start a new curve.
//
//  PLEASE NOTE: you can define only one curve at a time.
//
//  HISTORY : Version 1.1 adds on-demand density change and connects the dots with straight lines.


(screenshot)




            >>> DOWNLOAD v1.1 <<<


Known issues:
- (version 1.0  only!) This is not a bug, this is by (poor) design : That is a very simple implementation, with a "fixed" density of rendered points --> It means that rendering a short line will create a very "dense" curve (with a lot of puffy pixels along the curve) while rendering a long line will create a very thin line with "holes" in it.
- (version 1.1) None.
 

AnasAbdin

Man this is awesome ;-D
It would be great to use it to move characters and objects in paths 8-)

selmiak


Gurok

#3
The notes say otherwise, but I believe the for() loops mean this requires 3.4.0.3+.

Very nice. This is very impressive and nice and readable. I'd like to see variable density.

Could you just draw a line between points to avoid holes?

AnasAbdin's dreams could come true if you exposed the array of curve points.
[img]http://7d4iqnx.gif;rWRLUuw.gi

Monsieur OUXX

#4
Quote from: Gurok on Tue 14/04/2015 23:38:02
The notes say otherwise, but I believe the for() loops mean this requires 3.4.0.3+.
Wait what? Are you saying there is a "for" in my script? If there is one, I'd be utterly surprised. :D I didn't even know it was possible in AGS.

Quote from: Gurok on Tue 14/04/2015 23:38:02
I'd like to see variable density.
As I indicated in the script's header the basic code is inspired from someone else so I'm not exactly sure what defines the points' density but I believe it's simply the number of points you define for the curve rendering. So it's only a matter of calling 'Bezier.Init(666, 5000);' instead of 'Bezier.Init(666,50)' to increase the density by a factor of 100 (50-->5000).
EDIT: implemented in version 1.1

Quote from: Gurok on Tue 14/04/2015 23:38:02
Could you just draw a line between points to avoid holes?
Oh yes that would actually be very easy to implement, using array 'curve' in the module at rendering time. That's also the array you may expose using a little export/import.
Spoiler

Code: ags

//in the module's header
import float curve[]; //never sure of the syntax: maybe you need to remove the brackets

//in the module's body
float curve[];
export float curve[]; //never sure of the syntax: maybe you need to remove the brackets

[close]
EDIT: implemented in version 1.1



I did that just for fun in one hour and I wans't intending on upgrading the module but you guys made interesting suggestions, I might have a look ;) Can't promise though.
 

Gurok

#5
Quote from: Monsieur OUXX on Wed 15/04/2015 09:31:08
Quote from: Gurok on Tue 14/04/2015 23:38:02
The notes say otherwise, but I believe the for() loops mean this requires 3.4.0.3+.
Wait what? Are you saying there is a "for" in my script? If there is one, I'd be utterly surprised. :D I didn't even know it was possible in AGS.

Oh no, my mistake. Sorry. :-[ I must have been reading the C# code this morning :/
[img]http://7d4iqnx.gif;rWRLUuw.gi

Monsieur OUXX

BOOM. Updated. New screenshot, new version.
 

ollj

#7
this module is good as canvas di draw lines, but not too efficient in interpolating bezier curves.

first i was like, wow, like it, save me some time.

then i looked into it, and was very confused why it is using algorithms/subroutines that use a lot of factorials and Math.pow().
while this may work faster for very high degree Bezier curves than other simpler Bezier-plotting methods, very high Bezier curves are pretty useless, because you switch to other curve representations to calculate/approximating a high degree curve, even with your used algorithm. instead of using a 20th Degree bezier curve, you simply use NURBS or Fourier interpolation for a '"curve with many turns". I am pretty sure fast-fourier-transform in 2 dimensions (like wolfram aplhas people-like-curves) beats your algorithm in accuracy and speed if you would like a "very curved curve drawn".

what i am getting at is that this modules way of drawing Bezier curves is likely not a good way for ags. instead a simple recursive iterative function that repeatedly interpolates between 2 points of a list of points, each time shortening the list by 1 point, is likely smaller and faster for most cases within ags.

see it as a challenge to make nurbs (non uniform bezier splines) curves or interpolated 2d-sinosoid-curves (using fourier analysis) in ags.
while "fast fourier transform" is not too efficient to draw interpolate splines|curves, graphic cards are designed to calculate these, and such ascript may be educational.

Monsieur OUXX

I'm sure this module is not optimized in any possible way. And by that I mean that it's probably slower than a disabled turtle who has asthma.
However, it's cool because it exists. I ran my calculations, and I came to the conclusion that a module that exists is always better optimized than a module that doesn't exist. ;)
 

SMF spam blocked by CleanTalk