actionscript-3optimizationgeometrystarling-framework

Optimizing electric field lines drawing algorithm


I have written an algorithm which draws the electric field lines of charges i space. When there are more than one charges most of lines are curved. Since Starling does not have drawLine(x1,y1,x2,y2) kind of function.

I am using a small image of line and drawing it repeatedly on the screen to get a curved line. When all the field lines are drawn I may end up drawing thousands of small line segments on the screen. Since they have same texture this should not be a problem but still I am unsure whether it is a good idea or not.

Another technique I am thinking of is that I first make a flash sprite and draw all lines inside then draw it inside a bitmap and create a texture from that bitmap. Still I think it is not a good Idea to do this every ENTER_FRAME event.

var s:flash.display.Sprite = new flash.display.Sprite();
//drawing inside sprite goes here
var bmd:BitmapData = new BitmapData(w, h, true, 0xffffff);
bmd.draw(s);
var texture:Texture = Texture.fromBitmapData(bmd);

var img:Image = new Image(texture);
addChild(img)

Solution

  • If I understand, you are plotting smooth curves available as polylines of short sides and you want to optimize by replacing the polylines with curved primitives to compress the representation.

    In the first place, you can minimize the number of line segments by means of a polyline simplification algorithm, such as Douglas-Peucker.

    You can also generalize this concept by recursively splitting the polyline and choosing one or two intermediate points (one for a decompsition in circular arcs or Bezier quadrics, two for Bezier cubics), and checking if a deviation criterion is met or not.

    You will see the number of required arcs dropping sharply.

    If your curves have known angular points, make sure to force a split there, as smooth curves can't render them easily.