views:

158

answers:

3

Hi there, 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.

A: 

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.

iandisme
For right now I am not seeing _anything_.There are a couple problems... one is that the image is not drawn where i would expect it to be. The lines/dots are inconsequential; its' the coordinates being right that I am after. Basically I want the anchor (xy) to be in the middle of the triangle, pointing upward as an arrow would... I've altered my code in case that helps.
jml
OK; I have updated my code, please see above. I can see a triangle being drawn but it looks as if the triangle is not completely filled in. I took your advice and used the lineTo(nextx, nexty) but it seems as though I am not starting off in the correct position when I call beginFill(0x000000, 0.5).
jml
+1  A: 

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);     
} 
Robert Bak
OK; thanks for mentioning that.
jml
wow! totally worked! how come you need to use 'as Graphics'? I'll certainly look into it in the docs; but I haven't seen this syntax being used before.
jml
Sorry for the mess, you don't really need to, I just needed to check something in the process, and left it in. Going to sleep now, it's past midnight :)
Robert Bak
OK; thanks again. I appreciate the help.
jml
FYI, using the as keyword to typecast is less efficient when compared to something like: Graphics(tri.graphics) when you know that the object you are typecasting will actually be that type (ie: as performs checking to determine if it can do the cast before doing it, the method I mention just casts without a check).
Tegeril
A: 

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.

Martin Wickman
one reason is that I wanted to learn how to do it; another would be that now I have the points; so I _can_ hardcode it; Ultimately one might want a shape like this to morph between states or be more versatile depending on the arguments/properties. I do see your point if you'd only want a triangle. But... you can scale/translate/transform with this as well.
jml