I am working on Day 3 part1 of Advent of Code 2019, which involves two wires twisting and turning.
There are two parts to my question:
If I have a data frame with the coordinates where the wire passes and takes a turn (like snake and ladders), how do I form a line? Is there a function I could use?
This is the code I've tried, but to be frank I'm not sure whether it works or not
lines(path1$x, path1$y)
where path1 is the data frame containing those coordinates
After I have formed the two lines, how can I get a data frame containing the points of intersection of those lines?
I have tried the intersect() function which clearly does not work.
Since there is not example data from your side it's difficult to build a solution that fits your example. However, a simple sf
example can exemplify what you're after.
You can create line objects with st_linestring
and check their intersection with st_intersection
. Below is a simple example:
library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.
# Line one has a dot located in (1, 2) and a dot located in (3, 4) connected.
f_line <- st_linestring(rbind(c(1, 2), c(3, 4)))
# Line two has a dot located in (3, 1) and a dot located in (2.5, 4) connected.
s_line <- st_linestring(rbind(c(3, 1), c(2.5, 4)))
# You can see their intersection here
plot(f_line, reset = FALSE)
plot(s_line, add = TRUE)
# sf has the function st_intersection which gives you the intersection
# 'coordinates' between the two lines
st_intersection(s_line, f_line)
#> POINT (2.571429 3.571429)
For your example, you would need to transform your coordinates into an sf
object and use st_intersection