Recipe 4.2 Drawing a Curve

4.2.1 Problem

You want to draw a curve using ActionScript.

4.2.2 Solution

Set a line style with lineStyle( ) and then use the curveTo( ) method.

4.2.3 Discussion

When drawing lines or curves with ActionScript, the first thing you must do is define the line style, as shown in Recipe 4.1.

Once you have set a line style, you can draw a curve using the curveTo( ) method. The curveTo( ) method draws an approximation of a Bézier curve (though optimized for performance), which requires three points: a starting point, a control point, and a destination point. The starting point is always determined by the current pen position. The destination point is simply the point on the canvas to which you wish to draw. The control point is the point that determines the shape of the curve; it is calculated by determining where the tangents to the curve at the starting and destination points intersect. The control point is not actually on the curve, as shown in Figure 4-1.

Figure 4-1. The control point of a curve This simple example draws a curve:

```// Draw a curved line in myMovieClip_mc with a control point at (0,100) and a
// destination point of (100,100).
this.createEmptyMovieClip("myMovieClip_mc", 1);
myMovieClip_mc.lineStyle(1, 0x000000, 100);
myMovieClip_mc.curveTo(0, 100, 100, 100);```   Preface  Part I: Local Recipes  Chapter 1. ActionScript Basics  Chapter 2. Runtime Environment  Chapter 3. Color  Chapter 4. Drawing and Masking  Introduction  Recipe 4.1 Drawing a Line  Recipe 4.2 Drawing a Curve  Recipe 4.3 Drawing a Rectangle  Recipe 4.4 Drawing a Rounded Rectangle  Recipe 4.5 Drawing a Circle  Recipe 4.6 Drawing an Ellipse  Recipe 4.7 Drawing a Triangle  Recipe 4.8 Drawing Regular Polygons  Recipe 4.9 Filling a Shape with a Solid or Translucent Color  Recipe 4.10 Filling a Shape with a Gradient  Recipe 4.11 Filling a Shape with a Complex Gradient  Recipe 4.12 Scripting Masks  Chapter 5. Numbers and Math  Chapter 6. Arrays  Chapter 7. Movie Clips  Chapter 8. Text  Chapter 9. Strings  Chapter 10. Dates and Times  Chapter 11. Forms  Chapter 12. Objects and Custom Components  Chapter 13. Programming Sound  Part II: Remote Recipes  Part III: Applications  Appendix A. Unicode Escape Sequences for Latin 1 Characters  Colophon