1
$\begingroup$

I had a similar question earlier, but it was answered, and including this there would be messy.

This works: (it draws just the vertical sides of a rectangle of size 2w*2h)

Clear["`*"]
P = {};
p1[w_, h_] := {-w, -h};
p2[w_, h_] := {-w, h};
p3[w_, h_] := {w, -h};
p4[w_, h_] := {w, h};
line1[w_, h_, pt_] := Line[{p1[w, h] + pt, p2[w, h] + pt}];
line2[w_, h_, pt_] := Line[{p3[w, h] + pt, p4[w, h] + pt}];
AppendTo[P, line1[w, h, pt]];
AppendTo[P, line2[w, h, pt]];
With[{P = P}, 
 Manipulate[
  Graphics[{line1[w, h, pt], line2[w, h, pt]}, PlotRange -> 1, 
   Axes -> True], {{w, 0.5}, 0, 1}, {{h, 0.5}, 0, 
   1}, {{pt, {0, 0}}, {0, 0}, {1, 1}}]]

However if you replace the final line with this, it fails (it draws two diagonal lines).

With[{P = P}, 
 Manipulate[
  Graphics[P, PlotRange -> 1, 
   Axes -> True], {{w, 0.5}, 0, 1}, {{h, 0.5}, 0, 
   1}, {{pt, {0, 0}}, {0, 0}, {1, 1}}]]

Notice that the only difference in that line is replacing the {line1 ...} with P. If you print {line1 ...} and P, they look identical.

I think the problem is variable scope. I've tried With[{w = w, h = h, pt = pt, P = P}, ...]. Same result. I've tried removing the With[{P = P} ...]. I get an error.

The reason this is important is, I have to pass a long set of lines in P, and it's cumbersome to write them all inside the Graphics[...] statement.

$\endgroup$

1 Answer 1

1
$\begingroup$

Notice change to the very last line for the evaluation of pt inside the Manipulate:

Clear["`*"]
P = {};
p1[w_, h_] := {-w, -h};
p2[w_, h_] := {-w, h};
p3[w_, h_] := {w, -h};
p4[w_, h_] := {w, h};
line1[w_, h_, pt_] := Line[{p1[w, h] + pt, p2[w, h] + pt}];
line2[w_, h_, pt_] := Line[{p3[w, h] + pt, p4[w, h] + pt}];
AppendTo[P, line1[w, h, pt]];
AppendTo[P, line2[w, h, pt]];
With[{P = P},
 Manipulate[
  Graphics[P, PlotRange -> 1, Axes -> True], {{w, 0.5}, 0, 
   1}, {{h, 0.5}, 0, 1}, {{pt, 0}, 0, 1}]]

The reason is that you change the structure of the calculation when you enter the manipulate with pt yet unevaluated, but with the rest of the expression already evaluated using a function. Inside the manipulate, you were adding a scalar to a pair.


To help understand what was going on, I used a few Echo statements outside and inside Manipulate for troubleshooting such as:

Echo[P, "outside "];

That's your problem, how you intend to solve it, I leave it up to you to post your own answer when you do so.

$\endgroup$
4
  • $\begingroup$ Thanks! So I put pt={0,0} at the top and now the non-working case draws the correct lines. However, I want to put a Locator on the last line's "{{pt, {0, 0}}, {0, 0}, {1, 1}}" statement. When I put Locator there, a locator shows up. But when I drag the Locator, the value of pt does not change! In other words, the two lines do not move! I think this is because I initialized pt to a fixed value so the locator cannot change it. What I'm trying to do is to be able to drag to translate the two lines, with a locator. My real case has many more lines; I want to translate them together with Locator. $\endgroup$
    – dnh234589
    Dec 18, 2021 at 16:39
  • $\begingroup$ Note, if you recall the other Mma SO question I linked to in my original post, your first answer there is basically the same as my solution here. And the problem there (initializing the offending variable) causes the same problem here (the variable can no longer change). So I'm not sure how to initialize a 2D list and allow it to change, so I can use Locator. $\endgroup$
    – dnh234589
    Dec 18, 2021 at 16:41
  • $\begingroup$ May be some experienced contributor can see through your problem and suggest improvements. I am not aware of the larger architecture of your project. I suggest you start a fresh post to clearly say what difficulty you face and provide a link to your previous posts as well. $\endgroup$
    – Syed
    Dec 18, 2021 at 17:26
  • $\begingroup$ OK thanks! Just started one here explaining exactly what I'm trying to do: mathematica.stackexchange.com/questions/260921/… $\endgroup$
    – dnh234589
    Dec 18, 2021 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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