I am trying to show vector of fixed length which is the tangent to a circle which rotates. Any suggestions on how to simplify this ? My question is not about using Manipulate etc but more is there an easier way to compute and draw the tangent ?
x[t_] := Cos[t];
y[t_] := Sin[t];
d[x_, y_] := If[y == 0, 0, -x/y];
tangent[1, 0] = Arrow[{{1, 0}, {1, 1}}];
tangent[-1, 0] = Arrow[{{-1, 0}, {-1, -1}}];
tangent[0, -1] = Arrow[{{0, -1}, {1, -1}}];
tangent[0, 1] = Arrow[{{0, 1}, {1, 1}}];
tangent[x_, y_] =
If[y > 0,
Arrow[{{x, y}, {x - Cos[ArcTan[d[x, y]]],
y - Sin[ArcTan[ d[x, y]]]}}],
Arrow[{{x, y}, {x + Cos[ArcTan[d[x, y]]],
y + Sin[ArcTan[ d[x, y]]]}}]];
Manipulate[
p = ParametricPlot[{x[t], y[t]}, {t, 0, 2 π},
PlotRange -> {-2, 2}];
g = Graphics[{{Blue, Line[{{0, 0}, {x[a], y[a]}}]}, {Red,
tangent[x[a], y[a]]}}];
Show[{p, g}],
{a, 0, 2 π}]
I extended my example above to include a 3D solution using suggested solutions -
Module[{x,y,z,tangent},
x[t_]:=Cos[t];
y[t_]:=Sin[t];
z[t_]:=0;
r ={{-2,2},{-2,2},{-2,2}};
p=ParametricPlot3D[{x[t],y[t],z[t]},{t,0,2π},PlotRange->r,BoxRatios->1];
tangent[x_,y_,z_]:={Red,Arrow[{{x,y,z},{x-y,x+y,z}}]};
Manipulate[
g :=Graphics3D[{Sphere[{x[t],y[t],z[t]},0.2],
{Green,Arrow[{{x[t],y[t],z[t]},{x[t],y[t],1}}]},
{Blue,Arrow[{{0,0,0},{x[t],y[t],z[t]}}]},
tangent[x[t],y[t],0]
},PlotRange->r,BoxRatios->1];
Show[p,g],
{t,0,4π}
]
]
Comments
Post a Comment