A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Trigonometry - Drawing API - find rotated points x,y with radians. lineTo

  1. #1
    Senior Member
    Join Date
    Sep 2000
    Posts
    144

    Trigonometry - Drawing API - find rotated points x,y with radians. lineTo

    First I would like to say thanks to everyone for keeping this a great forum. I have been comming here for years to ask questions and post answers, and I am always happy with the time I spend here.

    PROBLEM:
    I need to rotate a drawing API object BEFORE it is drawn. Rotating the clip will not work in my situation. (I am writing source for a PDF file with the help of blazePDF component). I have tried a dozen ways with radians, cos(r*len), atan2, etc. I cannot get the trig correct. I have made a small example below, with a very basic shape. The var "r" being passes to the method needs to facilitate rotating the object. All of the lineTo(x,y) points need modified based on the radians.

    Will someone more versed in Trig lend a hand? Thanks for any help you could offer.

    --mm

    Code:
    // draw a box that is rotated 45 degrees
    var r:Number = 45*(Math.PI/180);
    drawBox(r);
    /*
     * Draw a box
     *
     * @param	r	A number in radians to rotate the object
     */
    function drawBox(r:Number){	
    	_root.createEmptyMovieClip("pdf",1);
    	pdf.lineStyle(2, 0x0000FF, 100);
    	
    	pdf.moveTo(100,100);
    	pdf.lineTo(200,100);
    	pdf.lineTo(200,200);
    	pdf.lineTo(100,200);
    	pdf.lineTo(100,100);
    
    }

  2. #2
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    Try this. You can see how to adapt it.

    code:


    MovieClip.prototype.rotLineTo = function(x, y, rot) {
    var s = Math.sin(rot);
    var c = Math.cos(rot);
    this.lineTo(x * c - y * s, x * s + y * c);
    }

    MovieClip.prototype.rotMoveTo = function(x, y, rot) {
    var s = Math.sin(rot);
    var c = Math.cos(rot);
    this.moveTo(x * c - y * s, x * s + y * c);
    }

    _root.createEmptyMovieClip("pdf",1);
    pdf.lineStyle(2, 0x0000FF, 100);
    pdf._x = 200;
    var r = 45 * Math.PI / 180;

    pdf.rotMoveTo(100, 100, r);
    pdf.rotLineTo(200, 100, r);
    pdf.rotLineTo(200, 200, r);
    pdf.rotLineTo(100, 200, r);
    pdf.rotLineTo(100, 100, r);

    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  3. #3
    Senior Member
    Join Date
    Sep 2000
    Posts
    144
    WOW,

    Thanks SO much. That is some very nice code. It does exactly what I need it to, and it is so much better then the rout i was going down.

    Thanks again! You saved me at least two more days of work.

    fyi, this is a chunk of code that draws one of my objects with your code implemented. (radians is set higher up as a class variable, and drawPolygon is a generic drawing method all my shapes use)
    Code:
    function drawRightArrow(mc:MovieClip, orgn:Array, fill:Object):Void {
    	var x:Number = orgn[0];
    	var y:Number = orgn[1];
    	var s:Number = Math.sin(radians);
    	var c:Number = Math.cos(radians);
    	// FORMAT(x * c - y * s, x * s + y * c);
    	// pointsArray ~ pntsArr ~ pA 
    	var pA:Array = [[0, 4.35], [0, -4.35], [7.5, 0.03], [0, 4.35]];
    	drawPolygon(mc, [(pA[0][0]*c-pA[0][1]*s)+x, (pA[0][0]*s+pA[0][1]*c)+y], 
    			[
    			[(pA[1][0]*c-pA[1][1]*s)+x, (pA[1][0]*s+pA[1][1]*c)+y], 
    			[(pA[2][0]*c-pA[2][1]*s)+x, (pA[2][0]*s+pA[2][1]*c)+y], 
    			[(pA[3][0]*c-pA[3][1]*s)+x, (pA[3][0]*s+pA[3][1]*c)+y]
    			], 
    			[null], 
    			fill);
    }
    --mm

  4. #4
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    I'm glad that helped. You might still want to make another function, drawPolygonWithRotation(), if you have to do this kind of thing repeatedly. Then you could just feed that function your array of points and value of 'radians,' and not have to manually do the rotation each time you draw a polygon.
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

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