如何在三角形的每一侧制作正方形,而又不用硬编码正方形?

发布于 2025-01-21 21:05:37 字数 722 浏览 0 评论 0原文

我有一个data.frame包含4个点,使用GEOM_PATH连接时会创建一个三角形:

library(ggplot2)

triangle = data.frame(x = c(0, 0.5, 1, 0),
                      y = c(0, 0.5, 0, 0))

ggplot(triangle, aes(x, y)) +
        geom_path()

”

现在,我想创建一个新的< code> data.frame (基于三角形),有4分(例如xminxmaxyminymax)从三角形的侧面创建正方形(因此,此data.frame将有3行(对于每个方形)和4列(每个点

。 /renzd.png“ alt =”在此处输入图像描述“>

是否可以在不硬编码正方形的侧面进行操作?

I have a data.frame containing 4 points, which creates a triangle when connected using geom_path:

library(ggplot2)

triangle = data.frame(x = c(0, 0.5, 1, 0),
                      y = c(0, 0.5, 0, 0))

ggplot(triangle, aes(x, y)) +
        geom_path()

enter image description here

Now, I want to create a new data.frame (based on triangle), that has 4 points (e.g. xmin, xmax, ymin, ymax) that creates squares from the sides of the triangle (hence, this data.frame will have 3 rows (for each square) and 4 columns (for each point).

Here is an example:

enter image description here

Is it possible to do it without hard-coding the sides of the squares?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2025-01-28 21:05:37

由于正方形的角度将以角度为单位,因此您可能需要以顶点的x,y坐标为单位的输出。这只需要一点触发。以下函数采用x,y点的向量表示封闭的三角形,并返回两侧正方形顶点的数据框架:

make_squares <- function(x, y) {
  
  x1    <- x[-4]
  x2    <- x[-1]
  xdiff <- (x2 - x1)
    
  y1    <- y[-4]
  y2    <- y[-1]
  ydiff <- (y2 - y1)
  
  lengths <- sqrt(xdiff^2 + ydiff^2)
  angles  <- atan2(ydiff, xdiff)
  
  x3 <- x2 - sin(angles) * lengths
  x4 <- x1 - sin(angles) * lengths
  
  y3 <- y2 + cos(angles) * lengths
  y4 <- y1 + cos(angles) * lengths
  
  
  df <- data.frame(x = round(c(x1, x2, x3, x4, x1), 3),
                   y = round(c(y1, y2, y3, y4, y1), 3),
                   square = rep(1:3, 5))
  `row.names<-`(df[order(df$square),], NULL)
}

输出看起来像这样:

make_squares(triangle$x, triangle$y)
#>       x    y square
#> 1   0.0  0.0      1
#> 2   0.5  0.5      1
#> 3   0.0  1.0      1
#> 4  -0.5  0.5      1
#> 5   0.0  0.0      1
#> 6   0.5  0.5      2
#> 7   1.0  0.0      2
#> 8   1.5  0.5      2
#> 9   1.0  1.0      2
#> 10  0.5  0.5      2
#> 11  1.0  0.0      3
#> 12  0.0  0.0      3
#> 13  0.0 -1.0      3
#> 14  1.0 -1.0      3
#> 15  1.0  0.0      3

您可以在图中使用它:

ggplot(triangle, aes(x, y)) + 
  geom_path() +
  geom_polygon(data = make_squares(triangle$x, triangle$y),
               aes(group = square), fill = "green4", color = "black") +
  coord_equal()

Since the squares will be at an angle, you probably need the output to be in terms of x, y co-ordinates of the vertices. This just requires a bit of trig. The following function takes a vector of x, y points representing a closed triangle and returns a data frame of the vertices of the squares on each side:

make_squares <- function(x, y) {
  
  x1    <- x[-4]
  x2    <- x[-1]
  xdiff <- (x2 - x1)
    
  y1    <- y[-4]
  y2    <- y[-1]
  ydiff <- (y2 - y1)
  
  lengths <- sqrt(xdiff^2 + ydiff^2)
  angles  <- atan2(ydiff, xdiff)
  
  x3 <- x2 - sin(angles) * lengths
  x4 <- x1 - sin(angles) * lengths
  
  y3 <- y2 + cos(angles) * lengths
  y4 <- y1 + cos(angles) * lengths
  
  
  df <- data.frame(x = round(c(x1, x2, x3, x4, x1), 3),
                   y = round(c(y1, y2, y3, y4, y1), 3),
                   square = rep(1:3, 5))
  `row.names<-`(df[order(df$square),], NULL)
}

The output looks like this:

make_squares(triangle$x, triangle$y)
#>       x    y square
#> 1   0.0  0.0      1
#> 2   0.5  0.5      1
#> 3   0.0  1.0      1
#> 4  -0.5  0.5      1
#> 5   0.0  0.0      1
#> 6   0.5  0.5      2
#> 7   1.0  0.0      2
#> 8   1.5  0.5      2
#> 9   1.0  1.0      2
#> 10  0.5  0.5      2
#> 11  1.0  0.0      3
#> 12  0.0  0.0      3
#> 13  0.0 -1.0      3
#> 14  1.0 -1.0      3
#> 15  1.0  0.0      3

And you can use it in your plot like this:

ggplot(triangle, aes(x, y)) + 
  geom_path() +
  geom_polygon(data = make_squares(triangle$x, triangle$y),
               aes(group = square), fill = "green4", color = "black") +
  coord_equal()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文