0

I want to draw a triangle using two angles and a side length(or a just a straight line).Can anyone give me a code for ActionScript.Also each corner there should be the angle of that corner. Thanks in advance

Sorry i cannot upload any images..

just imagine a normal triangle named ABC. i want to draw triangle by giving length of AB, and angles of A,B .for example if i give corner "a=45 degree" and "b=45 degree" (then other angle will be 90 degrees) and "AB=20 pixels" then the triangle should be drawn.

user1118321
  • 25,567
  • 4
  • 55
  • 86
user2102013
  • 29
  • 11
  • Can you please rephrase your question a bit to make it clearer ? From what I gather it's a matter of using a [trigonometric functions](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Math.html)(sin/cos/tan mainly). What is the relationship between the two angles and the side ? Can you draw a diagram ? – George Profenza Feb 23 '13 at 14:55
  • http://cnx.org/content/m31227/1.1/Picture%203.png like in this picture..if i give length of a = 20, angle of B=60 , angle of D=60 then is it possible to draw a triangle.. – user2102013 Feb 23 '13 at 15:13
  • 1
    [Law of sines](http://en.wikipedia.org/wiki/Law_of_sines): **a/sinA=b/sinB=c/sinC** ,you can get all sides length and angles. – NeaghFoz Feb 23 '13 at 15:36
  • Thanks .i know from maths , it is possible to draw them..I just dont know how to code it from actionscript..Can anyone please provide some codes . – user2102013 Feb 23 '13 at 15:40

1 Answers1

0

Fun with maths :)

The Sprite class has a graphics property which lets you draw with the Flash Graphics 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.

I drew the triangle by setting the start coordinates with the moveTo() 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 lineTo() method.

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;
        }
    }
}
Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • Can i ask one more thing ..how to rotate this triangle with specified angle.?? Thank you. – user2102013 Feb 24 '13 at 16:22
  • Take a look at [this answer](http://stackoverflow.com/a/2259502/398606) You can use that algorithm to get the new x/y values for the points defined by `(bSideEndX, bSideEndY)` and `(cSideEndX, cSideEndY)`. You are rotating those points around the starting point `(startX, startY)`. – Sunil D. Feb 24 '13 at 18:15