如何在某些点之后绘制形状(椭圆形或椭圆形)并计算其面积? [英] How to draw a shape (ellipse or oval) following some points and calculate its area?

查看:184
本文介绍了如何在某些点之后绘制形状(椭圆形或椭圆形)并计算其面积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制树木年轮并计算其面积.但是,我注意到实际上并不是所有的环都具有像圆一样的对称半径.我有4个半径的数据测量值,我想像每个例子一样在每个无线电的每个点后面绘制圆环(或任何类似的形状)(此图是用PowerPoint中的矢量手动完成的):

I am trying to plot rings of trees and calculate their areas. However, I have noticed that in reality not all rings have symmetric radii like a circle. I have data measurements of 4 radii, and I would like to plot rings (or any similar shape) following each point of every radio like this example (this figure was done manually with vectors in PowerPoint):

问题在于,在R中,我仅发现了使用symbols()函数中的circles选项绘制这些环的可能性,并且得到了以下图形:

the problem is that in R I found only the possibility to plot these rings with the circles option from the symbols() function, and I got this graph:

使用此R脚本:

data <- data.frame(
a = c(1,4,5,8, 10),
b = c(1, 3,7,9, 10),
c = c(2, 6, 8, 9 ,10),
d = c(1, 3, 4, 7, 9) )

data$y <- (data$a - data$b)/2 # y position
data$x <- (data$d - data$c)/2 # x position
data$z <- rowMeans(data[,1:4]) # radio length

symbols(x = data$x, y = data$y, circles=data$z, 
        xlim = c(-10, 10)*1.5, ylim = c(-10, 10)*1.5, inches = F, fg = "orange", lwd = 2)

我检查了一些具有椭圆形绘制功能的软件包(elliplotellipseellipseplotcar等),但我不喜欢它们的功能.我对使用这些软件包不感兴趣,相反,我想编写自己的代码.

I have checked some packages with functions to draw ellipses (elliplot, ellipse, ellipseplot, car, etc), but I don't like their functions. I am not interested in use these packages, on the contrary I would like to write an own code.

我的想法是使用我的四个半径的数据值绘制最符合圆环实际形状的形状,它可以是椭圆形,椭圆形等.

My idea is to plot a shape which best meets the real figure of a ring with my data values of the four radii, it can be an ellipse, oval, etc.

使用圆圈,我仅使用一个无线电的数据(在我的示例中为所有半径的平均值). 使用椭圆会更好,因为我可以使用至少两个值,长轴(A + B)和短轴(C + D).但是绘制一个使用四个半径(A,B,C,D)或更大半径的值的形状会很棒.

With a circle I am using only data of one radio (in my example, the mean of all radii). With a ellipse would be better, because I can use at least two values, the major-axis (A+B), and the minor-axis (C+D). But would be great to draw a shape that use the values of four radii (A, B, C, D) or even more radii.

这里一个人画了一个非常漂亮的超椭圆使用一个 R脚本,另外一个吸引了一些椭圆喜欢戒指

Here a guy drew a very nice superellipse using a R script, and another one drew some ellipses likes rings also in R.

但是,我不知道如何使用他们的方法来解决我的特定问题.

However, I don't know how to use their methods to my specific problem.

如果有人知道如何开始在R中绘制至少一个椭圆会很好.但是,如果知道如何使用四个半径的值绘制形状(椭圆形,椭圆形等)并最终计算出它们的面积,那就太好了.

If somebody have idea how to start drawing at least an ellipse in R would be nice. But would be great to know how to draw a shape (oval, ellipse, etc.) using the values of four radii and finally calculate their area.

非常感谢您的帮助或任何指示.

I would appreciate very much your help or any direction to do that.

更新:

感谢@ cuttlefish44的出色回答,这对向我的学生解释树木的生长非常有用.但是,大多数热带树木的形状都很不规则,现在我想知道是否可以用附加的无线电"E"和半径轴在这种位置的不同位置绘制其他形状:

Thanks @cuttlefish44 for your excellent answer, that was very useful to explain tree growth to my students. However, most tropical trees have very irregular shapes and now I am wondering to know if can I draw this other shape with an additional radio "E" and the radii axes at different positions like this scheme:

任何方向对我来说都是非常有用的.

any direction would be very useful for me.

推荐答案

如果A& B在y轴上,C& D在x轴上,不难计算椭圆的参数.我用optim()来获取参数(注意:这种方法有很小的错误,例如2.439826e-12).

If A & B are on y-axis and C & D are on x-axis, it isn't difficult to calculate the parameters of ellipses. I used optim() to get params (Note: this approach has tiny error, such as 2.439826e-12).

 # change all data into xy coordinates and make ring-factor
library(reshape2); library(dplyr)

data <- data.frame(
  a = c(1, 4, 5, 8, 10),
  b = c(1, 3, 7, 9, 10) * -1,
  c = c(2, 6, 8, 9, 10) * -1,
  d = c(1, 3, 4, 7, 9) )

data <- t(data)
colnames(data) <- LETTERS[1:ncol(data)]   # ring-factor
df <- melt(data, value.name = "x")        # change into long-form

df$y <- df$x                              # make xy coordinates
df[df$Var1=="a"|df$Var1=="b", "x"] <- 0
df[df$Var1=="c"|df$Var1=="d", "y"] <- 0

中心坐标ox&的计算oy

calculation of center coordinates, ox & oy

center <- df %>% group_by(Var2) %>% summarize(sum(x)/2, sum(y)/2) %>% as.data.frame()

椭圆参数的计算;半长轴和-minor轴,ra& rb

calculation of parameters of ellipse; semi-major and -minor axis, ra & rb

opt.f <- function(par, subset, center) {     # target function
  ox <- center[[1]]                          # par[1] and par[2] are ra and rb
  oy <- center[[2]]
  x <- subset$x
  y <- subset$y
  sum(abs((x - ox)^2/par[1]^2 + (y - oy)^2/par[2]^2 - 1))   # from ellipse equation
}

lev <- levels(df$Var2)

## search parameters
res <- sapply(1:length(lev), function(a) 
  optim(c(1,1), opt.f, subset = subset(df, Var2 == lev[a]), 
        center = center[a, 2:3], control = list(reltol = 1.0e-12)))

res  # result. you can get detail by res[,1etc]. values are not 0 but much nearly 0

绘图功能(可能有些包装有类似包装)

function to plot (Probably some packages have similar one)

radian <- function(degree) degree/180*pi
plot.ellipse <- function(ox, oy, ra, rb, phi=0, start=0, end=360, length=100, func=lines, ...) {
  theta <- c(seq(radian(start), radian(end), length=length), radian(end))
  if (phi == 0) {
    func(ra*cos(theta)+ox, rb*sin(theta)+oy, ...)
  } else {
    x <- ra*cos(theta)
    y <- rb*sin(theta)
    phi <- radian(phi)
    cosine <- cos(phi)
    sine <- sin(phi)
    func(cosine*x-sine*y+ox, sine*x+cosine*y+oy, ...)
  }
}

plot(0, type="n", xlim=c(-10, 10), ylim =c(-10, 10), asp=1, xlab="x", ylab="y", axes = F)
axis(1, pos=0);axis(2, pos=0, las=2)
points(df$x, df$y)
for(a in 1:length(lev)) plot.ellipse(ox = center[a, 2], oy = center[a, 3], 
                                     ra = res[,a]$par[1], rb = res[,a]$par[2], length=300)

area <- sapply(res[1,], function(a) pi * a[1] * a[2])

这篇关于如何在某些点之后绘制形状(椭圆形或椭圆形)并计算其面积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆