# An efficient way of constructing line animation [closed]

I am trying to visualize the solution of this leetcode problem by using MatrixPlot. Here is what I have now:

The steps of solving the problem is available in its editorial solution.

I use Line to draw the cross and Circle to draw the circle. Here's my code, it's messy because I hardcode many parameters (time, positions etc.)

Is there a way to visualize the solution with more simplicity and less code?

My code:

    piec[x_, top_, btm_] :=
Piecewise[{{btm, x <= btm}, {x, x < top && x > btm}, {top,
x >= top}}]

piecLine[a_, b_, c_, d_] :=
Piecewise[{{Null, a == c && b == d}, {Line[{{a, b}, {c, d}}],
a != c || b != d}}]

Manipulate[
Show[MatrixPlot[
Table[{Boole[i >= 1], Boole[i >= 2], Boole[i >= 3], Boole[i >= 4],
Boole[i >= 5], Boole[i >= 6], Boole[i >= 7], Boole[i >= 8],
Boole[i >= 9], Boole[i >= 10]}, {i, 10}], Mesh -> All,
MeshStyle -> Directive[Gray, Dashed],
ColorRules -> {1 -> GrayLevel[0.76], 0 -> Orange}],
Table[Graphics[{AbsoluteThickness[t],
piecLine[j - a, 9.5 - a, piec[k, j + a, j - a],
piec[-j + k + 9.5, 9.5 + a, 9.5 - a]],
piecLine[j - a, 9.5 + a, piec[k, j + a, j - a],
piec[9.5 + j - k, 9.5 + a, 9.5 - a]]}], {j, 1.5, 8.5,
1}],   (*first row*)

Table[Graphics[{AbsoluteThickness[t],
piecLine[9.5 - a, j - a, piec[-j + k + 9.5, 9.5 + a, 9.5 - a],
piec[k, j + a, j - a]]}], {j, 7.5, 1.5, -1}],
Table[
Graphics[{AbsoluteThickness[t],
piecLine[9.5 - a, j + a,
9.5 - a + piec[k - j - 0.5 + a, 2 a, 0],
j + a - piec[k - j - 0.5 + a, 2 a, 0]]}], {j, 1.5, 7.5, 1}], (* first column *)

Table[Graphics[{AbsoluteThickness[t],
piecLine[j + 1 - a, 8.5 + a,
piec[k - 11.5 + 2.5, j + 1 + a, j + 1 - a],
piec[20 - k + j - 1.5, 8.5 + a, 8.5 - a]],
piecLine[j + 1 - a, 8.5 - a,
piec[k - 11.5 + 2.5, j + 1 + a, j + 1 - a],
piec[k - 11.5 + 8.5 - j + 1.5, 8.5 + a, 8.5 - a]]}], {j, 1.5,
6.5, 1}],               (* second row *)

Graphics[{AbsoluteThickness[t],
Circle[{9.5, 9.5}, 1.3 a, {0, piec[k, 2, 0] Pi}]}],
Graphics[{AbsoluteThickness[t],
Circle[{9.5, 8.5}, 1.3 a, {0, piec[k - 4, 2, 0] Pi}]}],
Graphics[{AbsoluteThickness[t],
Circle[{8.5, 8.5}, 1.3 a, {0, piec[k - 6, 2, 0] Pi}]}],
Graphics[{AbsoluteThickness[t],
Circle[{7.5, 7.5}, 1.3 a, {0, piec[k - 8, 2, 0] Pi}]}]
], {k, 0, 20}];


## closed as unclear what you're asking by MarcoB, Feyre, corey979, Young, C. E.Jan 31 '17 at 16:44

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• I'll admit that I don't understand how your visualization is correlated with the problem you linked. Perhaps you could explain what the symbols mean etc. – MarcoB Jan 30 '17 at 16:58
• The code appears to have errors in it. – C. E. Jan 30 '17 at 21:36
• You are missing values for a and t. – Jack LaVigne Jan 31 '17 at 2:46

## 1 Answer

You can tidy the code up a bit. Here I have:

• Replaced piec with the built-in Clip
• Replaced the piecewise function in piecLine with multiple definitions
• Defined cross and nought to do the drawing
• Replaced multiple Graphics expressions with a single one
• Replaced the matrix in MatrixPlot with a simpler alternative

.

a = 0.2;

piecLine[a_, b_, a_, b_] := {}
piecLine[a_, b_, c_, d_] := Line[{{a, b}, {c, d}}]

cross[p_, q_, t_] := {
piecLine[p - a, q - a, p + Clip[t, {-a, a}], q + Clip[t, {-a, a}]],
piecLine[p - a, q + a, p + Clip[t - 0.5, {-a, a}], q + Clip[0.5 - t, {-a, a}]]}

nought[pos_, t_] := Circle[pos, 1.3 a, {0, Clip[t, {0, 2}] Pi}]

background = MatrixPlot[LowerTriangularize@ConstantArray[1, {10, 10}],
Mesh -> All, MeshStyle -> Directive[Gray, Dashed],
ColorRules -> {1 -> GrayLevel[0.76], 0 -> Orange}];

Manipulate[
Show[background, Graphics[{AbsoluteThickness[2],

Table[cross[j, 9.5, k - j], {j, 1.5, 8.5}],
Table[cross[9.5, j, k - j], {j, 1.5, 7.5}],
Table[cross[j + 1, 8.5, k - j - 10], {j, 1.5, 6.5}],

nought[{9.5, 9.5}, k],
nought[{9.5, 8.5}, k - 4],
nought[{8.5, 8.5}, k - 6],
nought[{7.5, 7.5}, k - 8]

}]], {k, 0, 20}]


The code is not fundamentally any different to your original, but I think it's easier to read this way.