python - Unix : Getting Mouse -coordinates over X like the Mathematica?

标签 python r language-agnostic wolfram-mathematica x11

数学

DynamicModule[{list = {}}, 
 EventHandler[
  Dynamic[Framed@
    Graphics[{BSplineCurve[list], Red, Line[list], Point[list]}, 
     PlotRange -> 2]], {{"MouseClicked", 
     1} :> {AppendTo[list, 
      MousePosition["Graphics"]]}}, {"MouseClicked", 2} :> 
   Print[list]]]

我想在没有 Mathematica 的家里完成上述操作。使用您想要的任何工具,我喜欢使用 Python 和 R,但对任何候选解决方案都很满意。我首先想到的是 RStudio 和这个问题 here但我不确定是否有更好的方法来做到这一点。

我如何在 X 上进行交互式 GUI 创新?

Mathematica 的过程 - 概述的片段

1. you click points

2. you will see BSplineCurve formating between the points and points are red

3. points are saved to an array

4. when finished, you click `right-mouse-button` so array to stdout

最佳答案

这是一个 R 函数,可以执行您描述的操作:

dynmodfunc <- function() {
    plot(0:1,0:1,ann=FALSE,type='n')
    mypoints <- matrix(ncol=2, nrow=0)
    while( length(p <- locator(1, type='p', col='red')) ) {
        mypoints <- rbind(mypoints, unlist(p))
        plot(mypoints, col='red', ann=FALSE, xlim=0:1, ylim=0:1)
        if(nrow(mypoints)>1) {
            xspline(mypoints, shape=-1)
        }
    }
    mypoints
}

(out <- dynmodfunc())

您可以将 shape 参数更改为 xspline 以更改样条曲线的样式。此版本返回一个包含 x 和 y 值的 2 列矩阵,但如果愿意,可以将其更改为其他结构。还有很多其他的东西也可以定制。

添加了将输出粘贴到 Mathematica 中的函数:

matrix2mathematica <- function(x) {
    paste0( '{', 
        paste0( '{', x[,1], ', ', x[,2], '}', collapse=', '),
    '}')
}

cat( matrix2mathematica(out))

关于python - Unix : Getting Mouse -coordinates over X like the Mathematica?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12078575/

相关文章:

python - SQLAlchemy - 错误 TVP 的行必须是 Sequence 对象

正则表达式上的 Python 拆分字符串

减少 R 中函数的额外参数

string - 大写与小写

model-view-controller - MVC 的替代品是什么?

actionscript-3 - 如何避免使用接口(interface)重用相同的实现代码?

python - 多服务器生产环境中的django-celery

python - 使用 Pandas 从每组中随机选择一行

R 编程 - 子集时间特定数据

r - 将时间对象转换为 R 中的分类(早上、下午、晚上、晚上)变量?