rect.monkey2 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. Namespace std.geom
  2. #rem monkeydoc Convenience type alias for Rect\<Int\>.
  3. #end
  4. Alias Recti:Rect<Int>
  5. #rem monkeydoc Convenience type alias for Rect\<Int\>.
  6. #end
  7. Alias Rectf:Rect<Float>
  8. #rem monkeydoc The Rect class provides support for manipulating rectangular regions.
  9. #end
  10. Struct Rect<T>
  11. #rem monkeydoc Minimum rect coordinates.
  12. #end
  13. Field min:Vec2<T>
  14. #rem monkeydoc Maximum rect coordinates.
  15. #end
  16. Field max:Vec2<T>
  17. #rem monkeydoic Creates a new Rect.
  18. #end
  19. Method New()
  20. End
  21. Method New( min:Vec2<T>,max:Vec2<T> )
  22. Self.min=min
  23. Self.max=max
  24. End
  25. Method New( x0:T,y0:T,x1:T,y1:T )
  26. min=New Vec2<T>( x0,y0 )
  27. max=New Vec2<T>( x1,y1 )
  28. End
  29. Method New( x0:T,y0:T,max:Vec2<T> )
  30. Self.min=New Vec2<T>( x0,y0 )
  31. Self.max=max
  32. End
  33. Method New( min:Vec2<T>,x1:T,y1:T )
  34. Self.min=min
  35. Self.max=New Vec2<T>( x1,y1 )
  36. End
  37. #rem monkeydoc Converts the rect to a rect of a different type
  38. #end
  39. Operator To<C>:Rect<C>()
  40. Return New Rect<C>( min.x,min.y,max.x,max.y )
  41. End
  42. #rem monkeydoc Converts the rect to a printable string.
  43. #end
  44. Operator To:String()
  45. Return "Rect("+min.x+","+min.y+","+max.x+","+max.y+")"
  46. End
  47. #rem monkeydoc The minimum X coordinate.
  48. #end
  49. Property X:T()
  50. Return min.x
  51. Setter( x:T )
  52. min.x=x
  53. End
  54. #rem monkeydoc The minimum Y coordinate.
  55. #end
  56. Property Y:T()
  57. Return min.y
  58. Setter( y:T )
  59. min.y=y
  60. End
  61. #rem monkeydoc The width of the rect.
  62. ' Writing to this property modifies the maximum Y coordinate only.
  63. #end
  64. Property Width:T()
  65. Return max.x-min.x
  66. ' Setter( width:T )
  67. ' max.x=min.x+width
  68. End
  69. #rem monkeydoc The height of the rect.
  70. ' Writing to this property modifies the maximum Y coordinate only.
  71. #end
  72. Property Height:T()
  73. Return max.y-min.y
  74. ' Setter( height:T )
  75. ' max.y=min.y+height
  76. End
  77. #rem monkeydoc The minimum X coordinate.
  78. #end
  79. Property Left:T()
  80. Return min.x
  81. Setter( left:T )
  82. min.x=left
  83. End
  84. #rem monkeydoc The minimum Y coordinate.
  85. #end
  86. Property Top:T()
  87. Return min.y
  88. Setter( top:T )
  89. min.y=top
  90. End
  91. #rem monkeydoc The maximum X coordinate.
  92. #end
  93. Property Right:T()
  94. Return max.x
  95. Setter( right:T )
  96. max.x=right
  97. End
  98. #rem monkeydoc The maximum X coordinate.
  99. #end
  100. Property Bottom:T()
  101. Return max.y
  102. Setter( bottom:T )
  103. max.y=bottom
  104. End
  105. #rem monkeydoc The top-left of the rect.
  106. #end
  107. Property Origin:Vec2<T>()
  108. Return min
  109. Setter( origin:Vec2<T> )
  110. min=origin
  111. End
  112. #rem monkeydoc The width and height of the rect.
  113. #end
  114. Property Size:Vec2<T>()
  115. Return max-min
  116. Setter( size:Vec2<T> )
  117. max=min+size
  118. End
  119. #rem monkeydoc The center of the rect.
  120. #end
  121. Property Center:Vec2<T>()
  122. Return (min+max)/2
  123. ' Setter( center:Vec2<T> )
  124. End
  125. #rem monkeydoc The top-left of the rect.
  126. #end
  127. Property TopLeft:Vec2<T>()
  128. Return min
  129. Setter( v:Vec2<T> )
  130. min=v
  131. End
  132. #rem monkeydoc The top-right of the rect.
  133. #end
  134. Property TopRight:Vec2<T>()
  135. Return New Vec2<T>( max.x,min.y )
  136. Setter( v:Vec2<T> )
  137. max.x=v.x
  138. min.y=v.y
  139. End
  140. #rem monkeydoc The bottom-right of the rect.
  141. #end
  142. Property BottomRight:Vec2<T>()
  143. Return max
  144. Setter( v:Vec2<T> )
  145. max=v
  146. End
  147. #rem monkeydoc The bottom-left of the rect.
  148. #end
  149. Property BottomLeft:Vec2<T>()
  150. Return New Vec2<T>( min.x,max.y )
  151. Setter( v:Vec2<T> )
  152. min.x=v.x
  153. max.y=v.y
  154. End
  155. #rem monkeydoc True if Right\<=Left or Bottom\<=Top.
  156. #end
  157. Property Empty:Bool()
  158. Return max.x<=min.x Or max.y<=min.y
  159. End
  160. #rem monkeydoc Adds another rect to the rect and returns the result.
  161. #end
  162. Operator+:Rect( r:Rect )
  163. Return New Rect( min+r.min,max+r.max )
  164. End
  165. #rem monkeydoc Subtracts another rect from the rect and returns the result.
  166. #end
  167. Operator-:Rect( r:Rect )
  168. Return New Rect( min-r.min,max-r.max )
  169. End
  170. #rem monkeydoc Multiples the rect by a vector and returns the result.
  171. #end
  172. Operator*:Rect( v:Vec2<T> )
  173. Return New Rect( min.x*v.x,min.y*v.y,max.x*v.x,max.y*v.y )
  174. End
  175. #rem monkeydoc Divides the rect by a vector and returns the result.
  176. #end
  177. Operator/:Rect( v:Vec2<T> )
  178. Return New Rect( min.x/v.x,min.y/v.y,max.x/v.x,max.y/v.y )
  179. End
  180. #rem monkeydoc Adds a vector to the rect and returns the result.
  181. #end
  182. Operator+:Rect( v:Vec2<T> )
  183. Return New Rect( min+v,max+v )
  184. End
  185. #rem monkeydoc Subtracts a vector from the rect and returns the result.
  186. #end
  187. Operator-:Rect( v:Vec2<T> )
  188. Return New Rect( min-v,max-v )
  189. End
  190. #rem monkeydoc Adds another rect to the rect.
  191. #end
  192. Operator+=( r:Rect )
  193. min+=r.min
  194. max+=r.max
  195. End
  196. #rem monkeydoc Subtracts another rect from the rect.
  197. #end
  198. Operator-=( r:Rect )
  199. min-=r.min
  200. max-=r.max
  201. End
  202. #rem monkeydoc Multiples the rect by a vector.
  203. #end
  204. Operator*=( v:Vec2<T> )
  205. min*=v
  206. max*=v
  207. End
  208. #rem monkeydoc Divides the rect by a vector.
  209. #end
  210. Operator/=( v:Vec2<T> )
  211. min/=v
  212. max/=v
  213. End
  214. #rem monkeydoc Adds a vector to the rect.
  215. #end
  216. Operator+=( v:Vec2<T> )
  217. min+=v
  218. max+=v
  219. End
  220. #rem monkeydoc Subtracts a vector from the rect.
  221. #end
  222. Operator-=( v:Vec2<T> )
  223. min-=v
  224. max-=v
  225. End
  226. #rem monkeydoc Computes the intersection of the rect with another rect and returns the result.
  227. #end
  228. Operator&:Rect( r:Rect )
  229. Local x0:=Max( min.x,r.min.x )
  230. Local y0:=Max( min.y,r.min.y )
  231. Local x1:=Min( max.x,r.max.x )
  232. Local y1:=Min( max.y,r.max.y )
  233. Return New Rect( x0,y0,x1,y1 )
  234. End
  235. #rem monkeydoc Computes the union of the rest with another rect and returns the result.
  236. #end
  237. Operator|:Rect( r:Rect )
  238. Local x0:=Min( min.x,r.min.x )
  239. Local y0:=Min( min.y,r.min.y )
  240. Local x1:=Max( max.x,r.max.x )
  241. Local y1:=Max( max.y,r.max.y )
  242. Return New Rect( x0,y0,x1,y1 )
  243. End
  244. #rem monkeydoc Intersects the rect with another rect.
  245. #end
  246. Operator&=( r:Rect )
  247. min.x=Max( min.x,r.min.x )
  248. min.y=Max( min.y,r.min.y )
  249. max.x=Min( max.x,r.max.x )
  250. max.y=Min( max.y,r.max.y )
  251. End
  252. #rem monkeydoc Unions the rect with another rect.
  253. #end
  254. Operator|=( r:Rect )
  255. min.x=Min( min.x,r.min.x )
  256. min.y=Min( min.y,r.min.y )
  257. max.x=Max( max.x,r.max.x )
  258. max.y=Max( max.y,r.max.y )
  259. End
  260. #rem monkeydoc Gets the rect centered within another rect.
  261. #end
  262. Method Centered:Rect( r:Rect )
  263. Local x:=(r.Width-Width)/2+r.min.x
  264. Local y:=(r.Height-Height)/2+r.min.y
  265. Return New Rect( x,y,x+Width,y+Height )
  266. End
  267. #rem monkeydoc Checks if the rect contains a vector or another rect.
  268. #end
  269. Method Contains:Bool( v:Vec2<T> )
  270. Return v.x>=min.x And v.x<max.x And v.y>=min.y And v.y<max.y
  271. End
  272. Method Contains:Bool( r:Rect )
  273. Return min.x<=r.min.x And max.x>=r.max.x And min.y<=r.min.y And max.y>=r.max.y
  274. End
  275. #rem monkeydoc Checks if the rect intersects another rect.
  276. #end
  277. Method Intersects:Bool( r:Rect )
  278. Return r.max.x>min.x And r.min.x<max.x And r.max.y>min.y And r.min.y<max.y
  279. End
  280. #rem monkeydoc Gets a string describing the rect.
  281. Deprecated: Use Operator To:String instead.
  282. #end
  283. Method ToString:String()
  284. Return Self
  285. End
  286. End
  287. #rem monkeydoc Transforms a Rect\<Int\> by an AffineMat3.
  288. #end
  289. Function TransformRecti<T>:Recti( rect:Recti,matrix:AffineMat3<T> )
  290. Local min:=matrix * New Vec2<T>( rect.min.x,rect.min.y )
  291. Local max:=matrix * New Vec2<T>( rect.max.x,rect.max.y )
  292. Return New Recti( Round( min.x ),Round( min.y ),Round( max.x ),Round( max.y ) )
  293. End