sdi_types.lua

Lua source files to HTML converted04/22/03 19:13:13

   1  -- SDI_HellCreator
   2  -- Salomatine Dmitry.
   3  -- sdihellcreator@bezeqint.net
   4  -- Israel
   5  -- original sdi_types.lua
   6  -- revision 13.04.2003
   7  -- start app types definition
   8  require("sdi_class");
   9  -- In this file defined Point2d,Vector,Rect,Matrix4x4
  10  -- useful utills
  11  function isNum(x)
  12      if type(x)=="number" then
  13         return 1;
  14      end
  15      return 0;
  16  end
  17  function isNum2(x,y)
  18      if isNum(x) and isNum(y) then
  19         return 1;
  20      end
  21      return 0;
  22  end
  23  function isNum3(x,y,z)
  24      if isNum(x) and isNum(y) and isNum(z)then
  25         return 1;
  26      end
  27      return 0;
  28  end
  29  -- returning the smal first the big second
  30  function swapbigger(a,b)
  31   if a<b then
  32      return a,b;
  33   end
  34   return b,a;
  35  end
  36  --Point2d actualy have only x and y coordinate
  37  --used in stuff like rect,window,button e.t.c.
  38  Point2d={};
  39  local Point2d_mt = Class(Point2d);
  40  -- via new function must provide arguments
  41  function Point2d:new(tx,ty)
  42      if isNum2(tx,ty) then
  43      return setmetatable({x=tx,y=ty}, Point2d_mt);
  44      end
  45      return setmetatable({x=0,y=0}, Point2d_mt);
  46  end
  47  --default constructor for object via new
  48  function Point2d:Point2d()
  49      return Point2d:new(0,0);--default
  50  end
  51  --set
  52  function Point2d:Set(tx,ty)
  53      if isNum2(tx,ty) then
  54      self.x=tx;
  55      self.y=ty;
  56      end
  57  end
  58  --get
  59  function Point2d:Get()
  60      return self.x,self.y;
  61  end
  62  --Vector have x,y and z coordinate and also more posible operations
  63  Vector={};
  64  local Vector_mt = Class(Vector);
  65  -- via new function must provide arguments
  66  function Vector:new(tx,ty,tz)
  67      if isNum3(tx,ty,tz) then
  68         return setmetatable({x=tx,y=ty,z=tz}, Vector_mt);
  69      end
  70      return setmetatable({x=0,y=0,z=0}, Vector_mt);
  71  end
  72  --default constructor for object via new
  73  function Vector:Vector()
  74      return Vector:new(0,0,0);--default
  75  end
  76  --set
  77  function Vector:Set(tx,ty,tz)
  78      if isNum3(tx,ty,tz) then
  79      self.x=tx;
  80      self.y=ty;
  81      self.z=tz;
  82      end
  83  end
  84  --get
  85  function Vector:Get()
  86      return self.x,self.y,self.z;
  87  end
  88  --move to by ofset
  89  function Vector:Move(tx,ty,tz)
  90      if isNum3(tx,ty,tz) then
  91         v={x=tx,y=ty,z=tz};
  92         self:Add(v);
  93      end
  94  end
  95  --add val to by ofset
  96  function Vector:Add(v)
  97      if isNum3(v.x,v.y,v.z) then
  98      self.x=self.x+v.x;
  99      self.y=self.y+v.y;
 100      self.z=self.z+v.z;
 101      end
 102  end
 103  --scale by factor
 104  function Vector:Scale(tx,ty,tz)
 105      if isNum3(tx,ty,tz) then
 106      self.x=self.x*tx;
 107      self.y=self.y*ty;
 108      self.z=self.z*tz;
 109      end
 110  end
 111  --normalize
 112  function Vector:Mag()
 113       return math.sqrt(self:Dot(self))
 114  end 
 115  --dot
 116  function Vector:Dot(v)
 117      return self.x * v.x + self.y * v.y
 118  end
 119  ----normalize later:)
 120  
 121  
 122  Rect = {};
 123  local Rect_mt = Class(Rect);
 124  -- via new function must provide arguments
 125  -- it can be doing by two points actualy will later
 126  function Rect:new(tx,ty,tdx,tdy)
 127      if isNum2(tx,ty) and isNum2(tdx,tdy) then
 128         return setmetatable({x=tx,y=ty,dx=tdx,dy=tdy}, Rect_mt);
 129      end
 130      return setmetatable({x=0,y=0,dx=0,dy=0}, Rect_mt);
 131  end
 132  --default constructor for object via new
 133  function Rect:Rect()
 134      return Rect:new(0,0,1,1);--default size iz 1x1
 135  end
 136  -- set/get position
 137  function Rect:Pos(sg,tx,ty)
 138      if sg==1 then -- set
 139         if isNum2(tx,ty) then
 140            self.x=tx;
 141            self.y=ty;
 142         end
 143      else -- get
 144         return self.x,self.y;
 145      end
 146  end
 147  -- set/get size delta
 148  function Rect:Size(sg,tx,ty)
 149      if sg==1 then --set
 150         if isNum2(tx,ty) then
 151            self.dx=tx;
 152            self.dy=ty;
 153         end
 154      else -- get
 155         return self.dx,self.dy;
 156      end
 157  end
 158  -- move only start point by ofset
 159  function Rect:Move(tx,ty)
 160         if isNum2(tx,ty) then
 161            self.x=self.x+tx;
 162            self.y=self.y+ty;
 163         end
 164  end
 165  -- get real coordinates xleft,ytop,xright,ybotom
 166  function Rect:Real()
 167      return self.x,self.y,self.x+self.dx,self.y+self.dy;
 168  end
 169  --  check if point in rect
 170  function Rect:InRect(tx,ty)
 171      if isNum2(tx,ty) then
 172         local xl,yt,xr,yb=self:Real();
 173         xl,xr=swapbigger(xl,xr);
 174         yt,yb=swapbigger(yt,yb);
 175         if (tx>=xl and tx<=xr)and(ty>=yt and ty<=yb) then 
 176            return 1;
 177         end
 178      end
 179      return 0;
 180  end
 181  -- prints all information about rect
 182  function Rect:Pinfo()
 183      print("Position is",self:Pos());
 184      print("Size is",self:Size());
 185  end