home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / POINT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  3.8 KB  |  206 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of mathematical TPoint, TSize, TRect classes.
  6. //----------------------------------------------------------------------------
  7. #pragma hdrignore SECTION
  8. #include <owl/owlpch.h>
  9. #include <owl/point.h>
  10.  
  11. #if !defined(SECTION) || SECTION == 1
  12.  
  13. //
  14. // Calculate the integer square root of a 32bit signed long. return a 16bit
  15. // signed. Is fairly fast, esp. compared to FP versions
  16. //
  17. int _OWLFUNC
  18. Sqrt(long val)
  19. {
  20.   if (val <= 0)
  21.     return 0;     // Throw a math exception?
  22.  
  23.   unsigned mask = 1;  // Bit mask to shift left
  24.   int best = 0;       // Best estimate so far
  25.  
  26.   for (; !(mask&0x8000); mask <<= 1)
  27.     if (((long)best+mask)*(best+mask) <= val)
  28.       best |= mask;
  29.   return best;
  30. }
  31.  
  32. //
  33. // make a duplicate of a C string using new
  34. //
  35. char* _OWLFUNC
  36. strnewdup(const char* s)
  37. {
  38.   if (!s)
  39.     s = "";
  40.   return strcpy(new char[strlen(s)+1], s);
  41. }
  42.  
  43. //
  44. // make a far duplicate of a C string using new far
  45. //
  46. #if defined(BI_DATA_NEAR)
  47. char far*  _OWLFUNC
  48. strnewdup(const char far* s)
  49. {
  50.   if (!s)
  51.     s = "";
  52.   return strcpy(new far char[strlen(s)+1], s);
  53. }
  54.  
  55. long
  56. atol(const char far* s)
  57. {
  58.   for (long val = 0; *s && isdigit(*s); s++)
  59.     val = val*10 + *s - '0';
  60.   return val;
  61. }
  62.  
  63. #endif
  64.  
  65.  
  66. TRect&
  67. TRect::Normalize()
  68. {
  69.   if (left > right)
  70.     Swap(left, right);
  71.   if (top > bottom)
  72.     Swap(top, bottom);
  73.   return *this;
  74. }
  75.  
  76. TRect&
  77. TRect::Offset(int dx, int dy)
  78. {
  79.   left += dx;
  80.   top += dy;
  81.   right += dx;
  82.   bottom += dy;
  83.   return *this;
  84. }
  85.  
  86. TRect&
  87. TRect::Inflate(int dx, int dy)
  88. {
  89.   left -= dx;
  90.   top -= dy;
  91.   right += dx;
  92.   bottom += dy;
  93.   return *this;
  94. }
  95.  
  96. TRect&
  97. TRect::operator &=(const TRect& other)
  98. {
  99.   if (!IsNull()) {
  100.     if (other.IsNull())
  101.       SetNull();
  102.     else {
  103.       left = Max(left, other.left);
  104.       top = Max(top, other.top);
  105.       right = Min(right, other.right);
  106.       bottom = Min(bottom, other.bottom);
  107.     }
  108.   }
  109.   return *this;
  110. }
  111.  
  112. TRect&
  113. TRect::operator |=(const TRect& other)
  114. {
  115.   if (!other.IsNull()) {
  116.     if (IsNull())
  117.       *this = other;
  118.     else {
  119.       left = Min(left, other.left);
  120.       top = Min(top, other.top);
  121.       right = Max(right, other.right);
  122.       bottom = Max(bottom, other.bottom);
  123.     }
  124.   }
  125.   return *this;
  126. }
  127.  
  128. #endif
  129. #if !defined(SECTION) || SECTION == 2
  130.  
  131. ostream& _OWLFUNC
  132. operator <<(ostream& os, const TRect& r)
  133. {
  134.   return os << '(' << r.left << ',' << r.top << '-'
  135.             << r.right << ',' << r.bottom << ')';
  136. }
  137.  
  138. //
  139. // streaming operators for resource Ids
  140. //
  141. ostream& _OWLFUNC
  142. operator <<(ostream& os, const TResId& id)
  143. {
  144.   bool  isNumeric = !id.IsString();
  145.  
  146.   if (isNumeric)
  147.     os << (long)id.Id;
  148.  
  149.   else
  150.     #if defined(__SMALL__) || defined(__MEDIUM__)
  151.       os << string(id.Id);
  152.     #else
  153.       os << id.Id;
  154.     #endif
  155.   return os;
  156. }
  157.  
  158. #endif
  159. #if !defined(SECTION) || SECTION == 3
  160.  
  161. ipstream& _OWLFUNC
  162. operator >>(ipstream& is, TRect& r)
  163. {
  164.   return is >> r.left >> r.top >> r.right >> r.bottom;
  165. }
  166.  
  167. opstream& _OWLFUNC
  168. operator <<(opstream& os, const TRect& r)
  169. {
  170.   return os << r.left << r.top << r.right << r.bottom;
  171. }
  172.  
  173. //
  174. // streaming operators for resource Ids
  175. //
  176. ipstream& _OWLFUNC
  177. operator >>(ipstream& is, TResId& id)
  178. {
  179.   bool  isNumeric;
  180.   is >> isNumeric;
  181.  
  182.   if (isNumeric) {
  183.     long  nid;
  184.     is >> nid;
  185.     id = int(nid);
  186.   }
  187.   else
  188.     id = is.freadString();
  189.   return is;
  190. }
  191.  
  192. opstream& _OWLFUNC
  193. operator <<(opstream& os, const TResId& id)
  194. {
  195.   bool  isNumeric = !id.IsString();
  196.   os << isNumeric;
  197.  
  198.   if (isNumeric)
  199.     os << (long)id.Id;
  200.  
  201.   else
  202.     os.fwriteString(id.Id);
  203.   return os;
  204. }
  205. #endif
  206.