6
$\begingroup$

I have dynamic graphics over an image but they won't line up, what am I doing wrong?

img = ExampleData[ExampleData["TestImage"][[1]]];
d = ImageDimensions@img;
pt1=d/4;pt2={d[[1]]3/4,d[[2]]1/4};pt3=3d/4;pt4={d[[1]]1/4,d[[2]]3/4};

MultiContourPolygon[polys_List] := Module[{contours = Append[#, First[#]]&/@polys}, 
    {Polygon@Join[Join@@contours,Reverse[First/@contours]],Line[contours]}
]

LocatorPane[
    Dynamic[{pt1,pt2,pt3,pt4}],
    Overlay[{img,
        Graphics[{Orange, Opacity[.4], 
            Dynamic @ MultiContourPolygon[{{pt1,pt2,pt3,pt4}, {{0,0}, {d[[1]],0},d,{0,d[[2]]}}}]},
            ImagePadding -> 0, ImageMargins -> 0, ImageSize -> d
        ]
        }, Alignment -> {Left,Bottom}, ImageSize -> d
    ]
]

enter image description here

$\endgroup$
0
4
$\begingroup$

You need to specify the Plot Range:

LocatorPane[Dynamic[{pt1, pt2, pt3, pt4}], 
 Overlay[{Image[img, ImageSize -> 400], 
   Graphics[{Orange, Opacity[.4], 
     Dynamic@MultiContourPolygon[{{pt1, pt2, pt3, 
         pt4}, {{0, 0}, {d[[1]], 0}, d, {0, d[[2]]}}}]}, 
    ImagePadding -> 0, ImageSize -> 400, 
    PlotRange -> Transpose[{{0, 0}, d}]]}, Alignment -> {Left, Left}, 
  ImageSize -> 400]]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Thanks! I knew I was missing something simple. $\endgroup$ – M.R. Jan 24 '13 at 22:44
  • 2
    $\begingroup$ @M.R. Mma has a lot of alignment options ... this kind of thing are sometimes easy ... sometimes not so $\endgroup$ – Dr. belisarius Jan 24 '13 at 22:46
4
$\begingroup$

Another way to do that is to use Show:

LocatorPane[Dynamic[{pt1, pt2, pt3, pt4}], 
 Show[Image[img], 
  Graphics[{Orange, Opacity[.4], 
    Dynamic@MultiContourPolygon[{{pt1, pt2, pt3, 
        pt4}, {{0, 0}, {d[[1]], 0}, d, {0, d[[2]]}}}]}, 
   ImagePadding -> 0]]]

enter image description here

With this solution you do not have to specify any PlotRange or ImageSize.

$\endgroup$
2
$\begingroup$

This is related to this recent question: relative scale of elements in ImageCompose

My answer is similar:

LocatorPane[Dynamic[{pt1, pt2, pt3, pt4}],
 Show[
  Rasterize @ img,
  Epilog ->
   {Orange, Opacity[.4], 
    Dynamic@MultiContourPolygon[{{pt1, pt2, pt3, pt4}, {{0, 0}, {d[[1]], 0}, 
        d, {0, d[[2]]}}}]}
 ]
]

Mathematica graphics

This way there is no need to mess with ImagePadding, ImageMargins, ImageSize, Alignment, PlotRange or any such options.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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