我正在尝试在图像内的新月周围绘制一个弧形,并遮盖其周围的所有内容。我使用了
draw.arc
包中的 plotrix
函数:
plot(1:10, asp = 1,main="Test draw.arc")
draw.arc(7,4,5, deg1=70, deg2=150, col="gold", lwd=70, lend=1)
这会绘制一条实心(圆形)弧:
我需要做类似的弧线,但是椭圆形的,以便使用 x 和 y 坐标来制作遮罩
我已尝试以下方法,但不起作用:
## Define parameters of the arc.
x = c(2,12)
y = c(5,10)
c (center) = c(5,7)
## Define the angle theta
a = atan2(x[2]-c[2],x[1]-c[1])
b = atan2(y[2]-c[2],y[1]-c[1])
library(numbers)
b = modq(b-a)+a
theta<-seq(a,b,50)
d = (y-x)
r = d/2
x = c[1]+r*cos(theta)
y = c[2]+r*sin(theta)
## Define x and y using "Degrees" .
x = radius * 180 *cos(theta)/pi + x
y = radius * 180 * sin(theta/pi) + y
plot(x, y, type='l')
有任何提示或指导可以使其发挥作用吗?
结果会是这样的:
这是一种通过设置生成椭圆弧的变量来使用
grid
的方法。用于掩盖图像的变量的实际放置和设置是通过反复试验来实现的。您也许能够找到更合适的。
这可以变成一个函数。
library(jpeg)
library(grid)
# read image
arc_image <- readJPEG("path/to/your/arc_image_to_be_masked.jpg")
# display image in grid
grid.raster(arc_image,
x = 20,
y = 20,
height = 100,
just = c("left", "bottom"),
default.units = "mm")
# inputs for ellipse
x = 100 # ellipse centre x coordinate
y = 65 # ellipse centre y coordinate
major_sa = 40 # ellipse major semi axis
minor_sa = 22 # ellipse minor semi axis
start_angle = 0.31 # start angle of ellipse arc in radians
end_angle = 2.90 # end angle of ellipse arc in radians
rot = 0.27 # ellipse angle of rotation in radians
n = 50 # number of sections making up the ellipse arc
# points on ellipse
vec_alpha <- seq(from = start_angle, to = end_angle, length.out = n)
# ellipse x y coordinates
ellipse_coord <-
data.frame(x = major_sa * cos(vec_alpha) * cos(rot) - minor_sa * sin(vec_alpha) * sin(rot) + x,
y = major_sa * cos(vec_alpha) * sin(rot) + minor_sa * sin(vec_alpha) * cos(rot) + y)
# the ellipse arc
# the thickness of the ellipse arc is controlled by the `gpar` argument `lwd`
ellipse_arc <-
grid::grid.lines(x = ellipse_coord$x,
y = ellipse_coord$y,
default.units = "mm",
gp = gpar(col = "green",
lineend = 1,
lwd = 140))