How to find boundary for set of points in R? - r

I have a data set of XYZ coordinates (lake bathymetry) and I want to create a grid out of them for later meshing purposes using GMSH. I know that there is an easy way to do it in MATLAB using the boundary(x,y,z) function. Unfortunately, I couln't find anything similar in R and I have too many points to create boundary lines by hand.
Does anyone has an idea how to proceed with this in R? At the end I need the lines consisting of the outer points in my point cloud.
Thanks a lot!

You can try using chull which finds the convex hull.
From the chull help
X <- matrix(stats::rnorm(2000), ncol = 2)
chull(X)
plot(X[,1],X[,2])
points(X[chull(X),1],X[chull(X),2],col=2,pch=19)

Related

Creating a ternary plot from Cartesian coordinates

I am trying something - I want to estimate contributions from sources via a mixing triangle.
I have all data points in the Cartesian system (2 tracers measured)
Now I need to transform my coordinates into trilinear coordinates.
I was looking for a pre-existing function for that or another way to compute it on my own.
Can someone help me? Is there something existing already in R?
ggtern needs those trilinear coordinates if I did not misunderstood the manual.
Thanks,
Nadine

r alphahull post-processing/ avoid two hulls

I have got a table with coordinates of points and want to get the smalest polygon around them. I tried different functions and so far alphahull works best for my purposes. My major interest is in the area of the hull. I have got approximately 3500 datasets, so I have to find a reliable method for my analysis.
I analysed some datasets and realised that in some cases I get a hull in a hull and areahull() is not able to return an area. A higher alpha-value would avoid this but would overestimate my area by far.
Is there a possibility to post-process my alpha-hull to remove the second hull? Or a better method to get the size of the area?
library(alphahull)
tmp <- ahull(path.points.1$x, path.points.1$y, alpha = 50)
plot(tmp, wpoints = F)
lin to example dataset
I found a solution which seems to work for my purposes: the function ahull_track() returns only the boundary as a geom_path()-object. the coordinates of the single boundary segments are stored in a list. unfortunately they are not in the correct order, so it is no straight-forward solution. I had to write a function which rearranges the segments into the correct order and generates a polygon.

Find nearest point of two 3D lines that do not intersect using R

I am performing an Xray investigation where I want to find coordinates for radioopaque markers in a 3D-space. Using a C-arm, I acquire two images of the same marker, one from bottom-to-top and one from the side.
From the Xray source (P1 in bottom-top, P3 in side image), the marker projects to the image I get on the image intensifier ("my registrations", giving me P2 and P4, respectively) for each of the both projections . Points are given in (x,y,z) format.
I would like to define lines P1-P2 and P3-P4 and find the point where they intersect.
Is this as simple as using the following or does the lm() operation not work in 3D?
P1 <- c(0,50,50)
P2 <- c(0,-50,50)
fit <- lm(P1~P2)
Because of inaccuracies turning the Xray equipment, it might be possible that there is no point of intersection, so I would need to approximate it by finding the point with the least distance to both lines.
As you can probably tell from my formulations I have some very basic understanding of R but I am no Maths/Programming/Statistics ace, so any help finding line equations to work with in order to find their intersect (or nearest approximation of that) would be highly appreciated.
Thanks in advance!

Set up a (igraph) graph using the XY co-ordinates for nodes

I am trying to create an igraph graph using the co-ordinates of nodes. I realized that there are no direct ways to do this through the igraph package for R.
There are ways to plot a graph on a 2D space using the layout() function, but I need to create a graph from known co-ordinates of nodes. I'll appreciate any help anyone can provide with this.
Why I'm trying to set up a graph this way? After parametrizing the graph nodes with co-ordinates, I want to connect the nodes using a probability measure that takes the distance between the nodes into account.
Thanks.
Hitaysh
# Initial Co-ordinates of nodes
n = 1000 # no. of nodes
nodes.coord <- data.frame(x=runif(n,min=0,max=n),
y=runif(n,min=0,max=n))
# Set up a graph with nodes on above co-ordinates...
P.S. This is my first post on StackOverflow. Any constructive feedback on how to ask questions better, is also welcome.
After reading your question afew more times, i'm guessing that something like this will work
n = 20 # no. of nodes
set.seed(15)
nodes.coord <- data.frame(
x=runif(n,min=0,max=n),
y=runif(n,min=0,max=n)
)
gg <- graph.empty(n)
plot(gg, layout=as.matrix(nodes.coord[,c("x","y")]))
But if you are going to connect nodes based on distance, it probably makes sense to find the connections prior to creating the igraph object since you already know the locations.

Points of intersection in R

This might be a stupid question...
I've two vectors containing Y coords
X is simply 1:8
I've added the lines to a plot area and would like to identify the point of intersection of the two lines.
Is there a default package to find this?
Thanks
The intersection of two lines is found analytically by solving the two linear equations which define the lines. I'll leave the math as an exercise to the OP.
If you want to use a very helpful and powerful package, you could read up on the package spatstat . Once you've converted your lines into psp objects, use spatstat:crossing.psp to find intersections.

Resources