数学
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/