11
$\begingroup$

I want to go from

Graphics[{Red, Rectangle[{0, 0}, {1, 3}], Blue, Polygon[{{1, 1}, {3, 1}, {2, 2}}]}, Frame -> True]

enter image description here

to

enter image description here

I have rather clumsily written:

RectangleToLines[r_] := With[{n = Flatten @ r},
  Line[{{n[[1]], n[[2]]}, {n[[3]], n[[2]]}, {n[[3]], n[[4]]}, {n[[1]],n[[4]]}, {n[[1]], n[[2]]}}]
  ]

RectangleToLines[{{0, 0}, {1, 3}}]

Line[{{0, 0}, {1, 0}, {1, 3}, {0, 3}, {0, 0}}]

and similar with the triangle. Terrible, because I would end up with many "FormToLines"-definitions.

I feel - that especially with V10 - there should be a much cleverer way to do this.

$\endgroup$
18
$\begingroup$

A little simpler & shorter, but effectively the same as the answer by ybeltukov:

g = Graphics[{Red, Rectangle[{0, 0}, {1, 3}], Blue, 
     Polygon[{{1, 1}, {3, 1}, {2, 2}}]}, Frame -> True];

g /. c_RGBColor :> Sequence[EdgeForm[c], Transparent]

enter image description here


Based on the excellent comments, a version that works for all color directives, draws no faces instead of transparent ones and uses Directive, which is more typical for Graphics, instead of the more general Sequence.

g /. c_?ColorQ :> Directive[EdgeForm[c], FaceForm[]]
$\endgroup$
  • 3
    $\begingroup$ +1 You could also use Directive $\endgroup$ – Simon Woods Sep 7 '14 at 14:58
  • 2
    $\begingroup$ You can also use FaceForm[] instead of Transparent. $\endgroup$ – Algohi Sep 7 '14 at 17:56
  • 2
    $\begingroup$ You can also use c_?ColorQ so that Hue, CMYKColor, GrayLevel, etc. are all recognized. $\endgroup$ – rm -rf Sep 8 '14 at 0:45
10
$\begingroup$

There is a brute force solution with substitution all colors to EdgeForm colors and putting FaceForm[None] at the beginning

g = Graphics[{Red, Rectangle[{0, 0}, {1, 3}], Blue, 
    Polygon[{{1, 1}, {3, 1}, {2, 2}}]}, Frame -> True];
Graphics[Prepend[#, FaceForm[None]] /. c_RGBColor :> EdgeForm[c], ##2] & @@ g

enter image description here

$\endgroup$
4
$\begingroup$

An even bruter force approach using new region functions

g1 = Graphics[{Red, Rectangle[{0, 0}, {1, 3}], Blue, 
    Polygon[{{1, 1}, {3, 1}, {2, 2}}]}, Frame -> True];

Show[
 RegionPlot[
    ImplicitRegion[
     RegionMember[#[[1]], {x, y}],
     {x, y}],
    PlotStyle -> None,
    BoundaryStyle -> #[[2]]] & /@
  Thread[{
    Cases[g1, r_?(RegionQ[#] &), Infinity],
    Cases[g1, rgb_RGBColor, Infinity]}],
 PlotRange -> All]

enter image description here

$\endgroup$
1
$\begingroup$

Why not just use EdgeForm and FaceForm directly instead?

points = {{0, 0}, {4, 0}, {3, 3}};
triangle = Triangle[points];
circle = Insphere[points];

Graphics[{
  circle,
  {
    FaceForm[{Yellow,Opacity[0]}],
    EdgeForm[{Thick,Black}],
    triangle
  }
}]

incircle

$\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.