3
$\begingroup$

I would like to draw a cross as an inset in a Graphics environment. I tried the following:

Graphics[{Inset[Style["\[Cross]", 100], {0, 0},{Center,Center}], Circle[]}, 
     Frame -> True]

Inset of cross in circle

Unfortunately, the {Center,Center} command for specifying the exact point of the cross that is drawn at the {0,0} position does not use the intercept of the two parts of the cross. Is there an easy way to produce a cross using graphics primitives rather than special characters? I have not done much with graphics yet and would really appreciate your help.

$\endgroup$
1
  • $\begingroup$ When I evaluated your code on OS X using Mathematica V9, the cross was perfectly centered. $\endgroup$
    – m_goldberg
    Commented Feb 5, 2013 at 23:16

2 Answers 2

6
$\begingroup$

Nice & easy:-

a = 0.2; t = 6;
Graphics[{Circle[],
  Inset[Graphics[{AbsoluteThickness[t],
     Line[{{-a, -a}, {a, a}}]}], {0, 0}, Center, 2 a],
  Inset[Graphics[{AbsoluteThickness[t],
     Line[{{-a, a}, {a, -a}}]}], {0, 0}, Center, 2 a]},
 Frame -> True, ImageSize -> 250]

enter image description here

Using Inset ensures the resulting X stays within the 2 a bounds specified. The simpler red X shown, spills over slightly due to the thickness. The cross is also shown in green.

a = 0.2; t = 6;
Show[Graphics[{Circle[],
   AbsoluteThickness[t], Red,
   Line[{{-a, -a}, {a, a}}],
   Line[{{-a, a}, {a, -a}}]},
  Frame -> True, ImageSize -> 350],

 Graphics[{Circle[],
   Line[{{0, -1}, {0, 1}}],
   Line[{{-1, 0}, {1, 0}}],
   Line[{{a, -1}, {a, 1}}],
   Line[{{-1, a}, {1, a}}],
   Line[{{-a, -1}, {-a, 1}}],
   Line[{{-1, -a}, {1, -a}}],
   Inset[Style["\[Cross]", 100, Green], {0, 0}, {Center, Center}],
   Inset[Graphics[{AbsoluteThickness[t],
      Line[{{-a, -a}, {a, a}}]}], {0, 0}, Center, 2 a],
   Inset[Graphics[{AbsoluteThickness[t],
      Line[{{-a, a}, {a, -a}}]}], {0, 0}, Center, 2 a]},
  Frame -> True, ImageSize -> 350]]

enter image description here

Addendum

Inset X's can be made to scale. But first, using non-inset lines scales straight away:

a = 0.2; t = 6;
 Graphics[{Circle[], AbsoluteThickness[t], Red,
  Line[{{-a, -a}, {a, a}}], Line[{{-a, a}, {a, -a}}]},
 Frame -> True, ImageSize -> 250, AspectRatio -> 0.4]

enter image description here

Inset lines' sizes are not affected by the enclosing graphic's AspectRatio:

a = 0.2; t = 6; r = 0.4;
Graphics[{Circle[], Inset[Graphics[{AbsoluteThickness[t],
     Line[{{-a, -a}, {a, a}}]}], {0, 0}, Center, 2 a], 
  Inset[Graphics[{AbsoluteThickness[t],
     Line[{{-a, a}, {a, -a}}]}], {0, 0}, Center, 2 a]},
 Frame -> True, ImageSize -> 250, AspectRatio -> r]

enter image description here

Adding a factor to the insets can apply scaling:

a = 0.2; t = 6; r = 0.4;
Graphics[{Circle[], Inset[Graphics[{AbsoluteThickness[t],
     Line[{{-a, -a r}, {a, a r}}]}], {0, 0}, Center, 2 a],
  Inset[Graphics[{AbsoluteThickness[t],
     Line[{{-a, a r}, {a, -a r}}]}], {0, 0}, Center, 2 a]},
 Frame -> True, ImageSize -> 250, AspectRatio -> r]

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ Using inset also makes it possible to locate the cross anywhere: a = 0.2; t = 6; Graphics[{Circle[], Inset[Graphics[{AbsoluteThickness[t], Line[{{-a, -a}, {a, a}}]}], {1, 0}, Center, 2 a], Inset[Graphics[{AbsoluteThickness[t], Line[{{-a, a}, {a, -a}}]}], {1, 0}, Center, 2 a]}, Frame -> True, ImageSize -> 250] shifts the cross to the right. $\endgroup$
    – Apatura
    Commented Feb 5, 2013 at 15:42
  • $\begingroup$ Just out of interest: why does the cross stay symmetrically if I combine it with a plot with differently scaled axes? Doesn't the a refer to the values of the x and y values where the lines of the cross end? $\endgroup$
    – Apatura
    Commented Feb 5, 2013 at 16:00
  • 1
    $\begingroup$ @Apatura - You could add aspect ratios to the insets, or scale them as shown in the addendum to my post. $\endgroup$ Commented Feb 5, 2013 at 18:28
  • $\begingroup$ Interestingly, the green cross is not offset on Mac OSX. $\endgroup$ Commented Feb 5, 2013 at 18:30
  • $\begingroup$ Thank you very much, your answer definitely helped me understanding the use of the Graphics and Inset options. $\endgroup$
    – Apatura
    Commented Feb 5, 2013 at 22:05
3
$\begingroup$

\[Cross] is rendered with a font glyph. The center of the glyph is not absolute but rather in terms of typographic positioning. For example the glyph for , will be rendered lower on a line than the glyph for ^ -- this is not an error. If you wish to center a glyph you need to specify appropriate offsets for the third parameter of Inset. For example on my system (10.0.2 under Windows):

Graphics[
  {Inset[Style["\[Cross]", 100], {0, 0}, {0, -0.13}], Circle[]},
  Frame -> True, GridLines -> {{0}, {0}}
]

enter image description here

$\endgroup$

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