6

I have the following code to show a red area defined by inequalities:

ClearAll["Global`*"];
p = Reduce[y <= 3/10 x + 18 && y > x^2/8, {x, y}]
r = RegionPlot[p, {x, -15, 18}, {y, -5, 25}, 
   GridLines -> {Table[i, {i, -15, 18}], Table[j, {j, -5, 25}]}, 
   PlotStyle -> Directive[{Opacity[0.5], Red}]];
bg = Graphics[{Opacity[0.2], Yellow, Rectangle[{-16, -6}, {19, 26}]}];
range = First /@ 
  Differences /@ (PlotRange /. Options[r]); target = 1; Show[{r, bg}, 
 AspectRatio -> (Last[range]/First[range]/target)]

which shows:

enter image description here

How can I count those integer grids and highlight them with colored dots?

LCFactorization
  • 3,047
  • 24
  • 37

3 Answers3

9

You can do :

p = ImplicitRegion[y <= 3/10 x + 18 && y > x^2/8, {x, y}]

points = Reduce[Element[{x, y}, p], {x, y}, Integers]

pp = Cases[points, x == xx_ && y == yy_ -> {xx, yy}]

pp // Length
(* 286 *) 

Show[RegionPlot[p], ListPlot[pp]]

enter image description here

b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84
8
eqn = y <= 3/10 x + 18 && y > x^2/8;
sol = Reduce[eqn, {x, y}, Integers];
Length @ sol
(* 286 *)
points = {x, y} /. {ToRules[sol]}; (* thanks: BobHanlon *)
RegionPlot[eqn, {x, -15, 18}, {y, -5, 25}, 
           GridLines -> {Range[-15, 18], Range[-5, 25]}, 
           PlotStyle -> Directive[{Opacity[0.5], Red}], 
           Epilog -> {PointSize[Medium], Point[points]}]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
8

Here is another solution using V10 functionalities:

region = ImplicitRegion[y <= 3/10 x + 18 && y > x^2/8, {{x, -15, 18}, {y, -5, 25}}];

lis = Tuples[{Range[-15, 18], Range[-5, 25]}];

We create a RegionMemberFunction

rm = RegionMember[region];

Now we select from lis the points that are in the region:

in = Select[lis, rm];

Length @ in

286

Visualize:

out = Complement[lis, in];

Show[RegionPlot[region], Graphics[{Red, Point[in],Green, Point[out]}], 
                         PlotRange -> {{-15, 18}, {-5, 25}}]

Mathematica graphics

RunnyKine
  • 33,088
  • 3
  • 109
  • 176