如何获得轮廓为R的相交直线的坐标

如何获得轮廓为R的相交直线的坐标,r,sp,sf,R,Sp,Sf,我有一个从网上上传的形状。根据计算出的质心,我想从中画出一条50度的线,找到他与轮廓相交的坐标。知道我该怎么做吗 谢谢 脚本: library(ggplot2) df = read.table("E:/cloud1.txt") #stored at https://ufile.io/zq679 colnames(df)<- c("x","y") meanX <- mean(df$x) meanY <- mean(df$y) ggplot(df, aes(x, y))+

我有一个从网上上传的形状。根据计算出的质心,我想从中画出一条50度的线,找到他与轮廓相交的坐标。知道我该怎么做吗

谢谢

脚本:

library(ggplot2)

df = read.table("E:/cloud1.txt")  #stored at https://ufile.io/zq679
colnames(df)<- c("x","y")

meanX <- mean(df$x)
meanY <- mean(df$y)

ggplot(df, aes(x, y))+ geom_point()+ geom_point(aes(meanX, meanY),colour="green",size=2)
库(ggplot2)
df=read.table(“E:/cloud1.txt”)#存储在https://ufile.io/zq679

colnames(df)下面是一个使用
sf
的解决方案:

df <- read.table("~/Bureau/cloud1.txt")  #stored at https://ufile.io/zq679
colnames(df) <- c("x","y")
meanX <- mean(df$x)
meanY <- mean(df$y)


# Transform your data.frame in a sf polygon (the first and last points
# must have the same coordinates)
library(sf)
poly <- st_sf(st_sfc(st_polygon(list(as.matrix(df)))))

# Choose the angle (in degrees)
angle <- 50

# Find the minimum length for the line segment to be always 
# outside the cloud whatever the choosen angle
maxX <- max(abs(abs(df[,"x"]) - abs(meanX)))
maxY <- max(abs(abs(df[,"y"]) - abs(meanY)))
line_length = sqrt(maxX^2 + maxY^2) + 1

# Find the coordinates of the 2 points to draw a line with 
# the intended angle.
# This is the gray line on the graph below
line <- rbind(c(meanX,meanY), 
              c(meanX + line_length * cos((pi/180)*angle), 
                meanY + line_length * sin((pi/180)*angle)))
# Transform into a sf line object
line <- st_sf(st_sfc(st_linestring(line)))

# Intersect the polygon and line. The result is a two points line
# shown in black on the plot below
intersect_line <- st_intersection(poly, line)

# Extract only the second point of this line.
# This is the intersecting point
intersect_point <- st_coordinates(intersect_line)[2,c("X","Y")] 

# Visualise this with ggplot
# You might need to install the latest github version :
# devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
ggplot() + geom_sf(data=poly, fill = NA) + 
    geom_sf(data=line, color = "gray80", lwd = 3) + 
    geom_sf(data=intersect_line, color = "gray20", lwd = 1) + 
    geom_point(aes(meanX, meanY), colour="orangered", size=2) +
    geom_point(aes(intersect_point["X"], intersect_point["Y"]), 
                   colour="orangered", size=2) +
    theme_bw()

df我已经更新了我的所有包(我使用R3.3.3),我得到了这个错误“error:找不到函数“geom_sf”。我有什么事吗?知道问题出在哪里吗?是的,您需要github提供的最新版本的ggplot2。您可以使用以下命令安装它:
devtools::install\u github(“tidyverse/ggplot2”)
。我已经更新了答案。一个很好很酷的代码。我试图改变角度,检查所有360度可能的交叉点。不幸的是,它不起作用,你知道如何解决它吗?你是对的!它仅适用于0到90°之间的角度。原因可能是我的基本三角学太远了。。。更新的版本应该适用于任何角度…如何删除网格并更改轮廓的颜色?