arith.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. Point
  13. Pt(int x, int y)
  14. {
  15. Point p;
  16. p.x = x;
  17. p.y = y;
  18. return p;
  19. }
  20. Rectangle
  21. Rect(int x, int y, int bx, int by)
  22. {
  23. Rectangle r;
  24. r.min.x = x;
  25. r.min.y = y;
  26. r.max.x = bx;
  27. r.max.y = by;
  28. return r;
  29. }
  30. Rectangle
  31. Rpt(Point min, Point max)
  32. {
  33. Rectangle r;
  34. r.min = min;
  35. r.max = max;
  36. return r;
  37. }
  38. Point
  39. addpt(Point a, Point b)
  40. {
  41. a.x += b.x;
  42. a.y += b.y;
  43. return a;
  44. }
  45. Point
  46. subpt(Point a, Point b)
  47. {
  48. a.x -= b.x;
  49. a.y -= b.y;
  50. return a;
  51. }
  52. Rectangle
  53. insetrect(Rectangle r, int n)
  54. {
  55. r.min.x += n;
  56. r.min.y += n;
  57. r.max.x -= n;
  58. r.max.y -= n;
  59. return r;
  60. }
  61. Point
  62. divpt(Point a, int b)
  63. {
  64. a.x /= b;
  65. a.y /= b;
  66. return a;
  67. }
  68. Point
  69. mulpt(Point a, int b)
  70. {
  71. a.x *= b;
  72. a.y *= b;
  73. return a;
  74. }
  75. Rectangle
  76. rectsubpt(Rectangle r, Point p)
  77. {
  78. r.min.x -= p.x;
  79. r.min.y -= p.y;
  80. r.max.x -= p.x;
  81. r.max.y -= p.y;
  82. return r;
  83. }
  84. Rectangle
  85. rectaddpt(Rectangle r, Point p)
  86. {
  87. r.min.x += p.x;
  88. r.min.y += p.y;
  89. r.max.x += p.x;
  90. r.max.y += p.y;
  91. return r;
  92. }
  93. int
  94. eqpt(Point p, Point q)
  95. {
  96. return p.x==q.x && p.y==q.y;
  97. }
  98. int
  99. eqrect(Rectangle r, Rectangle s)
  100. {
  101. return r.min.x==s.min.x && r.max.x==s.max.x &&
  102. r.min.y==s.min.y && r.max.y==s.max.y;
  103. }
  104. int
  105. rectXrect(Rectangle r, Rectangle s)
  106. {
  107. return r.min.x<s.max.x && s.min.x<r.max.x &&
  108. r.min.y<s.max.y && s.min.y<r.max.y;
  109. }
  110. int
  111. rectinrect(Rectangle r, Rectangle s)
  112. {
  113. return s.min.x<=r.min.x && r.max.x<=s.max.x && s.min.y<=r.min.y && r.max.y<=s.max.y;
  114. }
  115. int
  116. ptinrect(Point p, Rectangle r)
  117. {
  118. return p.x>=r.min.x && p.x<r.max.x &&
  119. p.y>=r.min.y && p.y<r.max.y;
  120. }
  121. Rectangle
  122. canonrect(Rectangle r)
  123. {
  124. int t;
  125. if (r.max.x < r.min.x) {
  126. t = r.min.x;
  127. r.min.x = r.max.x;
  128. r.max.x = t;
  129. }
  130. if (r.max.y < r.min.y) {
  131. t = r.min.y;
  132. r.min.y = r.max.y;
  133. r.max.y = t;
  134. }
  135. return r;
  136. }
  137. void
  138. combinerect(Rectangle *r1, Rectangle r2)
  139. {
  140. if(r1->min.x > r2.min.x)
  141. r1->min.x = r2.min.x;
  142. if(r1->min.y > r2.min.y)
  143. r1->min.y = r2.min.y;
  144. if(r1->max.x < r2.max.x)
  145. r1->max.x = r2.max.x;
  146. if(r1->max.y < r2.max.y)
  147. r1->max.y = r2.max.y;
  148. }
  149. uint32_t drawld2chan[] = {
  150. GREY1,
  151. GREY2,
  152. GREY4,
  153. CMAP8,
  154. };
  155. uint32_t
  156. setalpha(uint32_t color, uint8_t alpha)
  157. {
  158. int red, green, blue;
  159. red = (color >> 3*8) & 0xFF;
  160. green = (color >> 2*8) & 0xFF;
  161. blue = (color >> 1*8) & 0xFF;
  162. /* ignore incoming alpha */
  163. red = (red * alpha)/255;
  164. green = (green * alpha)/255;
  165. blue = (blue * alpha)/255;
  166. return (red<<3*8) | (green<<2*8) | (blue<<1*8) | (alpha<<0*8);
  167. }
  168. Point ZP;
  169. Rectangle ZR;