1
$\begingroup$

I want to have a grid background for the function (2/5)*x+3/5 but with one small detail. I want the coordinates of the points to be in the centres of the background squares, not on their corners. I am playing with Bresenham's line algorithm so I want to see nearest to the centre of which squares a line goes through.

$\endgroup$
5
  • 2
    $\begingroup$ I'm not sure what do you want but this: mathematica.stackexchange.com/q/37460/5478 is related for sure. $\endgroup$
    – Kuba
    Jan 24, 2014 at 10:51
  • $\begingroup$ I want to shift the GridLines by 0.5 to the right and down, so that the point (1;1) for example is exactly in the middle of a square of a grid instead of on a corner. $\endgroup$ Jan 24, 2014 at 10:55
  • $\begingroup$ Something like Plot[(2/5)*x + 3/5, {x, 0, 20}, GridLines -> {Range[.5, 20, 1], Range[.5, 20, 1]}, AspectRatio -> .5, ImageSize -> 500, Prolog -> RegionPlot[ Floor[(2/5)*x + 3/5] - .0 <= y <= Ceiling[(2/5)*x + 3/5] + .0, {x, 0, 20}, {y, 0, 10}, Axes -> False, AspectRatio -> .5, PlotStyle -> Opacity[.2]][[1]]] (This is just to get some idea of what you're after, not any real solution). $\endgroup$
    – ciao
    Jan 24, 2014 at 11:12
  • $\begingroup$ Yes, @rasher, this is exactly what I want, please post it as an answer. $\endgroup$ Jan 24, 2014 at 11:14
  • $\begingroup$ @AlexPopov- OK, but as I said, it was a proof-of-concept kind of thing, you'll probably want to tweak it! $\endgroup$
    – ciao
    Jan 24, 2014 at 11:15

3 Answers 3

4
$\begingroup$

As requested:

Plot[(2/5)*x + 3/5, {x, 0, 20}, 
 GridLines -> {Range[.5, 20, 1], Range[.5, 20, 1]}, AspectRatio -> .5,
  ImageSize -> 500, 
 Prolog -> 
  RegionPlot[
    Floor[(2/5)*x + 3/5] - .0 <= y <= Ceiling[(2/5)*x + 3/5] + .0, {x,
      0, 20}, {y, 0, 10}, Axes -> False, AspectRatio -> .5, 
    PlotStyle -> Opacity[.2]][[1]]]

I'm sure there are more elegant ways...

$\endgroup$
3
  • $\begingroup$ I posted an extension of this as an answer below. If you would prefer that I make it an edit to your answer I can do that instead. $\endgroup$
    – Mr.Wizard
    Jan 26, 2014 at 8:01
  • $\begingroup$ Good lord no, like I said in my comment to the OP, it was a proof-of-concept that ended up being useful to them. You've turned it into something actually usable for generalized cases, certainly worthy of it's own answer and credit! $\endgroup$
    – ciao
    Jan 26, 2014 at 8:10
  • $\begingroup$ Okay, cool. :-) $\endgroup$
    – Mr.Wizard
    Jan 26, 2014 at 8:21
3
$\begingroup$

Here is an attempt to package rasher's method into something a bit more easily reused:

SetAttributes[plotWithGrid, HoldAll]

plotWithGrid[fn_, {v_, x_, X_}, opts : OptionsPattern[Plot]] :=
 Module[{gf, plot, fill},
  gf[lo_, hi_] := Range[Floor[lo, 1/2], Ceiling[hi, 1/2]];
  plot = Plot[fn, {v, x, X}, GridLines -> {gf, gf}, opts];
  fill = PlotRange[plot] /. {_, {y_, Y_}} :>
    RegionPlot[⌊fn⌋ ≤ yy ≤ ⌈fn⌉,
      {v,  x - 0.1 (X - x), X + 0.1 (X - x)},
      {yy, y - 0.1 (Y - y), Y + 0.1 (Y - y)},
      Axes -> False,
      Frame -> False,
      PlotStyle -> Opacity[.2],
      PlotPoints -> 120
    ];
  Show[plot, Prolog -> fill[[1]]]
 ]

Test:

plotWithGrid[7 Sinc[x], {x, 0, 20}, PlotRange -> All, AspectRatio -> Automatic]

enter image description here

$\endgroup$
0
$\begingroup$

I couldn't find a general setting for shifting the grindlines to I used the option for specifying manual gridlines and used this: GridLines -> {{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5}, {-0.5, -1.5, -2.5, -3.5, -4.5, - 5.5}}.

$\endgroup$
1
  • 1
    $\begingroup$ Try {Range[.5, 11.5, .5], Reverse@Range[-5.5, -.5, .5]} $\endgroup$
    – Kuba
    Jan 24, 2014 at 11:26

Your Answer

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

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