jml
jml

Reputation: 1806

AS3: Math-related - Constructing Equilateral Triangle

I am trying to construct an equilateral triangle with the following code, but I am not seeing what I would expect to.
I would expect to see a triangle drawn to the stage, but I don't see one. I think there must be something wrong in terms of the location of the tri sprite on the screen.

public class test_drawing_triangle extends Sprite
{
    public function test_drawing_triangle()
    {
        var tri:Sprite = new Sprite;    
        var nextx:int;
        var nexty:int;
        var prevx:int;
        var prevy:int;
        var angle:Number;
        var radius:Number;
        var deg:int;
        var i:int;

        radius = 10;
       // tri.x = stage.stageWidth / 2;
        tri.y = 50;

        tri.graphics.clear();
        tri.graphics.beginFill(0x000000, 0.5);
        tri.graphics.moveTo(0,0);

        for(deg = 0; deg < 360; deg += 120)
        {
            angle = deg * Math.PI / 180;
            nextx = Math.cos(angle) * radius;
            nexty = Math.sin(angle) * radius;
            tri.graphics.lineTo(nextx, nexty);
        }
        tri.graphics.endFill();
        addChild(tri);
    }
}

UPDATE:

I can now see the triangle but it is not filled in. It seems to have the generally-correct shape, but I would expect for it be 3 sided, rather than 4. If anyone could take a sec to compile this and look at what I am describing it would really help.

Upvotes: 0

Views: 971

Answers (3)

Martin Wickman
Martin Wickman

Reputation: 19925

Is there are particular reason why you are using angles to draw the triangle?

If I were to do this, I'd simply hard code a unit sized equilateral triangle (defined as three 3d points) once and then use scale and translation operations to put it in the correct size and position.

Upvotes: 0

Robert Bak
Robert Bak

Reputation: 4236

I'm not sure if I understand what you're trying to do, but if you want to have the circles at the corners of the triangle. than you need to change the

tri.graphics.drawCircle(0, 0, 2);

to

tri.graphics.drawCircle(nextx, nexty, 2);

drawCircle takes absolute x,y and doesn't care about the moveTo

EDIT - use this code in place of your loop

deg = 30;
angle = (deg * Math.PI) / 180.0;
nextx = Math.cos(angle) * radius;
nexty = Math.sin(angle) * radius;
(tri.graphics as Graphics).moveTo(nextx, nexty);
for(deg = 150; deg < 420; deg += 120)
{
 angle = (deg * Math.PI) / 180.0;
 nextx = Math.cos(angle) * radius;
 nexty = Math.sin(angle) * radius;   
 (tri.graphics as Graphics).lineTo(nextx, nexty);     
} 

Upvotes: 1

iandisme
iandisme

Reputation: 6406

This code looks like its drawing three circles centered on the points of your triangle. If that's the case, then replace

tri.graphics.moveTo(nextx, nexty);  
tri.graphics.drawCircle(0, 0, 2);

with

tri.graphics.lineTo(nextx, nexty);

If that's not the case, please specify what it is you are seeing so we might have a better idea of what's gone wrong.

Upvotes: 0

Related Questions