Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

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

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]]]

I want to do the above at home where I do not have Mathematica. Use whatever tool you want, I like to use Python and R but happy with any solution candidate. The first thing that came to my mind was RStudio and this question here but I am unsure whether some better way to do this.

How can I do the interactive-GUI-innovating over X?

Procedure of the Mathematica -snippet outlined

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is an R function that does what you describe:

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())

You can change the shape argument to xspline to change the style of spline. This version returns a 2 column matrix with the x and y values, but that could be changed to another structure if prefered. There are plenty of other things that could be customized as well.

Added function to get the output to paste into Mathematica:

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

cat( matrix2mathematica(out))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...