我正在尝试找到一种在Mathematica中绘制钟面的通用解决方案,如图形对象。我已经实现了自己的版本,但我认为必须有更好的解决方案。我们赞赏一个代码较少或思路过程更清晰的整洁版本。
我的版本:
radius = 1;
elementList =
Join[Table[i, {i, 3, 1, -1}], Table[i, {i, 12, 4, -1}]];
elementNumber = Length[elementList];
thetaList = Table[i, {i, 0, 2 Pi, 2 Pi/elementNumber}][[1 ;; 12]];
coordinateList = Map[{radius*Cos[#], radius*Sin[#]} &, thetaList];
objectList =
Map[Style[#, FontFamily -> "Georgia", FontSize -> 30] &,
elementList];
Graphics[
Join[
MapThread[Text[#1, #2] &, {objectList, coordinateList}],
{Circle[{0, 0}, 1.2*radius]}
]
]
答案 0 :(得分:11)
这是制作时钟的一种方法:
clockFace = Import["http://i.imgur.com/ufanv.jpg"];
{hour, minute, second} = Take[Date[], -3];
hour = Mod[hour, 12] + minute/60.;
Graphics3D[
{
{Texture[clockFace],
Polygon[{{-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0}},
VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}
]
},
{Black, AbsoluteThickness[8],
Line[{{0, 0, 0},
.55 {Cos[Pi/2 - 2 Pi hour/12], Sin[Pi/2 - 2 Pi hour/12], 0}}
]
},
{Black, AbsoluteThickness[5],
Line[{{0, 0, 0},
.8 {Cos[Pi/2 - 2 Pi minute/60], Sin[Pi/2 - 2 Pi minute/60], 0}}
]
}
},
Boxed -> False, Lighting -> "Neutral"]
<强>加成强>
这是一个旋转的旋转3D时钟,供您娱乐:
clockFace = Import["http://i.imgur.com/ufanv.jpg"];
vtc = VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
hand[thickness_, radius_, time_] := {AbsoluteThickness[thickness],
Line[{{0, 0, -1}, {radius Cos[Pi/2 + 2 Pi time],
radius Sin[Pi/2 + 2 Pi time], -1}}],
Line[{{0, 0, 1}, {radius Cos[Pi/2 - 2 Pi time],
radius Sin[Pi/2 - 2 Pi time], 1}}],
Line[{{0, -1, 0}, {radius Cos[Pi/2 - 2 Pi time], -1,
radius Sin[Pi/2 - 2 Pi time]}}],
Line[{{0, 1, 0}, {radius Cos[Pi/2 + 2 Pi time], 1,
radius Sin[Pi/2 + 2 Pi time]}}],
Line[{{-1, 0, 0}, {-1, radius Cos[Pi/2 + 2 Pi time],
radius Sin[Pi/2 + 2 Pi time]}}],
Line[{{1, 0, 0}, {1, radius Cos[Pi/2 - 2 Pi time],
radius Sin[Pi/2 - 2 Pi time]}}]};
Dynamic[
{hour, minute, second} = Take[Date[], -3];
hour = Mod[hour, 12] + minute/60.;
Graphics3D[{
{Texture[clockFace],
Polygon[{{1, -1, -1}, {-1, -1, -1}, {-1, 1, -1}, {1, 1, -1}},
vtc],
Polygon[{{-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1}}, vtc],
Polygon[{{-1, 1, -1}, {-1, -1, -1}, {-1, -1, 1}, {-1, 1, 1}},
vtc], Polygon[{{1, -1, -1}, {1, 1, -1}, {1, 1, 1}, {1, -1, 1}},
vtc], Polygon[{{-1, -1, -1}, {1, -1, -1}, {1, -1, 1}, {-1, -1,
1}}, vtc],
Polygon[{{1, 1, -1}, {-1, 1, -1}, {-1, 1, 1}, {1, 1, 1}}, vtc]
}, {Black,
hand[8, .55, hour/12],
hand[5, .8, minute/60],
hand[3, .8, second/60]
}
},
Boxed -> False, Lighting -> "Neutral",
ViewPoint ->
5 {Cos[2 Pi second/60], Sin[2 Pi second/60],
Sin[2 Pi second/30]}, SphericalRegion -> True,
Background -> Black, ImageSize -> Full]] // Deploy
答案 1 :(得分:6)
这里是一个功能版本,它推广了钟面的生成,使您可以轻松更改数字的样式,&#34;小时&#34;的数量,以及面部的半径:
Options[clockFace] = {FontFamily -> "Georgia", FontSize -> 30};
clockFace[hours_Integer, radius_?NumericQ, opts : OptionsPattern[]] /;
hours > 0 && Im[radius] == 0 && radius > 0 :=
With[{range = Range[12]},
With[{objects =
Style[#,
FilterRules[{opts}, Options[Style]] ~Join~ Options[clockFace]] & /@ range,
thetas = Pi/2 - 2 Pi*range/hours},
Graphics[Append[
MapThread[Text[#1, {Cos[#2], Sin[#2]}] &, {objects, thetas}],
Circle[radius*1.2]]]]]
有些事情只是Mathematica风格问题;例如,
FilterRules[{opts}, Options[Style]] ~Join~ Options[clockFace]
只是将相关可选参数传递给Style
的方法,同时确保在相关时使用clockFace
的默认值,因为Mathematica将使用第一个适用的规则查找规则列表(功能选项只是规则列表)。我还使用With
来命名,这就是嵌套的原因;其他人可能更喜欢使用单个Module
。无论哪种方式,总是最好尽可能地制作局部变量。
但最大的变化是按顺序生成数字列表,使用Range
,然后调整thetas
的定义,以便所有内容都在正确的位置结束。我认为看到发生了什么事情要容易得多,因为减号意味着你顺时针移动并靠Pi/2
抵消,这清楚地说明你是从哪里开始的时间的顶部。
答案 2 :(得分:5)
以下是一个正在工作的3D时钟,它可以轻松地将@Arnoud的答案与Christopher's blog entry结合起来:
makeHand[fl_, bl_, fw_, bw_] :=
Polygon[{{-bw, -bl, 0.1}, {bw, -bl, 0.1}, {fw, fl, 0.1},
{0, fl + 8 fw, 0.1}, {-fw, fl, 0.1}}/9];
hourHand = makeHand[5, 5/3, .1, .3];
minuteHand = makeHand[7, 7/3, .1, .3];
secondHand = {Red, EdgeForm[Black], makeHand[7, 7/3, .1/2, .3/2]};
clockFace = Import["http://i.imgur.com/ufanv.jpg"];
Graphics3D[{
{Texture[clockFace],
Polygon[{{-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0}},
VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]},
Rotate[hourHand, Dynamic[Refresh[-30 Mod[AbsoluteTime[]/3600, 60] \[Degree],
UpdateInterval -> 60]], {0, 0, 1}],
Rotate[minuteHand, Dynamic[Refresh[-6 Mod[AbsoluteTime[]/60, 60] \[Degree],
UpdateInterval -> 1]], {0, 0, 1}],
Rotate[secondHand, Dynamic[Refresh[-6 Mod[AbsoluteTime[], 60] \[Degree],
UpdateInterval -> 1/20]], {0, 0, 1}]}, Boxed -> False]
修改强>
通过在计划任务中使用Rasterize []来捕获动画!
a = Graphics3D[(* etc etc*)];
b = {};
t = CreateScheduledTask[AppendTo[b, Rasterize@a], {2, 30}];
StartScheduledTask[t];
While[MatchQ[ScheduledTasks[], {ScheduledTaskObject[_, _, _, _,True]}],Pause[1]];
RemoveScheduledTask[ScheduledTasks[]];
Export["c:\\test.gif", b, "DisplayDurations" -> 1]
答案 3 :(得分:5)
答案 4 :(得分:3)
你的方法很好。这有点乱。以下是我的解释:
hours = 12;
radius = 1;
thetaList = Rest@Range[2 Pi, 0, -2 Pi/hours] + Pi/2;
coordinateList = radius {Cos@#, Sin@#} & /@ thetaList;
Graphics[{
FontFamily -> "Georgia",
FontSize -> 30,
Text ~MapThread~ {Range@hours, coordinateList},
Circle[{0, 0}, 1.2 radius]
}]