Bezier Patches and Surface Curvatures
In[632]:=
ClearAllEvaluate$Context <> "*"
Basic Definition In[633]:=
colorInterpolation[startColor_, endColor_, numCurves_, currentIndex_] :=
Moduler1, g1, b1, r2, g2, b2, fraction, {r1, g1, b1} = startColor;
{r2, g2, b2} = endColor;
(*Calculate the fraction representing the current position between the curves*)
fraction = (currentIndex - 1) (numCurves - 1);
(*Interpolate the color components separately*)
RGBColorr1 + fraction (r2 - r1), g1 + fraction (g2 - g1), b1 + fraction (b2 - b1)
(*Example usage*)
startColor = {1, 0, 0}; (*Red*)
endColor = {0, 1, 0}; (*Blue*)
numCurves = 9; (*Number of curves in the design*)
(*Calculate colors for three curve pieces*)
curveColors = TablecolorInterpolation[startColor, endColor, numCurves, i], i, numCurves Out[637]=
{ , , , , , , , , }
In[638]:=
globalToLocal[a_, b_, u_] := (u - a) (b - a)
In[639]:=
bezierPoints =
{0, 0}, {1, 0} , {2, 0}, {3, 0},
{3, 0}, 4, -5, 5, 5, 6, 0,
{6, 0}, {7, -5}, {8, -3}, 9, 0,
{9, 0}, 10, 3, {11, 0}, 12, 0,
{12, 0}, {14, 0}, {11, 3}, {13, 2},
{{13, 2}, {12, 1}, {15, 1}, {13, 2}},
{14, 3}, 15, 4, 16, 4, 17, 0,
{17, 0}, 18, - 10, -5, 0, {0, 0}
;
Below is very important. It’s the core part: a function that can generate the bezire
curve.
In[640]:=
f[u_] := ModuleindexInbezierPoints = Floor[u] + 1, localVar = u - Floor[u], pts,
pts = bezierPointsindexInbezierPoints; Sumptsi + 1 × BernsteinBasis[3, i, localVar], i, 0, 3
In[641]:=
g1 = ParametricPlotf[t], t, 0, Length@bezierPoints
Out[641]=
5 10 15
-4
-2
2
In[642]:=
bezierLinesGraph =
TableGraphicscurveColorsi, LinebezierPointsi, i, Length[bezierPoints];
In[643]:=
controlPoints = TableGraphicsPointSize[0.02], curveColorsi, PointbezierPointsi,
i, Length@bezierPoints;
In[644]:=
g2 = ParametricPlotf[t], t, 0, Length@bezierPoints, ColorFunction → Function[{x, y, u}, curveColors〚Floor[u] + 1〛], ColorFunctionScaling → False;
2
Plot1
In[645]:=
Show[bezierLinesGraph, g2, controlPoints]
Out[645]=
Plot2
In[646]:=
g1
Out[646]=
5 10 15
-4
-2
2
Plot3 (wait)
In[647]:=
initRec = {-1, 1}, {1, -1};
3
In[648]:=
GraphicsBlue, Disk [{0, 0}, 1.5](*添加蓝⾊填充的Disk*),
Red, Rectangle@@ initRec, Black, Arrow[{{0, 0}, {1, 0}}], Green,
Arrow{0, 0}, {0, 1}, (*添加垂直箭头*)PointSize[0.02], Point[{0, 0}]
Out[648]=
This is very important: The derivative of bazier curve. Ref:
In[649]:=
"https://computergraphics.stackexchange.com/questions/10551/how-to-take-the-derivative-of-
a-b%C3%A9zier-curve#:~:text=We%20obtain%20a%20quadratic%20B,control%20points%20
scaled%20by%20n.";
In[650]:=
firstD[u_] :=
ModuleindexInbezierPoints = Floor[u] + 1, localVar = u - Floor[u], diffs, two,
diffs = Differences /@ bezierPointsindexInbezierPoints; two = 3 Sumdiffsi + 1 × BernsteinBasis2, i, localVar, i, 0, 2;
(*Don't need to convert to a number*)
two〚1〛, two〚2〛
In[651]:=
firstD[0.5]
Out[651]=
{3., 0.}
4
In[652]:=
SecondD[u_] :=
ModuleindexInbezierPoints = Floor[u] + 1, localVar = u - Floor[u], diffs, diffs2, two,
diffs = Differences /@ bezierPointsindexInbezierPoints; diffs2 = Differences /@ diffsindexInbezierPoints; two = 6 Sumdiffsi + 1 × BernsteinBasis1, i, localVar, i, 0, 1;
(*Don't need to convert to a number*)
two〚1〛, two〚2〛
Plot4
In[653]:=
ManipulateModulecurve, tangent, tangentVector, u, perpendicularVector,
translationMatrix, rotationMatrix, movingRectangle, u = uValue;
curve = f[u];
tangent = firstD[u];
tangentVector = tangent;
perpendicularVector = {-tangentVector〚2〛, tangentVector〚1〛}; translationMatrix = TranslationTransform[curve];
rotationMatrix = RotationTransform[{{1, 0}, tangentVector}];
movingRectangle =
GeometricTransformation[Rectangle@@ initRec, translationMatrix.rotationMatrix];
blueDisk = Disk[curve, 1.5];
ShowGraphicsBlue, blueDisk (*添加蓝⾊填充的Disk*), Red,
movingRectangle, Black, Arrowcurve, curve + Normalize[tangentVector], Green, Arrowcurve, curve + Normalize[perpendicularVector], (*添加垂直箭头*)PointSize[0.02], Point[curve], g1,
uValue, 0, "Parameter u", 0, Length@bezierPoints - 0.02,
0.001, Appearance → "Labeled"
Out[653]=
Parameter u 0
Show , g1
5