wolfram-mathematica - 如何在几个Graphics3D对象中选择一个并在Mathematica中更改其坐标?

标签 wolfram-mathematica

在问题“ Mathematica and MouseListener - developing interactive graphics with Mma”的公认答案中,Sjoerd C de Vries演示了可以选择3D图形中的对象并更改其颜色。

我想知道在Graphics3D中是否有可能(以与上述类似的方式)具有两个或多个对象(例如,两个长方体)来选择一个并更改其坐标(通过移动或其他方式)?

最佳答案

我在这里部分重用了Sjoerd的代码,但也许像这样

DynamicModule[{pos10, pos11 = {0, 0, 0}, 
  pos12 = {0, 0, 0}, pos20, pos21 = {0, 0, 0}, pos22 = {0, 0, 0}}, 
 Graphics3D[{EventHandler[
    Dynamic[{Translate[Cuboid[], pos11]}, ImageSize -> Tiny], 
   {"MouseDown" :> (pos10 = Mean@MousePosition["Graphics3DBoxIntercepts"]),
    "MouseDragged" :> (pos11 = 
      pos12 + Mean@MousePosition["Graphics3DBoxIntercepts"] - pos10),
    "MouseUp" :> (pos12 = pos11)}], 
  EventHandler[
   Dynamic[{Translate[Cuboid[{1, 1, 1}], pos21]}, ImageSize -> Tiny], 
   {"MouseDown" :> (pos20 = Mean@MousePosition["Graphics3DBoxIntercepts"]),
    "MouseDragged" :> (pos21 = 
       pos22 + Mean@MousePosition["Graphics3DBoxIntercepts"] - pos20),
    "MouseUp" :> (pos22 = pos21)}]},
  PlotRange -> {{-3, 3}, {-3, 3}, {-3, 3}}]]


请注意,这只会在一个平面中移动长方体,因此您必须旋转边界框以使其垂直于该平面移动,但是通过添加修饰键来引入第三个尺寸并不难。



编辑

感谢您的评论。这是上面代码的更新版本。在此版本中,如果多维数据集碰巧移到外部,则它们会跳回到边界框内,这样就可以解决多维数据集消失的问题。

DynamicModule[{init, cube, bb, restrict, generate},
 init = {{0, 0, 0}, {2, 1, 0}};
 bb = {{-3, 3}, {-3, 3}, {-3, 3}};
 cube[pt_, scale_] := 
  Translate[Scale[Cuboid[{-1/2, -1/2, -1/2}, {1/2, 1/2, 1/2}], scale], pt];
 restrict[pt_] := MapThread[Min[Max[#1[[1]], #2], #1[[2]]] &, {bb, pt}];
 generate[pos_, scale_] := Module[{mp, pos0, pos1, pos2},
   mp := MousePosition["Graphics3DBoxIntercepts"];
   pos1 = pos;
   EventHandler[
    Dynamic[{cube[pos1, scale]}, ImageSize -> Tiny], 
    {"MouseDown" :> (pos0 = LeastSquares[Transpose[mp], pos1].mp), 
     "MouseDragged" :> 
       ((pos1 = #[[2]] + Projection[pos0 - #[[2]], #[[1]] - #[[2]]]) &@mp),
     "MouseUp" :> (pos1 = restrict[pos1])}]];

 Graphics3D[generate[#, 1] & /@ init, PlotRange -> bb, PlotRangePadding -> .5]
]

关于wolfram-mathematica - 如何在几个Graphics3D对象中选择一个并在Mathematica中更改其坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8397468/

相关文章:

function - Mathematica,从另一个笔记本调用函数

wolfram-mathematica - 将文字或html插入.notebook

wolfram-mathematica - 当 r 在 Mathematica 中是符号时,如何找到函数的 r 阶导数?

wolfram-mathematica - 在 Mathematica 中使用 PatternSequence 和 Cases 来查找峰值

r - glm 和 LogitModelFit 之间的区别

list - Mathematica 中的规则行为和初始列表的长度

wolfram-mathematica - 数学中的网格

wolfram-mathematica - 如何在 Mathematica 中绘制区间值函数?

database - 如何将sqlite数据库导入mathematica

wolfram-mathematica - 如何安全地杀死从内核?