Showing posts with label dashed. Show all posts
Showing posts with label dashed. Show all posts

Friday, January 29, 2010

Drawing Dashed Lines, Arcs, and Cubic Curves

The ActionScript Graphics class supports drawing solid lines and quadratic curves. But there isn't native support for dashed lines or cubic (Bezier) curves.

I've created a sample application that shows the three line types: straight, quadratic curve and cubic curve. It also lets you choose between showing a solid line, a dashed line, or both. The line thickness can also be adjusted.



I also got a little carried away and added the quadratic and cubic control points as buttons that you can drag around the canvas to change the curve.

Most of the drawing functionality for these examples is in the GraphicsUtils.as class that contains the following static functions:
  • drawLine() - draws a straight line, either solid (using Graphics.lineTo() function) or dashed
  • drawQuadCurve() - draws a quadratic curve, either solid (using Graphics.curveTo() function) or dashed
  • drawCubicCurve() - draws a cubic curve (two control points), either solid or dashed. The curve is an approximation done by dividing the curve into many small segments and drawing straight lines
  • drawCircle() - draws a circle, either solid (using Graphics. drawCircle() function) or dashed
  • drawArc() - draws an arc, either solid or dashed
The dashed lines are drawn by dividing the line into many small segments to draw the dashes. By default a dash length of 10 pixels is used, but that is customizable.

The equations used to calculate the values at any point along the line for the three cases were found on the Wikipedia entry for Bézier curves.