4.7. Drawing a Triangle
Problem
You want to draw a triangle at runtime.
Solution
Create a custom MovieClip.drawTriangle( ) method
using the Drawing API and invoke it on
a movie clip.
Discussion
You can determine and plot the vertices of a triangle given the lengths of two sides and the angle between them. This is a better approach than specifying the lengths of the three sides because knowing the lengths of two sides and the angle between them always determines a triangle, whereas three arbitrary sides may not fit together to make a triangle.
The custom drawTriangle( ) method accepts six
parameters:
-
ab The length of the side formed between points
aandb, as shown in Figure 4-4.-
ac The length of the side formed between points
aandc, as shown in Figure 4-4.-
angle The angle (in degrees) between sides
abandac.-
rotation The rotation of the triangle in degrees. If 0 or
undefined, sideacparallels the x axis.-
x The x coordinate of the centroid (the center point) of the triangle.
-
y The y coordinate of the centroid of the triangle.

Figure 4-4. The parameters used in the drawTriangle( ) method
Define the custom drawTriangle( ) method on
MovieClip.prototype to make it available to all
movie clip instances:
// Include the custom Math library from Chapter 5 to access Math.degToRad( ). #include "Math.as" MovieClip.prototype.drawTriangle = function (ab, ac, angle, rotation, x, y) { // Convert the ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access