如何将定位器限制在一组有限的(但不是常规的)位置上? [英] How can I constrain locators to a limited (but not regular) set of positions?

查看:45
本文介绍了如何将定位器限制在一组有限的(但不是常规的)位置上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Mathematica 中,定位器可以通过 LocatorPane 的参数限制在某些屏幕区域(参见 LocatorPane 文档.)

In Mathematica, locators can be constrained to certain screen regions via the parameters of LocatorPane (See LocatorPane documentation.)

三个有序对的列表 {{{minX, minY}, {maxX, maxY}, {dX, dY}}} 通常是确定定位器行为的关键.{minX, minY}{maxX, maxY} 设置区域.{dX, dY} 设置跳跃大小:0 表示无限制,任何其他正数表示每跳的大小.

A list of three ordered pairs {{{minX, minY}, {maxX, maxY}, {dX, dY}}} is usually the key to determining the behavior of locators. {minX, minY} and {maxX, maxY} set the region. {dX, dY} sets the jump size: zero for unrestrained, any other positive number for the size of each hop.

在下面的代码中,{{{-.9, 0}, {1, 0}, {0, 0}}} 为定位器设置区域和跳转pts.前两个有序对将定位器限制在数轴上的区间 [-9, 1] 内.有序对 {0, 0} 不对任何一个定位器施加额外的约束.但是,由于 y 值只能为零,由于前两项定义的区域,两个定位器都不能自由离开 x 轴.

In the code below, {{{-.9, 0}, {1, 0}, {0, 0}}} sets the region and jumps for the locator pts. The first two ordered pairs limit the locators to the interval [-9, 1] on the number line. The ordered pair {0, 0} imposes no additional constraints on either of the locators. However, because the y values can only be zero, due to the region defined by the first two items, neither locator is free to leave the x-axis.

我想将每个定位器限制为 myTicks 中的 x 值.(在完整的程序中,myTicks 会随着时间的推移而改变,这取决于用户所做的决定.)因为刻度不是沿着 x 均匀分布的,所以这个问题不能通过为 x 跳跃设置一个常数值来解决.如果该值考虑到定位器的当前位置,则下一个左跳可能与右跳不同.

I'd like to confine each locator to x-values in myTicks. (In the full program, myTicks will change over time depending on decisions made by the user.) Because the ticks are not uniformly spaced along x, the issue cannot be solved by setting a constant value for the x-jump. And if the value were take into account the current position of the locator, the next left hop might be different size than the right hop.

myTicks = {-.9, 0, .1, .2, .45, .79, 1};
pts = {{.25, 0}, {.75, 0}};

LocatorPane[Dynamic[pts],
  Graphics[{}, 
    Axes -> {True, False}, 
    PlotLabel -> Row[{"locators at: " , Dynamic[pts[[1, 1]]], " and ", 
       Dynamic[pts[[2, 1]]]}], 
    Ticks -> {myTicks, Automatic}],
{{{-.9, 0}, {1, 0}, {0, 0}}}]

任何建议将不胜感激!

推荐答案

这似乎有效.

myTicks = {-.9, 0, .1, .2, .45, .79, 1};

DynamicModule[{p = {.25, 0}, p2 = {.75, 0}},
 LocatorPane[Dynamic[{p, p2}], 
  Graphics[{}, Axes -> {True, False}, 
   PlotLabel -> 
    Row[{"locators at: ", 
      Dynamic[p[[1]] = Nearest[myTicks, p[[1]]][[1]]], " and ", 
      Dynamic[p2[[1]] = Nearest[myTicks, p2[[1]]][[1]]]}], 
   Ticks -> {myTicks, Automatic}], {{{-.9, 0}, {1, 0}}}, ContinuousAction -> False]
]

这篇关于如何将定位器限制在一组有限的(但不是常规的)位置上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆