0
$\begingroup$

In my previous question 2D random walk within a bounded area, I asked how to implement a 2D random walk within a bounded area. In the provided solution, one can use Rectangle[{-10, -10}, {10, 10}] or Disk[{x, y}, r] to define the bounded region as a disk or rectangle. How can I define new bounded regions in Mathematica?

For example, how can I add these two Rectangle[{-10, -10}, {10, 10}] and Rectangle[{10, 0}, {15, 5}] to get a new region made of these two rectangles?

new region

$\endgroup$
  • $\begingroup$ What version of Mathematica are you using? It's pretty easy on v10 I believe, would need to use Polygon on earlier versions. $\endgroup$ – Öskå Sep 29 '14 at 13:13
  • $\begingroup$ Element[position + randomStep, region] is the line that determines if the new step is valid or not. If you use an earlier version than 10, you don't have access to RegionUnion so you can't combine arbitrary shapes. Instead you have to write Element[position + randomStep, rectangel1] || Element[position+randomStep, rectangle2] to indicate that both areas are valid. $\endgroup$ – C. E. Sep 29 '14 at 13:28
3
$\begingroup$

As Pickett's answer in the comment, if you are using v10 then you may try this:

r = RegionUnion[Rectangle[{-10, -10}, {10, 10}], 
   Rectangle[{10, 0}, {15, 5}]];
RegionPlot[r, BoundaryStyle -> Black, PlotStyle -> Black, 
 Frame -> False]

enter image description here

$\endgroup$
  • $\begingroup$ Well, this is in the docs and in Pickett's comment. I don't think the question can be answered until the OP specifies the version in use. If it's just about RegionUnion this question should be closed as "can easily be found in the documentation". $\endgroup$ – Öskå Sep 29 '14 at 17:37
  • $\begingroup$ @Öskå yes, I agree. $\endgroup$ – Algohi Sep 29 '14 at 17:44
  • $\begingroup$ Furthermore, Pickett's answer in the OP's first question uses RegionUnion.. $\endgroup$ – Öskå Sep 29 '14 at 17:45
  • $\begingroup$ Thanks, I mentioned Pickett's in the answer. $\endgroup$ – Algohi Sep 29 '14 at 17:55
  • $\begingroup$ What would be the solution if I have access to MMA9. In the office I have MMA9 and at home I have MMA10. The reply answer my problem. However, What if I or others have access just to MMA9. $\endgroup$ – MOON Oct 1 '14 at 11:25
1
$\begingroup$

If you do not want to use RegionUnion[], it is easy to produce a Polygon[] corresponding to the desired shape: this hinges on the use of the undocumented functions Graphics`PolygonUtils`PolygonUnion[] and Graphics`PolygonUtils`PolygonCombine[]. (In much older versions, they were in the Graphics`Mesh`​ context.) In your case, you will need the additional step of needing to convert Rectangle[] into an explicit Polygon[].

r1 = Rectangle[{-10, -10}, {10, 10}]; r2 = Rectangle[{10, 0}, {15, 5}];

new = Composition[Graphics`PolygonUtils`PolygonCombine,
                  Graphics`PolygonUtils`PolygonUnion][{r1, r2} /. 
      Rectangle[pmin_, pmax_] :> Polygon[{#1, #2, #4, #3} & @@
                                         Tuples[Transpose[{pmin, pmax}]]]];

{Graphics[new], Graphics[Line @@ new]} // GraphicsRow

the composite polygon

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.