Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

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

share|improve this answer
    
Thanks! I knew I was missing something simple. –  M.R. Jan 24 '13 at 22:44
2  
@M.R. Mma has a lot of alignment options ... this kind of thing are sometimes easy ... sometimes not so –  belisarius Jan 24 '13 at 22:46
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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