# How to color the edges of a triangle?

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:

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

• Pretty sure the only way is to make the graphic using individual lines. Jun 28, 2016 at 21:09
• What about using Graph? Jun 28, 2016 at 21:13

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}]
}]

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:

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

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]})]

• +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,
– Kuba
Jun 28, 2016 at 21:43
• @Kuba - I was trying for Style[.., Opacity[0]] but I couldn't get it to work.. Jun 28, 2016 at 21:47
Graphics[Transpose[{{Red, Green, Blue}, SSSTriangle[2, 2, 3] /.
Triangle[li_] :> Line /@ Partition[Append[li, li[[1]]], 2, 1] }]]

• Vastly simpler: Graphics[Riffle[{Red, Green, Blue}, Map[Line, Partition[#, 2, 1, 1] & @@ SSSTriangle[2, 2, 3]]]] Jun 28, 2016 at 23:30