Message Boards Message Boards

0
|
3668 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Graphics does not show anything with a Do inside

Posted 10 years ago

Can you please tell me what is wrong with the following code?

Manipulate[Module[{v}, v = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; Graphics[{Do[Line[{v[[i]], v[[1]]}], {i, n}]}]], {n, {2, 3, 4}}]

I was hoping I get some lines as the output. Thanks, Ferenc

POSTED BY: Ferenc Beleznay
3 Replies

Hi,

I guess that you want to use the Table command rather than the Do loop:

Manipulate[Module[{v}, v = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; Graphics[Table[Line[{v[[i]], v[[1]]}], {i, 1, n}]]], {n, {2, 3, 4}}]

Cheers, M.

POSTED BY: Marco Thiel
Posted 10 years ago

Yes, indeed, thank you. I see now that Graphics needs a list of primitives, and the output of Do is not a list. I have to get used to the Mathematica language and move away from procedural thinking. Thanks, Ferenc

POSTED BY: Ferenc Beleznay

Dear Ferenc,

you can, of course, also use the Do command to create a list. There are different ways to do this and it is certainly easier to use Table. If for some reason you need to use Do, then something like this might work for you

Manipulate[Module[{v}, v = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; 
Graphics[Reap[Do[Sow[Line[{v[[i]], v[[1]]}]], {i, 1, n}]][[2,1]]]], {n, {2, 3, 4}}]

M.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract