csconv.lua (1297B)
1 2 --[[ 3 Coordinate Systems Conversion 4 "csconv.lua" 5 M. Yamanaka 6 email: myamanaka@live.com 7 website: csmyamanaka.com 8 license: MIT (See included "LICENSE" file for details) 9 ]] 10 11 --[[ 12 a series of conversions between coordinate systems 13 canvXY ... canvas to XY coordinate system 14 arguments: 15 p ... point in canvas space 16 s ... scale factor 17 t ... translation 18 xyCanv ... XY to canvas coordinate system 19 arguments: 20 p ... point in XY coordinate system 21 s ... scale factor 22 t ... translatiion 23 polarXY ... polar to XY coordinate system 24 arguments: 25 p ... point in polar coordinate system {r, theta} 26 xyPolar ... XY to polar coordinate system 27 arguments: 28 p ... point in XY coordinate system 29 ]] 30 function canvXY(p, s, t) return {(p[1] - t[1])/s, (p[2] - t[2])/s} end 31 function xyCanv(p, s, t) return {s*p[1] + t[1], s*p[2] + t[2]} end 32 function polarXY(p) return {p[1]*math.cos(p[2]), p[1]*math.sin(p[2])} end 33 function xyPolar(p) 34 local th = 0 35 if p[1] == 0 and p[2] >= 0 then th = math.pi/2 36 elseif p[1] == 0 and p[2] < 0 then th = 3*math.pi/2 37 elseif p[1] >=0 and p[2] == 0 then th = 0 38 elseif p[1] < 0 and p[2] == 0 then th = math.pi 39 else th = math.atan(p[2]/p[1]) 40 end 41 return {math.sqrt(p[1]*p[1] + p[2]*p[2]), th} 42 end 43