home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / liblayer / src / xp_rect.c < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.7 KB  |  139 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.  *  xp_rect.c - Simple rectangle operations
  20.  */
  21.  
  22.  
  23. #include "xp.h"
  24. #include "xp_rect.h"
  25.  
  26. #ifndef MIN
  27. #define MIN(x, y)    (((x) < (y)) ? (x) : (y))
  28. #endif
  29.  
  30. #ifndef MAX
  31. #define MAX(x, y)    (((x) > (y)) ? (x) : (y))
  32. #endif
  33.  
  34. void
  35. XP_CopyRect(XP_Rect *src, XP_Rect *dest)
  36. {
  37.     XP_MEMCPY(dest, src, sizeof (XP_Rect));
  38. }
  39.  
  40. PRBool
  41. XP_EqualRect(XP_Rect *r1, XP_Rect *r2)
  42. {
  43.   return ((r1->left == r2->left) && (r1->top == r2->top) &&
  44.       (r1->bottom == r2->bottom) && (r1->right == r2->right));
  45. }
  46.  
  47. /* Offset rectangle in 2D space. This could be a macro */
  48. void
  49. XP_OffsetRect(XP_Rect *rect, int32 x_offset, int32 y_offset)
  50. {
  51.     XP_ASSERT(rect);
  52.     
  53.     rect->left += x_offset;
  54.     rect->top += y_offset;
  55.     rect->right += x_offset;
  56.     rect->bottom += y_offset;
  57. }
  58.  
  59. /* TRUE if point is within the boundary of the rect */
  60. PRBool
  61. XP_PointInRect(XP_Rect *rect, int32 x, int32 y)
  62. {
  63.     XP_ASSERT(rect);
  64.     
  65.     return (PRBool)((rect->left <= x) &&
  66.             (rect->top <= y) &&
  67.             (rect->right > x) &&
  68.             (rect->bottom > y));
  69. }
  70.  
  71. /* TRUE if any parts of the two rectangles overlap */
  72. PRBool
  73. XP_RectsOverlap(XP_Rect *rect1, XP_Rect *rect2)
  74. {
  75.     XP_ASSERT(rect1);
  76.     XP_ASSERT(rect2);
  77.     
  78.     return (PRBool)((rect1->right > rect2->left) &&
  79.             (rect1->left < rect2->right) &&
  80.             (rect1->bottom > rect2->top) &&
  81.             (rect1->top < rect2->bottom));
  82. }
  83.  
  84. /* TRUE if rect2 is entirely contained within rect1 */
  85. PRBool
  86. XP_RectContainsRect(XP_Rect *rect1, XP_Rect *rect2)
  87. {
  88.     XP_ASSERT(rect1);
  89.     XP_ASSERT(rect2);
  90.     
  91.     return (PRBool)((rect1->right >= rect2->right) &&
  92.             (rect1->left <= rect2->left) &&
  93.             (rect1->bottom >= rect2->bottom) &&
  94.             (rect1->top <= rect2->top));
  95. }
  96.  
  97. /* dst = src1 intersect src2. dst can be one of src1 or src2 */
  98. void
  99. XP_IntersectRect(XP_Rect *src1, XP_Rect *src2, XP_Rect *dst)
  100. {
  101.     XP_ASSERT(src1);
  102.     XP_ASSERT(src2);
  103.     XP_ASSERT(dst);
  104.     
  105.     if (!XP_RectsOverlap(src1, src2)) {
  106.         dst->left = dst->top = dst->right = dst->bottom = 0;
  107.     }
  108.     else {
  109.         dst->left = MAX(src1->left, src2->left);
  110.         dst->top = MAX(src1->top, src2->top);
  111.         dst->right = MIN(src1->right, src2->right);
  112.         dst->bottom = MIN(src1->bottom, src2->bottom);
  113.     }
  114. }
  115.  
  116. /* dst = bounding box of src1 and src2. dst can be one of src1 or src2 */
  117. void
  118. XP_RectsBbox(XP_Rect *src1, XP_Rect *src2, XP_Rect *dst)
  119. {
  120.     XP_ASSERT(src1);
  121.     XP_ASSERT(src2);
  122.     XP_ASSERT(dst);
  123.     
  124.     dst->left = MIN(src1->left, src2->left);
  125.     dst->top = MIN(src1->top, src2->top);
  126.     dst->right = MAX(src1->right, src2->right);
  127.     dst->bottom = MAX(src1->bottom, src2->bottom);
  128. }
  129.  
  130. /* Does the rectangle enclose any pixels? */
  131. PRBool
  132. XP_IsEmptyRect(XP_Rect *rect)
  133. {
  134.     XP_ASSERT(rect);
  135.     
  136.     return (PRBool)((rect->left == rect->right) ||
  137.                     (rect->top == rect->bottom));
  138. }
  139.