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)?
1 Answer
$\begingroup$
$\endgroup$
3
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}]
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]}]
-
$\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$– kglrOct 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