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
Transforming the Current Color of a Flash Movie Clip - You want to modify a movie clip's color relative to the current color transformation, instead of relative to the author-time color values. Use the getTransform( ) and setTransform( ) method (more...)
ActionScript: Repeating an Operation Many Times - You want to perform a task multiple times within a single frame. Use a loop to perform the same task multiple times within a single frame. For example, you can use a for statement: (more...)
Avoiding Conflicting Variables in ActionScript - You want to make sure that variables within a function do not interfere with variables in other functions or within the timeline in which the function is defined. Use the var keyword to declare loc (more...)
How to Exit a Function in ActionScript - You want to exit a function. Functions terminate automatically after the last statement within the function executes. Use a return statement to exit a function before reaching its end. The (more...)
Move Clip Rotation - A property like _x and _y is the movie clip property _rotation. The _rotation property accepts a value in degrees. A circle is divided into 360 degrees. The values used by _rotation range f (more...)
Targeting Flash Movie Clips - The simplest way to target a movie clip is to use its name, followed by a dot, followed by the command you want to send. However, there are plenty of other ways to target a movie clip as we (more...)
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...)

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