How to draw an anti-aliased bezier curve in raster? - actionscript-3

Does anyone know a decent algorithm for drawing a anti-aliased (smooth) quadratic bezier curves in a raster?
I could simply draw them as vectors and then copy the image to a raster..
Is there any clever yet freely available algorithm to draw the curve directly to the pixels?
I am currently using the algorithm implemented here:
http://www.bytearray.org/?p=67
quadBezier()
I am wondering if there is a way to change it to render anti-aliased bezier?
Thanks in advance.
I am doing this for learning purposes and because I am hoping it would be faster than using the copy pixels from a Shape option.
Here is an example showing the none-smooth bezier is faster than the regular bezier:
http://lab.generalrelativity.org/raster/

Implementing your own algorithm will almost certainly be slower than using bitmapData.draw(shape).
When you use graphics.lineTo, graphics.curveTo etc, you are constructing a model, but not actually drawing anything yet. It's only after the shape gets added to the stage, and during the subsequent [render] phase that the model is traversed and pixels drawn by a fast algorithm written in C. When you use bitmapData.draw to draw a vector into a bitmap, it will use the same native code. So it isn't as if you are drawing anything twice, as you might imagine, and it will be hard (or impossible) to beat for speed by implementing anything from scratch in AS3 alone.

Related

Is it possible to create a visualization like this in WebGL?

This visualization is created using Away3D (Flash):
www.guardian.co.uk/world/interactive/2011/mar/22/middle-east-protest-interactive-timeline
Would it be possible to create something of the same quality (re. interaction, animation, performance, pixel-perfection etc.) using WebGL?
Bonus: How would one set up the basic scene? (without interaction and animation)
Yes, it would be. The scene is not complex at all, so it would have good performance. Interaction, yes, depends how you implement it, but if you are doing project with, for example, Three.js it wouldn't be a problem. Pixel perfection, obviously, yes.
In the scene, you could have curved plane, with texture that is changing UVs of vertices when you use 'navigate'. Pins could be done with particle, or better, simple quads with transparent textures. To have pixel-precise pin picking, you could depth-test pins on ray trace, or use pin-shaped geometry with shader, which probably the best solution.
So, basic scene - curved plane with per-vertex-changing-UV, pin-shaped and texture pins.
Alternatively, you could do exactly the same thing with 2D canvas. All elements are just drawn and scaled, text would be much simpler, and picking would be with 2d calculations.
Hope this helps.

AS3: How to intersect vectors at runtime?

Let's say I use the Graphics class at runtime to draw some vector shapes dynamically. For example a square and a circle.
Is there a way to create a new shape at runtime where those 2 vectors shapes overlap?
Those kind of operations are very common in all vector design programs such as Illustrator, Corel, etc... but I haven't found anything in Adobe's documentation, nor anywhere else, to do it by code.
Although drawing operations on the Graphics class are described in terms of lines, points etc this is - as far as you're concerned - just telling it what to draw onto a bitmap. There's no way to remove a shape once drawn, short of clear(), which just wipes the whole thing clean.
I don't fully understand why, as the vector data must be retained - there's no loss of quality on scaling after drawing, for example.
If you don't want to get into some hardcore maths (for anything beyond straight lines, you'll need to) there's an answer here which might help if you've ever used PixelBender:
How to calculate intersection between shapes in flash / action script ? (access to shape's segments and nodes?)
Failing that, if it's just cosmetic you could play around with masking shapes (will probably end up quite hacky though) - however, if you actually want to use the intersection to draw or describe a shape you will need to dig out your maths book or look for a good graphics library.
Hope this helps

How to draw Quadratic Bezier Curves in Stage 3d? Would I need to break them to segments?

I am currently using the native .curveTo function and wondering if I could write some code that will render Quadratic Bezier Curves in Stage3D? I also may need to perform fills between the curves I draw to the screen.
Fills are supported by the extension
https://github.com/unwrong/Starling-Extension-Graphics/blob/master/extension/src/starling/display/graphics/Fill.as
It uses a triangulator under the hood to process the fill into something the GPU can handle.
It has already been implemented for Starling. Hope you'll find it useful:
https://github.com/unwrong/Starling-Extension-Graphics/pull/1

Drawing shapes on BitmapData, or geting its graphics context

what would be your recommendation for drawing shapes (rects, circles...) onto BitmapData, and how to effectively switch between colors.
Is there any way to get graphics context from BitmapData so I could easily paint shapes using graphics.draw...()?
Why do you want to use a BitmapData? Not sure what you're after, but after reading a couple of your questions it seems you're a kind of fighting against how flash works. I think you'll make things much easier for yourself if you use what's available already. BitmapData objects are meant mainly to manipulate pixels and don't expose methods for drawing shapes. A Graphics object (available through Sprite, Shape, etc) on the other hand, allows you to draw vector shapes easily.
So, the best approach for this, I guess, would be using the drawing API to do what you want and then, if needed, convert the graphic to a BitmapData.

Breaking a concave polygon into convex ones

I'm using a game physics library (Box2D) which only supports convex polygon shapes. However, I'd like the level builder to be able to just specify concave polygons without having to worry about that.
So, how can I automatically break apart a concave polygon into convex ones (or even all triangles).
Speed would be cool, but ease of implementation is more important. The breaking apart will only be done on game initialization.
(My language is Flash/ActionScript 3, but that shouldn't matter)
Bernard Chazelle and David P. Dobkin presented an algorithm for that in 1985: Optimal Convex Decompositions.
Other approaches can be found on Wikipedia.
you probabaly need triangulation
This page explains how to convert polygons into non-complex shapes using ActionScript 3. The code is large so I wont copy paste here.
http://www.emanueleferonato.com/2011/09/12/create-non-convex-complex-shapes-with-box2d/