# Re: [R] distances between points in R^3

From: baptiste Auguié <ba208_at_exeter.ac.uk>
Date: Mon, 4 Feb 2008 19:10:15 +0000

OK, I've come across a partial answer to the for loop problem in the use of "dist()". However, my problem not only require to find the distances between points, but also to store the vector differences, and the normal to each face when i get the delaunay triangulation sorted.

As i understand, the dist() function calls a C routine, is there a straight-forward way to find / edit / modify it to serve this specific purpose?

Thanks,

baptiste

On 3 Feb 2008, at 18:55, baptiste Auguié wrote:

> Dear R helpers,
>
> I'm trying to write a numerical scheme for a boundary integral
> method to solve an electromagnetic problem. This requires the
> computation of the distance between points at the surface of an
> object (a sphere, in my example). Here is my code,
>
>> require(rgl)
>> r<-1
>> size<-10
>> theta<-seq(0,2*pi,length=size*2)
>> phi<-seq(0,pi,length=size)
>> pc = as.matrix(rbind(expand.grid(theta,phi)))
>> x<- r* sin(pc[,2]) * cos(pc[,1])
>> y<- r* sin(pc[,2]) * sin(pc[,1])
>> z<- r* cos(pc[,2])
>>
>> plot3d(x, y, z, col=rainbow(1000), size=2,zlim=c(-1,1)) #
>> scatterplot of points on a sphere
>>
>> df<- unique(rbind(x,y,z), MARGIN = 2 ) # removes duplicates in
>> cartesian coordinates
>> dimension <- dim(df)[1]
>> matDistances <- array(data=0,dim=c(dimension,dimension))
>>
>> norm <- function(a) sqrt(a %*% a)
>>
>> for (ii in 1:dimension){
>> for (jj in ii:dimension){
>> matDistances[ii,jj]<- norm( df[,ii] - df[,jj])
>> }
>> }
>
>
> This is both inefficient and ugly, I'll welcome any suggestion. In
> particular:
>
> - the location of the points on the sphere is not ideal
> (understand: not uniformly distributed over the area): i looked
> into delaunayn from the geometry package and qhull.com but at best
> I obtained a random set of points on the sphere. It must be a most
> classical problem –– generating a uniform distribution of points on
> a sphere––, but i've set that problem aside for the moment.
>
> - the double for loop over all the points cries out for a
> vectorized solution, but i can't really think of any.
>
>
>
>
> baptiste
> _____________________________
>
> Baptiste Auguié
>
> Physics Department
> University of Exeter
> Exeter, Devon,
> EX4 4QL, UK
>
> Phone: +44 1392 264187
>
> http://newton.ex.ac.uk/research/emag
> http://projects.ex.ac.uk/atto
> ______________________________
>
>
>
>
>

Baptiste Auguié

Physics Department
University of Exeter
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

R-help_at_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. Received on Tue 05 Feb 2008 - 08:07:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Feb 2008 - 09:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.