I am new to R and programming in general, so although your framework  
wasn't exactly what I needed, it was a tremendous help in getting what  
I needed.

Here is what I ended up doing:
idloc <- function(xy,tol=0.10,hitcol="red",mispch=19,miscol="red"){
        if (type=="flag") {col="red"}else if (type=="unflag"){col="green"}

tol2 =tol^2
   incoords = cbind(grconvertX(xy[,1],to="inches"),grconvertY(xy[, 
2],to="inches"))
   hit=c()
   hits=c()
   multhit=c()
while(names(dev.cur())=="quartz") {
        ptU = try(locator(1))
        if (class(ptU)!="try-error") {
        pt=c(grconvertX(ptU$x,to='inches'),grconvertY(ptU$y,to="inches"))
        d2= (incoords[,1]-pt[1])^2 + (incoords[,2]-pt[2])^2
        if(any(d2 <tol2)){
                print("clicked")
                hit=c(hit,(1:dim(xy)[1])[d2 <= min(d2)])
                points(xy[hit,1],xy[hit,2],pch="x",col=col)
        }else{
                        print("missed")
                        missed=c(ptU$x,ptU$y)
                        points(ptU,pch=mispch,col="red",type="p")
                        ptU2 = try(locator(1))
                         if (class(ptU)!="try-error") {
                                         
pt2=c(grconvertX(ptU2$x,to='inches'),grconvertY(ptU2$y,to="inches"))
                                        multhit <- 
c(hit,(1:dim(xy)[1])[incoords[,1]>=min(pt[1],pt2[1]) &  
incoords[,1]<=max(pt[1],pt2[1]) & incoords[,2]>=min(pt[2],pt2[2]) &  
incoords[,2]<=(max(pt[2],pt2[2]))])
                                        
                                        
points(xy[multhit,1],xy[multhit,2],pch="x",col=col)
                                        points(ptU2,pch=19,col="white")
                                        points(ptU,pch=19,col="white")
                        }
                        
                        }
                }
                hits <- unique(c(hits,multhit,hit))
        }
                return(list(hit=hits))
        }
type="flag"
xy = cbind(1:10,runif(10))
plot(xy)
test <- idloc(xy)



On Feb 27, 2009, at 9:43 AM, Barry Rowlingson wrote:

> 2009/2/27 Brian Bolt <bb...@kalypsys.com>:
>> awesome.  Thank you very much for the quick response. I think this is
>> exactly what I was looking for.
>
> Here's a basic framework:
>
> `idloc` <-
>  function(xy,n=1, tol=0.25){
>
>    tol2=tol^2
>
>    icoords = cbind(grconvertX(xy[,1],to="inches"),grconvertY(xy[, 
> 2],to="inches"))
>    hit = c()
>    missed = matrix(ncol=2,nrow=0)
>    for(i in 1:n){
>      ptU = locator(1)
>      pt = c(grconvertX(ptU$x,to='inches'),grconvertY(ptU 
> $y,to="inches"))
>
>      d2 = (icoords[,1]-pt[1])^2 + (icoords[,2]-pt[2])^2
>      if (any(d2 < tol2)){
>        print("clicked")
>        hit = c(hit, (1:dim(xy)[1])[d2 < tol2])
>      }else{
>        print("missed")
>        missed=rbind(missed,c(ptU$x,ptU$y))
>      }
>
>    }
>    return(list(hit=hit,missed=missed))
>
>  }
>
> Test:
>
> xy = cbind(1:10,runif(10))
> plot(xy)
> idloc(xy,10)
>
> now click ten times, on points or off points. You get back:
>
> $hit
> [1]  4  6  7 10
>
> $missed
>         [,1]      [,2]
> [1,] 5.698940 0.6835392
> [2,] 6.216171 0.6144229
> [3,] 5.877982 0.5752569
> [4,] 6.773190 0.2895761
> [5,] 7.210847 0.3126149
> [6,] 9.239985 0.5614337
>
> - $hit is the indices of the points you hit (in order, including
> duplicates) and $missed are the coordinates of the misses.
>
> It crashes out if you hit the middle button for the locator, but that
> should be easy enough to fixup. It doesn't label hit points, but
> that's also easy enough to do.
>
> Barry


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to