AS3 Cartesian Point / Polar Point

Share your knowledge in a tutorial, or post code snippets and classes that might be useful to the community

AS3 Cartesian Point / Polar Point

Postby tox » Sat May 22, 2010 1:42 am

Classes for a cartesian / polar point. Have fun!

Class for a cartesian point
Code: Select all
package de.webcodesign.math.cartesian
{
   import de.webcodesign.math.polar.PolarPoint;
   import flash.geom.Point;
   
   /**
    * A cartesian point that can be returned with polar coordinates.
    * @author Tox, webcodesign.de
    */
   public class CartesianPoint extends Point
   {
      public function CartesianPoint(x:Number=0, y:Number=0) { super(x, y); }
      public function toPolar():PolarPoint { return new PolarPoint(Math.sqrt(y*y+x*x), Math.atan2(y, x)); }
   }
   
}


Class for a polar point
Code: Select all
package de.webcodesign.math.polar
{
   import de.webcodesign.math.cartesian.CartesianPoint;
   
   /**
    * A polar point that can be returned with cartesian coordinates.
    * @author Tox, webcodesign.de
    */
   public class PolarPoint
   {
      public var r:Number = 0;
      public var theta:Number = 0;
      public function PolarPoint(r:Number = 0, theta:Number = 0) { this.r = r; this.theta = theta; }
      public function shift(r0:Number, theta0:Number):void
      {
         var r1:Number = Math.sqrt(r * r + r0 * r0 - 2 * r * r0 * Math.cos(theta - theta0));
         var theta1:Number = Math.atan2((r * Math.sin(theta) - r0 * Math.sin(theta0)), (r * Math.cos(theta) - r0 * Math.cos(theta0)));

         r = r1;
         theta = theta1;
      }
      public function rotate(theta0:Number):void
      {
         var r1:Number = r;
         var theta1:Number = theta - theta0;
         r = r1;
         theta = theta1;
      }
      public function toCartesian():CartesianPoint { return new CartesianPoint(r * Math.cos(theta), r * Math.sin(theta)); }
   }
   
}
greetz,

tox, webcodesign.de
User avatar
tox
 
Posts: 111
Joined: Sun Jul 30, 2006 10:53 pm
Location: Leeuwarden, Netherlands
Flash Version: Macromedia Flash 5
Authoring Environment: Microsoft Windows

Return to Shared User Content

Who is online

Users browsing this forum: No registered users and 0 guests