0
$\begingroup$

How do I color edges of a convex polygon, say a square, using interpolation with respect to the color (to form parametric polynomials) in Mathematica, so that the colors displayed on the 4 edges would vary linearly (say we use three colors)?

$\endgroup$
2

1 Answer 1

2
$\begingroup$

You can use VertexColors:

coords = {{1, 2}, {3, 2}, {3, 4}, {1, 4}}; 
colors = {Green, Red, Blue, Orange};
verticesandcolors = Transpose /@ 
  Partition[Transpose[Join[#, {#[[1]]}] & /@ {coords, colors}], 2, 1];
Graphics[{Thickness[.03], Line[#, VertexColors -> #2] & @@@ verticesandcolors, 
  FaceForm[], EdgeForm[Black], Polygon@coords}]

enter image description here

Alternatively, you can construct a BSplineFunction from coords and use it with ParametricPlot with a custom ColorFunction:

bsF = BSplineFunction[coords, SplineDegree -> 1, SplineClosed -> True];
cF = Blend[Transpose[{Range[0, 1, 1/4], Join[colors, {colors[[1]]}]}], #] &;
ParametricPlot[bsF[u], {u, 0, 1}, Axes -> False, 
  PlotStyle -> Directive[{JoinForm["Round"], CapForm["Round"], Thickness[.05]}],  
  ColorFunction -> (cF[#3] &), Epilog -> {FaceForm[], EdgeForm[Black], Polygon[coords]}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Can the function "Manipulate" be incorporated in a way that is consistent with the question described? $\endgroup$ Oct 21, 2018 at 17:00
  • $\begingroup$ @ChonglinZhu, what would you like to control in Manipulate? Colors, coordinates...? $\endgroup$
    – kglr
    Oct 21, 2018 at 17:03
  • $\begingroup$ the color and the parameter goes from 0 to 1, I saw my tutor did it $\endgroup$ Oct 21, 2018 at 18:21

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.