Converts Polar coordinates to Cartesian Coordinates, and back

From Director Online Wiki
Jump to: navigation, search

function to convert values between cartesian coordinate (x,y) system and a polar coordinate system (distance, angle)

---------------------------------------------
--Returns Polar coôrdinates to cartesian
--_d = distance from center point(0,0)
--_r = angle from center (radians)
---------------------------------------------
on PolarToCartesian _d, _r
  if ilk(_d) = #list then
    _a = _d[1]
    _b = _d[2]
  else
    _a = _d
    _b = _r
  end if
  _x = _a*sin(_b)
  _y = _a*cos(_b)
  return point(_x,_y)
end
 
---------------------------------------------
--Returns Cartesian coôrds to Polar Coörds
--x,y are coordinates in a field with center point(0,0)
---------------------------------------------
on CartesianToPolar _x, _y
  if ilk(_x) = #point then
    _a = _x[1]
    _b = _x[2]
  else
    _a=_x
    _b=_y
  end if
  _distance = Distance(point(0,0),point(_a,_b))
  _angle = AngleVector(point(0,0),point(_a,_b))
  return[_distance,_angle]
end
 
--DEPENDING FUNCTIONS FOR THE ABOVE
on AngleVector pointA, pointB
  _vector = pointb-pointA
  _ang = atan(_vector[1],_vector[2])
  if _ang < 0 then _ang = _ang+(2*PI)
  return _ang
end
 
on Distance  pt1, pt2
  a = pt1[1]-pt2[1]
  b = pt1[2]-pt2[2]
  return sqrt(float((a*a)+(b*b)))
end