Open lmmx opened 3 years ago
The program can be broken up into 4 sections as follows:
PD = .5
s[t_, f_] := t^.6 - f
t
and f
s
is a function definition with arguments f_
and t_
(suggesting they should be passed as the value of f
and t
)0.6
exponent and then a f
subtracted (recall f
is "basically phase shift")This part is cryptic (as is typical for graphics code, the terseness makes the meaning of individual variables unclear).
At first glance I thought this was an alias of Dt
(which is Mathematica's derivative) but in fact it's a custom function definition dt
, taking 6 arguments:
dt[cl_, ps_, sg_, hf_, dp_, f_] := {
PointSize,
Hue[cl, 1, .6 + sg .4 Sin[hf s[t, f]]],
Point[
{
-sg s[t, f] Sin[s[t, f]],
-sg s[t, f] Cos[s[t, f]],
dp + s[t, f]
}
]
};
I would guess that maybe(!?):
cl_
means "coloured lights" or "christmas lights"ps_
means "phase shift" (but f
already means this so probably not)sg_
means "sin gradient" (perhaps a bad guess)hf_
means "hue frequency" or "hue flicker" (since the colour changes along the curve)dp_
means ??f_
is the argument corresponding to f
which we already know means phase shift...but at this point the trail seems to go cold: I can't figure out what the functions sg
or dp
do (they don't show up searching Mathematica's docs, whereas I expected them to be functions from this code)
The frames are created as "ParallelTable" (which I imagine is like a NumPy NdArray) containing the options for a Graphics3D
representation of a 3D image:
ViewPoint
- viewing positionBoxRatios
- bounding 3D box ratiosViewVertical
- direction to make vertical ViewCenter
- point to display at the centerBoxed
- whether to draw the bounding boxPlotRange
- range of values to includeBackground
- background color for the plotframes = ParallelTable[
Graphics3D[
Table[
{
dt[1, .01, -1, 1, 0, f],
dt[.45, .01, 1, 1, 0, f],
dt[1, .005, -1, 4, .2, f],
dt[.45, .005, 1, 4, .2, f]
},
{
t, 0, 200, PD
}
],
ViewPoint -> Left,
BoxRatios -> {1, 1, 1.3},
ViewVertical -> {0, 0, -1},
ViewCenter -> {{0.5, 0.5, 0.5}, {0.5, 0.55}},
Boxed -> False,
PlotRange -> {{-20, 20}, {-20, 20}, {0, 20}},
Background -> Black
],
{f, 0, 1, .01}
];
Export["tree.gif", frames]
This one's easy, the equivalent in matplotlib is FuncAnimation.save
Oops, I appear to have mixed up the one above which recreates the Javascript tree to the one posted in the replies, the GIF of which I embedded above. The source of this is actually a commenter, Silvia Hao, who writes the following
PD = .5;
s[t_, f_] := t^.6 - f
dt[cl_, ps_, sg_, hf_, dp_, f_, flag_] := Module[
{sv, basePt},
{
PointSize[ps],
sv = s[t, f];
Hue[
cl (1 + Sin[.02 t])/2, 1, .3 + sg .3 Sin[hf sv]
],
basePt = {
-sg s[t, f] Sin[sv],
-sg s[t, f] Cos[sv],
dp + sv
};
Point[basePt],
If[flag,
{
Hue[
cl (1 + Sin[.1 t])/2,
1,
.6 + sg .4 Sin[hf sv]
],
PointSize[RandomReal[.01]],
Point[
basePt + 1/2 RotationTransform[
20 sv,
{
-Cos[sv],
Sin[sv],
0
}
][
{
Sin[sv],
Cos[sv],
0
}
]
]
},
{}]
}]
frames = ParallelTable[
Graphics3D[Table[{
dt[1, .01, -1, 1, 0, f, True], dt[.45, .01, 1, 1, 0, f, True],
dt[1, .005, -1, 4, .2, f, False],
dt[.45, .005, 1, 4, .2, f, False]},
{t, 0, 200, PD}],
ViewPoint -> Left, BoxRatios -> {1, 1, 1.3},
ViewVertical -> {0, 0, -1},
ViewCenter -> {{0.5, 0.5, 0.5}, {0.5, 0.55}}, Boxed -> False,
PlotRange -> {{-20, 20}, {-20, 20}, {0, 20}}, Background -> Black],
{f, 0, 1, .01}];
Once again this can be split out into parts:
PD = .5;
s[t_, f_] := t^.6 - f
This is unchanged from above
dt[cl_, ps_, sg_, hf_, dp_, f_, flag_] := Module[
{sv, basePt},
{PointSize[ps],
sv = s[t, f];
Hue[cl (1 + Sin[.02 t])/2, 1, .3 + sg .3 Sin[hf sv]],
basePt = {-sg s[t, f] Sin[sv], -sg s[t, f] Cos[sv], dp + sv};
Point[basePt],
If[flag,
{Hue[cl (1 + Sin[.1 t])/2, 1, .6 + sg .4 Sin[hf sv]], PointSize[RandomReal[.01]],
Point[basePt + 1/2 RotationTransform[20 sv, {-Cos[sv], Sin[sv], 0}][{Sin[sv], Cos[sv], 0}]]},
{}]
}]
frames = ParallelTable[
Graphics3D[Table[{
dt[1, .01, -1, 1, 0, f, True], dt[.45, .01, 1, 1, 0, f, True],
dt[1, .005, -1, 4, .2, f, False],
dt[.45, .005, 1, 4, .2, f, False]},
{t, 0, 200, PD}],
ViewPoint -> Left, BoxRatios -> {1, 1, 1.3},
ViewVertical -> {0, 0, -1},
ViewCenter -> {{0.5, 0.5, 0.5}, {0.5, 0.55}}, Boxed -> False,
PlotRange -> {{-20, 20}, {-20, 20}, {0, 20}}, Background -> Black],
{f, 0, 1, .01}];
There's one here and an incredible on here (direct link)
The description is as follows:
I noticed that a Reddit post about programming a lighted Christmas Tree from a simple equation
t * Sin(t)
became very popular on Reddit. It is connected to a program a programmer developed. I thought how fast we can make it with Wolfram language ? Here is the result with slight flickering ;-) Note a very special care needs to be paid to the dimming of the lights at a larger distances, and pretty shadowing.
Function
f(t, f)
is rescaling sampling rate of driving parametert
of parametric curve so points are distributed uniformly.f
is basically phase shift.Parameter
PD
is just average distance between pointsThis .GIF file has 100 frames. Enjoy!