[R] xsplineGrob() in grid: Find the point(s) two curves intersect

Paul Murrell p@u| @end|ng |rom @t@t@@uck|@nd@@c@nz
Mon Aug 11 00:42:18 CEST 2025


Hi

The 'sf' package can calculate the intersection ...

library(grid)
x1 <- c(0.25, 0.25, 0.75, 0.75)
x2 <- c(0.05, 0.15, 0.25, 0.35)
y1 <- c(0.25, 0.75, 0.75, 0.25)
a <- xsplineGrob(x1, y1, shape = 1, gp=gpar(col="grey"))
b <- xsplineGrob(x2, y1, shape = .5, gp=gpar(col="grey"))

## Draw the lines to check that the answer is correct
grid.newpage()
grid.draw(a)
grid.draw(b)

## Use 'sf' to find point of intersection
library(sf)
aPts <- xsplinePoints(a)
bPts <- xsplinePoints(b)
aLine <- st_linestring(cbind(aPts$x, aPts$y))
bLine <- st_linestring(cbind(bPts$x, bPts$y))
int <- st_intersection(aLine, bLine)

## Draw the intersection
grid.circle(int[1], int[2], default.units="in",
             r=unit(1, "mm"), gp=gpar(fill="black"))

Hope that helps

Paul

On 9/08/25 06:36, Jeff Newmiller via R-help wrote:
> "Spline" is a class of curve generating techniques. The grid library implements two of these techniques to interpolate a curve through some points (b-spline and x-spline, differing primarily in the control variables). As a graphics library it has a focus on graphical output, though it does export the xsplinePoints() function with which you can approximate the curve with short line segments and proceed to do your math starting from there. There are some options discussed here [1].
> 
> [1] <https://stackoverflow.com/questions/68786700/calculate-2d-spline-path-in-r-with-customizable-number-of-interpolation-points>
> 
> On August 8, 2025 7:16:03 AM PDT, Shu Fai Cheung <shufai.cheung using gmail.com> wrote:
>> Hi All,
>>
>> I am new to the grid package. Please pardon me for asking a question
>> that may be a trivial one.
>>
>> If two curves created by grid::xsplineGrob() intersect, is there any
>> existing function that can find this/these point(s)?
>>
>> This is an example:
>>
>> library(grid)
>>
>> x1 <- c(0.25, 0.25, 0.75, 0.75)
>> x2 <- c(0.05, 0.15, 0.25, 0.35)
>> y1 <- c(0.25, 0.75, 0.75, 0.25)
>>
>> a <- xsplineGrob(x1, y1, shape = 1)
>> b <- xsplineGrob(x2, y1, shape = .5)
>>
>> grid.draw(a)
>> grid.draw(b)
>>
>> Specifically, I would like to find these points without drawing the
>> two curves using grid.draw().
>>
>> I found methods to find this/these point(s) on the internet. But this
>> is a frequently asked question, so I guess maybe there are existing
>> functions/packages that can do this. However, I could not yet find
>> any.
>>
>> Regards,
>> Shu Fai Cheung
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Dr Paul Murrell (he/him)
Te Kura Tatauranga | Department of Statistics
Waipapa Taumata Rau | The University of Auckland
Private Bag 92019, Auckland 1142, New Zealand
64 9 3737599 x85392
paul using stat.auckland.ac.nz
www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list