A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: expert trig question

  1. #1
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472

    expert trig question

    Hi All,

    First - see the attached file!

    Based on 10 values i need to draw a polygon.
    In the code below I am able to draw a polygon but i need to dynamicly use the values in the array 'arrValues'

    PHP Code:
    ArrValues=[1,10,7,4,9,1,8,2,6,4];
    point 0;
    noOfPoints 10;
    steps = (2*Math.PI)/noOfPoints;

    _root.createEmptyMovieClip ("polygon"1);
      
    with (_root.polygon){
        
    beginFill (0x0000FF50);
        
    lineStyle (50xFF00FF100);
        
    moveTo (100*Math.cos(point), 100*Math.sin(point));
        for (
    i=0i<noOfPointsi++) {
            
    point += steps;
            
    pointX 100*Math.cos(point);
            
    pointY 100*Math.sin(point);
            
    lineTo (pointXpointY);
        }
        
    endFill();

    Any help is rewarded with good good karma
    //podenphant
    Attached Images Attached Images

  2. #2
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    code:

    function drawPolygon(MC, MCdepth, points, radius, fillcol, fillalpha, linethick, linecol, linealpha) {
    MC.createEmptyMovieClip("polygon", MCdepth);
    var point = 0;
    var steps = (2*Math.PI)/points.length;
    var p = MC.polygon;
    p.beginFill(fillcol, fillalpha);
    p.lineStyle(linethick, linecol, linealpha);
    p.moveTo(radius*Math.cos(point), radius*Math.sin(point));
    for (var i=0; i<points.length; i++) {
    point += steps;
    pointX = points[i]*radius*Math.cos(point);
    pointY = points[i]*radius*Math.sin(point);
    p.lineTo(pointX, pointY);
    }
    p.endFill();
    return p;
    }
    // Everything from here on down is just a demonstration of it working
    ArrValues = new Array();
    for (var i = 0; i<15; i++) {
    ArrValues.push(Math.random()*15);
    }
    createEmptyMovieClip("bob", 10);
    do {
    var radius = 1+Math.random()*10;
    var p = drawPolygon(bob, ArrValues.length+1, ArrValues, radius, 0x00FFFF, 50, 1, 0xFF00FF, 100);
    p._x = Math.random()*Stage.width;
    p._y = Math.random()*Stage.height;
    ArrValues.pop();
    } while (arrValues.length>2);


  3. #3
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472
    Hi AlsoGaiusCoffey,

    thanks for your response, nice coding
    I changed 'p.moveTo' to make it start from arrValues[0]

    How do I make the first point start at 12 o'clock (If you know what I mean?)

    Regards
    //poden

    PHP Code:
    function drawPolygon(MCMCdepthpointsradiusfillcolfillalphalinethicklinecollinealpha) {
            
    MC.createEmptyMovieClip("polygon"MCdepth);
            var 
    point 0;
            var 
    steps = (2*Math.PI)/points.length;
            var 
    MC.polygon;
            
    p.beginFill(fillcolfillalpha);
            
    p.lineStyle(linethicklinecollinealpha);
            
    p.moveTo(points[0]*radius*Math.cos(point), points[0]*radius*Math.sin(point)); 
            for (var 
    i=1i<points.lengthi++) {
                    
    point += steps;
                    
    pointX points[i]*radius*Math.cos(point);
                    
    pointY points[i]*radius*Math.sin(point);
                    
    p.lineTo(pointXpointY);
            }
            
    p.endFill();
            return 
    p;
    }

    ArrValues = new Array();
    ArrValues=[10,9,8,7,6,5,4,3,2,0]
    createEmptyMovieClip("bob"10);
    var 
    radius 10;
    var 
    drawPolygon(bobArrValues.length+1ArrValuesradius0x00FFFF5010xFF00FF100);
    p._x 0
    p
    ._y 

  4. #4
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    The point variable is your start and there are 2*PI radians in a circle, so point = Math.PI * 1.5 is about right for 12 o'clock.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center