4
$\begingroup$

I'd like to draw a triangle (given the side lengths) with the sides colored separately.

I tried Graphics[{FaceForm[Opacity[0]], EdgeForm[Blue], SSSTriangle[2, 2, 3]}]

giving:

my try

But I want to color the edges separately, eg Blue, Green and Red.

$\endgroup$
  • $\begingroup$ Pretty sure the only way is to make the graphic using individual lines. $\endgroup$ – george2079 Jun 28 '16 at 21:09
  • $\begingroup$ What about using Graph? $\endgroup$ – mgamer Jun 28 '16 at 21:13
6
$\begingroup$

I was going to do the same as george2079 (+1). I'll post it on account of having used a rather different programming style.

colorTriangle[Triangle[{pt1_, pt2_, pt3_}], {c1_, c2_, c3_}] := {
  c1, Line[{pt1, pt2}],
  c2, Line[{pt2, pt3}],
  c3, Line[{pt3, pt1}]
  }

Graphics[{
  colorTriangle[SSSTriangle[2, 2, 3], {Red, Green, Blue}]
  }]

Mathematica graphics

Coloring it so that particular colors correspond to particular side lengths is more difficult, but here's one way:

SetAttributes[colorTriangle, HoldFirst]
colorTriangle[SSSTriangle[l1_, l2_, l3_], {c1_, c2_, c3_}] := Module[{allTriangles, wellOrderedQ, wellOrderedTriangles},
  allTriangles = SSSTriangle[l1, l2, l3] /. Triangle[{pt1_, pt2_, pt3_}] :> Permutations[{
       Line[{pt1, pt2}], Line[{pt2, pt3}], Line[{pt3, pt1}]
       }];

  wellOrderedQ = allTriangles /. {Line[{pt11_, pt12_}], Line[{pt21_, pt22_}], Line[{pt31_, pt32_}]} :> And[
      Norm[pt11 - pt12] == l1,
      Norm[pt21 - pt22] == l2,
      Norm[pt31 - pt32] == l3
      ];

  wellOrderedTriangles = Pick[allTriangles, wellOrderedQ, True];

  Graphics@Riffle[{Red, Green, Blue}, #] & /@ wellOrderedTriangles
  ]

Since there aren't always one unique way to color a triangle in this fashion the result is a list of up to six triangles:

Mathematica graphics

$\endgroup$
  • $\begingroup$ Looking good! But the edge lengths are in a different order to their colors! $\endgroup$ – pdmclean Jun 28 '16 at 21:29
  • 2
    $\begingroup$ @pdmclean That criterion complicates the problem significantly and should have been mentioned in the question... Anyway, I've posted an update about that now. $\endgroup$ – C. E. Jun 28 '16 at 23:29
2
$\begingroup$

Just to make this more difficult than it has to be,

HighlightMesh[
 DiscretizeGraphics@SSSTriangle[2, 2, 3], {Style[{2, 1}, White]}~
  Join~({Style[{1, {#}}, Thick, {Red, Green, Blue}[[#]]] & /@ 
     Range[3]})]

Mathematica graphics

$\endgroup$
  • $\begingroup$ +1 posted similar thing but was 1 min late :) I was struggling with 2D component because usually Style None works but here it breaks everything and White saves the day, $\endgroup$ – Kuba Jun 28 '16 at 21:43
  • $\begingroup$ @Kuba - I was trying for Style[.., Opacity[0]] but I couldn't get it to work.. $\endgroup$ – Jason B. Jun 28 '16 at 21:47
1
$\begingroup$
Graphics[Transpose[{{Red, Green, Blue}, SSSTriangle[2, 2, 3] /.
     Triangle[li_] :> Line /@ Partition[Append[li, li[[1]]], 2, 1] }]]

enter image description here

$\endgroup$
  • 3
    $\begingroup$ Vastly simpler: Graphics[Riffle[{Red, Green, Blue}, Map[Line, Partition[#, 2, 1, 1] & @@ SSSTriangle[2, 2, 3]]]] $\endgroup$ – J. M. is away Jun 28 '16 at 23:30

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.