1
$\begingroup$

I'm new to Mathematica and this is also my first post here. Now for the question:

I try to let Mathematica draw a number of rectangles from lists of coordinates. Unfortunately it only works when I assign values to variables directly (for example: a=3) but not with lists.

Mathematica tells me that the coordinates have to be pairs of numbers (what they are, as far as I understand, because they are in the format {x,y}).

I imagine something like this:

min = {{1, 1}, {2, 2}, {2, 3}}
Graphics[{EdgeForm[{Thin, Blue}], White,Table[Rectangle[min, {5,5}]]}]

Which I hope should draw three rectangles with different starting points and the same endpoint. This is just chosen as a simple example, later start- and endpoint shall be given via lists.

I've tried few different approaches:

1. A list with coordinates ("min" mentioned earlier) --> error message (see above)

2. Two lists to split x- and y-coordinate --> error message (see above)

3. Using functions to calculate the coordinates (just as a test), which looks like this:

Graphics[{EdgeForm[{Thin, Blue}], White, 
  Table[Rectangle[{a + 1, a + 2}, {5, 5}], {a, 2}]}] --> works

4. Using a list for iteration, basically I've changed the {a,2} above to {a,minc} with minc beeing minc={{1},{2}} --> error message (see above)

5. Using a list for iteration but try to pick it's elements, leading to {a,Part[minc]} --> error message (see above)

6. Assigned coordiantes to variables directly and used this variables (for example: a=1, b=2, Rectangle[{a,b},{5,5}]) --> works

So, how do I manage to read in coordinates from lists for this application?

$\endgroup$
  • 1
    $\begingroup$ Look up Map[] and MapThread[]. $\endgroup$ – J. M. will be back soon Jun 15 '15 at 15:04
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$ – bbgodfrey Jun 15 '15 at 15:14
  • $\begingroup$ @Guess who it is: Thank you for the hint! I looked it up but I couldn't make it work. I believe I did not really understand the syntax and ended up with this: Map[Graphics[Rectangle[{#}, {5, 5}]] & /@ max]. But I guess it is the same problem like m_goldberg pointed out (which works...). @ bbgodfrey: Thank you, I'll try my best. $\endgroup$ – Gaston Jun 16 '15 at 6:50
2
$\begingroup$

Very simple error. You must give a second argument to Table which gives the interation specifications.

min = {{1, 1}, {2, 2}, {2, 3}};
Graphics[{EdgeForm[{Thin, Blue}], White, Table[Rectangle[m, {5, 5}], {m, min}]}]

rects

$\endgroup$
  • $\begingroup$ Thank you for your help, this really was easy. Now I feel like a fool... $\endgroup$ – Gaston Jun 16 '15 at 6:53
  • $\begingroup$ @Gaston. Don't feel bad, making errors like this one is part of the learning process. I been using Mathematica for 18 years and I still make similar mistakes sometimes. What experience brings is the ability to recognize such mistakes quickly. $\endgroup$ – m_goldberg Jun 16 '15 at 7:03

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