# How to construct new geometrical shapes in Mathematica?

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?

• What version of Mathematica are you using? It's pretty easy on v10 I believe, would need to use Polygon on earlier versions. – Öskå Sep 29 '14 at 13:13
• 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. – C. E. Sep 29 '14 at 13:28

## 2 Answers

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]


• 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". – Öskå Sep 29 '14 at 17:37
• @Öskå yes, I agree. – Algohi Sep 29 '14 at 17:44
• Furthermore, Pickett's answer in the OP's first question uses RegionUnion.. – Öskå Sep 29 '14 at 17:45
• Thanks, I mentioned Pickett's in the answer. – Algohi Sep 29 '14 at 17:55
• 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. – MOON Oct 1 '14 at 11:25

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 GraphicsPolygonUtilsPolygonUnion[] and GraphicsPolygonUtilsPolygonCombine[]. (In much older versions, they were in the GraphicsMesh​ 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[GraphicsPolygonUtilsPolygonCombine,
GraphicsPolygonUtilsPolygonUnion][{r1, r2} /.
Rectangle[pmin_, pmax_] :> Polygon[{#1, #2, #4, #3} & @@
Tuples[Transpose[{pmin, pmax}]]]];

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