在Mathematica中绘制钟面(寻找更好的解决方案)

Nin*_*ing 10 graphics drawing wolfram-mathematica clock

我试图找到一个通用的解决方案来绘制像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]}
  ]
 ]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Arn*_*ing 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"]
Run Code Online (Sandbox Code Playgroud)

Mathematica生成的漂亮脸蛋的时钟

加成

这是一个旋转的旋转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
Run Code Online (Sandbox Code Playgroud)

3D时钟


Pil*_*lsy 6

这是一个功能的版本,它概括了钟面的生成,以便您轻松更改数字的样式,"小时"的数量和面部的半径:

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]]]]]
Run Code Online (Sandbox Code Playgroud)

有些事情只是Mathematica风格问题; 例如,

FilterRules[{opts}, Options[Style]] ~Join~ Options[clockFace]
Run Code Online (Sandbox Code Playgroud)

只是传递相关可选参数的方法,Style同时确保clockFace在相关时使用默认值,因为Mathematica将使用它在规则列表中找到的第一个适用规则(而函数选项只是规则列表) .我也习惯于With命名,这就是嵌套的原因; 其他人可能更喜欢使用单曲Module.无论哪种方式,最好尽可能使局部变量.

然而,最大的变化是按顺序生成数字列表,使用Range,然后调整定义,thetas以便一切都在正确的地方结束.我认为看到发生的事情要容易得多,因为减号意味着你正在顺时针方向移动并通过偏移来Pi/2明确表示你是从时钟顶部开始的.


Dr.*_*ius 5

以下是一个工作的3D时钟,通过将@Arnoud的答案与Christopher的博客条目轻松组合而成:

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]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑

通过在计划任务中使用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]
Run Code Online (Sandbox Code Playgroud)


C. *_* E. 5

Mathematica有一种称为ClockGauge内置的东西.钟表面造型的可能性是无穷无尽的,如文档中所示.裸骨版本看起来像这样:

ClockGauge[]
Run Code Online (Sandbox Code Playgroud)

钟表

  • 我想几乎所有关于Mma在这个网站上的问题都可以重写,因为大多数都来自v7和v8:D (2认同)