Drawing a Triangle using ActionScript

written by: Elis Frugalo; article published: year 2007, month 01;

In: Root » Computers and technology » Flash

  Share  
|
  PL  |  NL  |  FR  |  ES  |  PT  |  IT  |  DE  |  DK  |  NO  |  SE  |  FI  |  GR  |  JP  |  CN  |  KR  |  RU  |  AE


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);
 

Share

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us or use the "Report this article" button on this page to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.