Re: rectangle intersection
- To: mathgroup at smc.vnet.net
- Subject: [mg36134] Re: [mg36093] rectangle intersection
- From: Hartmut Wolf <hartmut_lebrecht_wolf at yahoo.com>
- Date: Fri, 23 Aug 2002 00:25:07 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Frank Brand [mailto:frank.brand at t-online.de] To: mathgroup at smc.vnet.net >Sent: Wednesday, August 21, 2002 11:52 AM >Subject: [mg36134] [mg36093] rectangle intersection > > >Dear colleagues, > >any hints on how to implement a very fast routine in Mathematica for >testing if two rectangles have an intersection area? >Thanks in advance >Frank Brand > > g := Table[Random[], {2}] No assumption is being made for the order of {x11, x12} etc. test[{{ x11_, x12_}, { y11_, y12_}}, {{ x21_, x22_}, { y21_, y22_}}] := ! (MatchQ[ Sort[Join[Transpose[{{ x11, x12}, {1, 1}}], Transpose[{{ x21, x22}, {2, 2}}]]][[All, 2]], {s_, s_, t_, t_}] \[Or] MatchQ[ Sort[Join[Transpose[{{ y11, y12}, {1, 1}}], Transpose[{{ y21, y22}, {2, 2}}]]][[All, 2]], {s_, s_, t_, t_}]) Exactly if for the x- or y-Coordinates all from one rectangle are to one side of the other, the rectangles do not intersect. (Of course you may define the Interface differently as to your use.) Unprotect[Rectangle] (*just for better demonstration*) Rectangle[{ x11_, y11_}, {x12_, y12_}] := Line[{{ x11, y11}, { x12, y11}, {x12, y12}, {x11, y12}, { x11, y11}}] x := Module[{ x11, x12, y11, y12}, {{{ x11, x12}, { y11, y12}}, {{ x21, x22}, { y21, y22}}} = {{g, g}, {g, g}}; Show[Graphics[ {Hue[0, .5, 1], Rectangle[{ x11, y11}, {x12, y12}], Hue[.5, .5, 1], Rectangle[{ x21, y21}, {x22, y22}]}]]; test[{{ x11, x12}, { y11, y12}}, {{ x21, x22}, { y21, y22}}] ] Type x and execute -- Hartmut Wolf __________________________________________________ Do You Yahoo!? HotJobs - Search Thousands of New Jobs http://www.hotjobs.com