Drawing a Triangle using ActionScript

by Elis Frugalo.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

Create a custom MovieClip.drawTriangle( ) method using the Drawing API and invoke it on a movie clip.

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 a and b

ac
The length of the side formed between points a and c

angle
The angle (in degrees) between sides ab and ac.

rotation
The rotation of the triangle in degrees. If 0 or undefined, side ac parallels 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.

Define the custom drawTriangle( ) method on MovieClip.prototype to make it available to all movie clip instances:

// Include the custom Math library to access Math.degToRad(  ).
   #include "Math.as"
MovieClip.prototype.drawTriangle = function (ab, ac, angle, rotation, x, y) {
 // Convert the angle between the sides from degrees to radians.
   angle = Math.degToRad(angle);
 // Convert the rotation of the triangle from degrees to radians.
   rotation = Math.degToRad(rotation);
 // Calculate the coordinates of points b and c.
   var bx = Math.cos(angle - rotation) * ab;
   var by = Math.sin(angle - rotation) * ab;
   var cx = Math.cos(-rotation) * ac;
   var cy = Math.sin(-rotation) * ac;
 // Calculate the centroid's coordinates.
   var centroidX = (cx + bx)/3 - x;
   var centroidY = (cy + by)/3 - y;
 // Move to point a, then draw line ac, then line cb, and finally ba (ab).
   this.moveTo(-centroidX, -centroidY);
   this.lineTo(cx - centroidX, cy - centroidY);
   this.lineTo(bx - centroidX, by - centroidY);
   this.lineTo(-centroidX, -centroidY);
   }

There are a few points about this method that bear further discussion.

Point a will always be the point of rotation, so you don't need to calculate it's coordinates. However, points b and c need to be calculated (using basic trigonometric ratios). We define point b at the end of line ab at an angle of angle in an unrotated triangle. To factor in rotation we subtract rotation from angle. We define point c to be at the end of line ac. In an unrotated triangle, point c is on the same x axis as point a, but to factor in rotation you should subtract rotation from the angle between line ac (and itself, which is, of course, 0). This leads us to:

var bx = Math.cos(angle - rotation) * ab;
   var by = Math.sin(angle - rotation) * ab;
   var cx = Math.cos(-rotation) * ac;
   var cy = Math.sin(-rotation) * ac;

The x coordinate of the centroid of a triangle is calculated by adding together the x coordinates of the vertices and dividing by three. The y coordinate is found in an analogous manner. In our drawTriangle( ) method, the coordinates of point a are always (0, 0), so it doesn't factor into the equation. We subtract the x and y inputs from the centroid coordinates to account for any user-defined offset:

var centroidX = (cx + bx)/3 - x;
   var centroidY = (cy + by)/3 - y;

Here is an example of how to use the drawTriangle( ) method. Notice that you still have to define the line style before invoking the drawTriangle( ) method.

// Draw a triangle with sides of 100 and 200 pixels and an angle of 30 degrees.
   this.createEmptyMovieClip("triangle_mc", 1);
   triangle_mc.lineStyle(1, 0x000000, 100);      // Use a one-pixel, black, solid border
   triangle_mc.drawTriangle(100, 200, 30);
 
Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip. Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)
Bouncing Ball Script - Start a new Flash movie. Create a movie clip that has a ball graphic inside it. You can name the instance of the movie clip, myClip, but our code will not depend on the name of the clip. (more...)
ActionScript: Creating Reusable Code - You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie. Create a function and then call (i.e., invoke) it by name whenever you (more...)
3D Scaling with ActionScript - Although Flash is not capable of real 3D graphics, the kind seen in popular computer games, you can create the illusion of 3D by using scaling. Scaling an object is a great way to give your (more...)
The Dot Syntax - Something else that you will be seeing a lot of as you learn ActionScript is dot syntax. Dot syntax is a way of grouping objects and functions that is used in many object-oriented programming (more...)
Movie Clip Scripts - Movie clip scripts, like button scripts, use handlers. Instead of using the on keyword that button scripts use, we will use the onClipEvent keyword. Here is an example of a movie clip script: (more...)
Decoding an RGB Value - You want to extract the red, green, and blue components from an RGB value returned by Color.getRGB( ). Use the bitshift right and bitwise AND operators. You can extract the red, gre (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.