6
$\begingroup$

Consider the following triangle.

Graphics[{EdgeForm[Directive[Thick, Red]], Gray, Triangle[]}]

The EdgeForm is defining the edges style for all three edges of the triangle.

How can I give separate Directive for each edge of the Triangle.

$\endgroup$
3
  • 1
    $\begingroup$ By using Line for each edge of the triangle separately. $\endgroup$ Feb 19, 2019 at 15:57
  • 2
    $\begingroup$ You mean have three separate Line objects that connect to give an illusion of a Triangle? $\endgroup$
    – user13892
    Feb 19, 2019 at 15:59
  • 1
    $\begingroup$ Yes. And in order to obtain a filled triangle, you may use a true Triangle object with specified FaceForm and EdgeForm[None]; the Line objects have to appear in the list afterwards (so that the Line objects are plotted after the Triangle object). $\endgroup$ Feb 19, 2019 at 16:03

1 Answer 1

8
$\begingroup$

You can construct lines from the input Triangle and add them to your list of primitives in Graphics:

ClearAll[triangleToLines]
triangleToLines[Triangle[a_]] := Line /@ Partition[Append[a, a[[1]]], 2, 1]

Examples:

Graphics[{EdgeForm[Directive[Thick]], Gray, Triangle[], 
  Opacity[.5], CapForm["Round"], Thickness[.05], 
  Thread[{{Red, Green, Blue}, triangleToLines[Triangle[]] }]}]

enter image description here

Graphics[{EdgeForm[Directive[Thick]], Gray, Triangle[],
  Opacity[.5], CapForm["Round"], Thickness[.05], 
  MapThread[# /. Line[x_] :> Line[x, VertexColors -> #2] &, {triangleToLines[
     Triangle[]] , Partition[{Red, Green, Blue}, 2, 1, 1]}]}]

enter image description here

Graphics[{EdgeForm[Directive[Thick]], Gray, Triangle[{{0, 0}, {1, 1}, {2, 0}}], 
  Opacity[.5], CapForm["Round"], Thickness[.05], 
  Thread[{{Red, Green, Blue}, triangleToLines[Triangle[{{0, 0}, {1, 1}, {2, 0}}]] }]}]

enter image description here

t3d = Triangle[{{0, 0, 0}, {1, 0, 0}, {0, 1, 1}}]; 
Graphics3D[{EdgeForm[Directive[Thick]], Gray, t3d, 
  Opacity[.5], CapForm["Round"], Thickness[.05], 
  Thread[{{Red, Green, Blue}, Tube[#, .1] & @@@ triangleToLines[t3d] }]}]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ +1 The definition for triangleToLines[Triangle[]] is unnecessary. It is handled by the general case. $\endgroup$
    – Bob Hanlon
    Feb 20, 2019 at 1:52
  • $\begingroup$ Thank you @BobHanlon. Excellent point!! $\endgroup$
    – kglr
    Feb 20, 2019 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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