Skip to main content
Commonmark migration
Source Link

I tried to plot integer points that fall into a 2D region that's dynamically changed. So I resort to Cases and Select, but Cases with Condition is not working expectedly. Here is the simplifed version of my code and result:

region[u_] := u <= x + y <= u + 2;
intPoints = Flatten[Table[{x, y}, {x, 0, 13}, {y, 0, 13}], 1];
Manipulate[
 Column[{
   {ListPlot[intPoints], 
    RegionPlot[region[u], {x, 0, 13}, {y, 0, 13}]},
   {ListPlot[Cases[intPoints, {x_, y_} /; region[u]]],
    ListPlot[Cases[intPoints, {x_, y_} /; Evaluate[region[u]]]],
    ListPlot[Select[intPoints, region[u] /. {x -> #[[1]], y -> #[[2]]} &]]}}],
 {u, 0, 13}
 ]

[![enter image description here][1]][1]enter image description here

I don't understand why inside Cases the point coordinates x and y won't go into region for the Condition to be evaluated. Does it first do the replacement of x and y and then do the evaluation? But why the additional Evaluate operation still doesn't help?

Could someone elaborate me on this? Thanks. [1]: https://i.sstatic.net/rH7N1.png

I tried to plot integer points that fall into a 2D region that's dynamically changed. So I resort to Cases and Select, but Cases with Condition is not working expectedly. Here is the simplifed version of my code and result:

region[u_] := u <= x + y <= u + 2;
intPoints = Flatten[Table[{x, y}, {x, 0, 13}, {y, 0, 13}], 1];
Manipulate[
 Column[{
   {ListPlot[intPoints], 
    RegionPlot[region[u], {x, 0, 13}, {y, 0, 13}]},
   {ListPlot[Cases[intPoints, {x_, y_} /; region[u]]],
    ListPlot[Cases[intPoints, {x_, y_} /; Evaluate[region[u]]]],
    ListPlot[Select[intPoints, region[u] /. {x -> #[[1]], y -> #[[2]]} &]]}}],
 {u, 0, 13}
 ]

[![enter image description here][1]][1]

I don't understand why inside Cases the point coordinates x and y won't go into region for the Condition to be evaluated. Does it first do the replacement of x and y and then do the evaluation? But why the additional Evaluate operation still doesn't help?

Could someone elaborate me on this? Thanks. [1]: https://i.sstatic.net/rH7N1.png

I tried to plot integer points that fall into a 2D region that's dynamically changed. So I resort to Cases and Select, but Cases with Condition is not working expectedly. Here is the simplifed version of my code and result:

region[u_] := u <= x + y <= u + 2;
intPoints = Flatten[Table[{x, y}, {x, 0, 13}, {y, 0, 13}], 1];
Manipulate[
 Column[{
   {ListPlot[intPoints], 
    RegionPlot[region[u], {x, 0, 13}, {y, 0, 13}]},
   {ListPlot[Cases[intPoints, {x_, y_} /; region[u]]],
    ListPlot[Cases[intPoints, {x_, y_} /; Evaluate[region[u]]]],
    ListPlot[Select[intPoints, region[u] /. {x -> #[[1]], y -> #[[2]]} &]]}}],
 {u, 0, 13}
 ]

enter image description here

I don't understand why inside Cases the point coordinates x and y won't go into region for the Condition to be evaluated. Does it first do the replacement of x and y and then do the evaluation? But why the additional Evaluate operation still doesn't help?

Could someone elaborate me on this? Thanks.

Source Link

Cases with condition vs Select inside Manipulate

I tried to plot integer points that fall into a 2D region that's dynamically changed. So I resort to Cases and Select, but Cases with Condition is not working expectedly. Here is the simplifed version of my code and result:

region[u_] := u <= x + y <= u + 2;
intPoints = Flatten[Table[{x, y}, {x, 0, 13}, {y, 0, 13}], 1];
Manipulate[
 Column[{
   {ListPlot[intPoints], 
    RegionPlot[region[u], {x, 0, 13}, {y, 0, 13}]},
   {ListPlot[Cases[intPoints, {x_, y_} /; region[u]]],
    ListPlot[Cases[intPoints, {x_, y_} /; Evaluate[region[u]]]],
    ListPlot[Select[intPoints, region[u] /. {x -> #[[1]], y -> #[[2]]} &]]}}],
 {u, 0, 13}
 ]

[![enter image description here][1]][1]

I don't understand why inside Cases the point coordinates x and y won't go into region for the Condition to be evaluated. Does it first do the replacement of x and y and then do the evaluation? But why the additional Evaluate operation still doesn't help?

Could someone elaborate me on this? Thanks. [1]: https://i.sstatic.net/rH7N1.png