Вот ответ на другой вопрос, а именно нахождение угла, который делит фигуру на 2 равные части. Он не использует вычисление линий, поэтому я решил сделать его вторым ответом, чтобы не потерять первый ответ.
library(rnaturalearth)
library(sp)
library(rgeos)
library(raster)
library(dplyr)
library(swfscMisc)
library(ggplot2)
projection <- '+proj=stere +lat_0=90 +lat_ts=90 +lon_0=-33 +k=0.994 +x_0=2000000 +y_0=2000000 +datum=WGS84 +units=m +no_defs '
Greenland.raw <- ne_countries(scale = "medium", country="Greenland")
# use a different projection to avoid some warnings
Greenland = spTransform(Greenland.raw, CRSobj = projection)
Greenland_cent <- gCentroid(Greenland)
# Make a simpler Greenland shape - expand and contract to smooth out the crinkly edges
SimplerGreenland <- gBuffer(gBuffer(Greenland, width = 1), width=-1)
# Make a line from the shape
SimplerGreenlandLine <- as(SimplerGreenland, 'SpatialLines')
# sample all around the simpler line
PointsAroundGreenland <- spsample(x = SimplerGreenlandLine, n = 1000, type = 'regular')
# find the furthest point from the centroid
distances <- gDistance(PointsAroundGreenland, Greenland_cent, byid = T)
furthestpointfromcentroid <- distances[which.max(distances)]
findArc <- function(angleindegrees, country_centroid, radius) {
startandend = c(90-angleindegrees, 90-angleindegrees+180)
centroid <- as.numeric(coordinates(country_centroid))
arc = circle.polygon(
x = centroid[1],
y = centroid[2],
radius = radius, units = 'km',
poly.type = 'cartesian', sides = 100, by.length = F,
brng.limits = startandend)
arc = arc[!duplicated(arc),]
segment = centroid %>% rbind(arc) %>% rbind(centroid)
p = Polygon(segment)
ps = Polygons(list(p), 1)
sps = SpatialPolygons(list(ps))
crs(sps) = crs(country_centroid)
sps
}
findHalf <- function(angleindegrees, country_centroid, radius, country_shape) {
areaCountry = gArea(country_shape)
sps <- findArc(angleindegrees, country_centroid, radius)
halfCountry <- crop(country_shape, sps)
areaHalfCountry <- gArea(halfCountry)
ratio <- areaHalfCountry / areaCountry
ratio
}
angle <- seq(from = 1, to = 70, by = 1)
lratios <- lapply(angle, function(a) findHalf(a, Greenland_cent, furthestpointfromcentroid, Greenland))
ratios.df <- do.call(rbind.data.frame, lratios)
names(ratios.df) <- 'ratio'
results.df <- data.frame(angle = angle) %>% dplyr::bind_cols(ratios.df)
ggplot(results.df, aes(x=angle, y=ratio)) + geom_line() + ggtitle('Ratio of half country to whole country')
Полученный график выглядит следующим образом.
Угол в 10 градусов и угол около 69 градусов делит Гренландию на две равные области (в предположении 2-й страны).