## vectormath.csh

### vectormath.cshPrevious | Next

 ```# Define a simple set of functions for doing vector math, where the vectors # are two-dimensional and may be specified in either rectangular or polar # coordinates, in either radians or degrees. # Basic functions including adding, subtracting, multiplying, dividing # and inverting vectors of the same type plus functions for converting # between rectangular and polar coordinates and between degrees and radians. # Copyright (c) 2015 by Hamilton Laboratories. All rights reserved. proc vector( a, b ) local v @ v[0] = a * 1.0 # Force into floating point @ v[1] = b * 1.0 return v end proc torect( p ) local r @ r[0] = p[0]*cos(p[1]) @ r[1] = p[0]*sin(p[1]) return r end proc topolar( r ) local p @ p[0] = sqrt( r[0]**2 + r[1]**2 ) @ p[1] = acos( r[0]/p[0] ) if ( r[1] < 0 ) @ p[1] = -p[1] return p end proc addrect( p, q ) local r @ r[0] = p[0] + q[0] @ r[1] = p[1] + q[1] return r end proc subrect( p, q ) local r @ r[0] = p[0] - q[0] @ r[1] = p[1] - q[1] return r end proc multpolar( a, b ) local p @ p[0] = a[0] * b[0] @ p[1] = a[1] + b[1] return p end proc multpolard( a, b ) return multpolar( a, b ) end proc divpolar( a, b ) local p @ p[0] = a[0] / b[0] @ p[1] = a[1] - b[1] return p end proc divpolard( a, b ) return divpolar( a, b ) end proc polardegrees( p ) @ p[1] *= 180/pi return p end proc polarradians( p ) @ p[1] *= pi/180; return p end proc dtorect( p ) return torect( polarradians( p ) ) end proc topolard( r ) return polardegrees( topolar( r ) ) end proc multrect( p, q ) local a, b @ a = topolar( p ) @ b = topolar( q ) @ a = multpolar( a, b ) return torect( a ) end proc divrect( p, q ) local a, b @ a = topolar( p ) @ b = topolar( q ) @ a = divpolar( a, b ) return torect( a ) end proc addpolar( a, b ) local p, q @ p = torect( a ) @ q = torect( b ) @ q = addrect( p, q ) return topolar( q ) end proc subpolar( a, b ) local p, q @ p = torect( a ) @ q = torect( b ) @ q = subrect( p, q ) return topolar( q ) end proc addpolard( a, b ) local p, q @ p = dtorect( a ) @ q = dtorect( b ) @ q = addrect( p, q ) return topolard( q ) end proc subpolard( a, b ) local p, q @ p = dtorect( a ) @ q = dtorect( b ) @ q = subrect( p, q ) return topolard( q ) end proc invertpolar( p ) local r @ r[0] = 1/p[0] @ r[1] = -p[1] return r end proc invertrect( r ) return torect( invertpolar( topolar( r ) ) ) end cat << eof These functions are now defined. vector( a, b ) Create a vector from two expressions. torect( p ) Polar in radians -> Rectangular dtorect( p ) Polar in degrees -> Rectangular topolar( r ) Rectangular -> Polar in radians topolard( r ) Rectangular -> Polar in degrees addrect( p, q ) Rectangular + - * / subrect( p, q ) multrect( p, q ) divrect( p, q ) invertrect( r ) Rectangular 1 / r addpolar( a, b ) Polar + - * / subpolar( a, b ) multpolar( a, b ) divpolar( a, b ) addpolard( a, b ) subpolard( a, b ) multpolard( a, b ) divpolard( a, b ) invertpolar( p ) Polar 1 / p polardegrees( p ) Polar radians -> degrees polarradians( p ) Polar degrees -> radians eof ```