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.