Unix:像Mathematica一样在X上获得鼠标坐标?

发布于 2021-01-29 15:04:47

Mathematica

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,这里是这个问题但是我不确定是否有更好的方法可以做到这一点。

如何在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
关注者
0
被浏览
71
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    这是执行您所描述的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))
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看