Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Fun with maths :)</p> <p>The <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Sprite.html" rel="nofollow">Sprite</a> class has a <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Sprite.html#graphics" rel="nofollow">graphics</a> property which lets you draw with the Flash <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html" rel="nofollow">Graphics</a> API. To do basic line drawings, think of the tip of a pen. You can move the tip to an x,y coordinate, and draw lines to other coordinates. When you do that, the pen's tip now rests at the new coordinates. In Flash the coordinate plane's origin (0,0) is at the top/left.</p> <p>I drew the triangle by setting the start coordinates with the <code>moveTo()</code> method. Then calculated the end points of the two lines that would be drawn from the starting coordinates. And finally, draw the line segments w/the <code>lineTo()</code> method.</p> <pre><code>package { import flash.display.Graphics; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; public class DrawTriangle extends Sprite { private var startX:Number = 200; private var startY:Number = 200; private var angleA:Number = 10; private var angleB:Number = 90; private var lengthOfSideC:Number = 200; public function DrawTriangle() { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; var angleAInRadians:Number = angleInRaidans(angleA); var angleBInRadians:Number = angleInRaidans(angleB); var angleCInRadians:Number = angleInRaidans(180 - angleA - angleB); var lengthOfSideA:Number = Math.sin(angleAInRadians) * lengthOfSideC / Math.sin(angleCInRadians); var lengthOfSideB:Number = Math.sin(angleBInRadians) * lengthOfSideC / Math.sin(angleCInRadians); var g:Graphics = this.graphics; g.clear(); g.lineStyle(1.5, 0xFF0000, 1, true); g.moveTo(startX, startY); var bSideEndX:Number = (lengthOfSideB * Math.cos(angleBInRadians))+ startX; var bSideEndY:Number = (lengthOfSideB * Math.sin(angleBInRadians))+ startY; g.lineTo(bSideEndX, bSideEndY); g.lineStyle(1.5, 0x0000FF); var cSideEndX:Number = -(lengthOfSideC * Math.cos(angleCInRadians)) + startX; var cSideEndY:Number = (lengthOfSideC * Math.sin(angleCInRadians)) + startY; g.lineTo(cSideEndX, cSideEndY); g.lineStyle(1.5, 0); g.lineTo(startX, startY); } private function angleInRaidans(degrees:Number):Number { return degrees * Math.PI / 180; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload