三角形,每个顶点都有来自 r 的颜色渐变。

huangapple go评论132阅读模式
英文:

Triangle with a color gradient from each vertex in r

问题

我想要为我的图形制作一个带有自定义颜色渐变的彩色三角形,从三角形的每个顶点开始。示例:

三角形,每个顶点都有来自 r 的颜色渐变。

以下是我目前的代码。颜色是正确的,但我无法让每个首选颜色从顶点开始。目前的三角形看起来像这样:

三角形,每个顶点都有来自 r 的颜色渐变。

我的当前代码:

  1. ## 创建一个单元格坐标的三角形
  2. cells.x <- cells.y <- vector()
  3. k <- 1
  4. asp <- abs(diff(par()$usr[4:3]) / diff(par()$usr[2:1])) * par()$pin[2] / par()$pin[1]
  5. for (i in 1:50) {
  6. for (j in i:(100 - i + 1)) {
  7. cells.x[k] <- 0.1 + (j - 1) * 0.1 / 100
  8. cells.y[k] <- (-1) + (i - 1) * 2 * 0.1 * asp / 100
  9. k <- k + 1
  10. }
  11. }
  12. # 定义渐变的三种颜色
  13. color1 <- "#1e88e5"
  14. color2 <- "#d81b60"
  15. color3 <- "#ffc107"
  16. # 创建三种颜色之间的渐变色函数
  17. color_gradient <- colorRampPalette(c(color1, color2, color3))
  18. # 计算点的数量
  19. num_points <- length(cells.x)
  20. # 基于点的数量生成渐变颜色
  21. gradient_colors <- color_gradient(num_points)
  22. ## 画点
  23. dx <- diff(range(cells.x))
  24. dy <- diff(range(cells.y))
  25. red.x <- max(cells.x)
  26. red.y <- min(cells.y)
  27. red <- 1 - sqrt((((cells.x - red.x) / dx)^2 + ((cells.y - red.y) / dy)^2) / 2)
  28. green.x <- min(cells.x)
  29. green.y <- min(cells.y)
  30. green <- 1 - sqrt((((cells.x - green.x) / dx)^2 + ((cells.y - green.y) / dy)^2) / 2)
  31. blue.x <- min(cells.x)
  32. blue.y <- max(cells.y)
  33. blue <- 1 - sqrt((((cells.x - blue.x) / dx)^2 + ((cells.y - blue.y) / dy)^2) / 2)
  34. points(cells.x, cells.y, col = gradient_colors, pch = 15, cex = 0.5)
英文:

I am looking to make a colored triangle for my plot with a customized color gradient starting from each vertex of the triangle. Example:

三角形,每个顶点都有来自 r 的颜色渐变。

Below is my code so far. The colors are correct but I am unable to have each of the preferred color start from the vertex. The triangle currently looks like that:

三角形,每个顶点都有来自 r 的颜色渐变。

My current code:

  1. ## make a triangle of cell coordinates
  2. cells.x&lt;-cells.y&lt;-vector()
  3. k&lt;-1
  4. asp&lt;-abs(diff(par()$usr[4:3])/diff(par()$usr[2:1]))*par()$pin[2]/par()$pin[1]
  5. for(i in 1:50){
  6. for(j in i:(100-i+1)){
  7. cells.x[k]&lt;-0.1+(j-1)*0.1/100
  8. cells.y[k]&lt;-(-1)+(i-1)*2*0.1*asp/100
  9. k&lt;-k+1
  10. }
  11. }
  12. # Define the three colors for the gradient
  13. color1 &lt;- &quot;#1e88e5&quot;
  14. color2 &lt;- &quot;#d81b60&quot;
  15. color3 &lt;- &quot;#ffc107&quot;
  16. # Create a color gradient function between the three colors
  17. color_gradient &lt;- colorRampPalette(c(color1, color2, color3))
  18. # Calculate the number of points
  19. num_points &lt;- length(cells.x)
  20. # Generate the gradient colors based on the number of points
  21. gradient_colors &lt;- color_gradient(num_points)
  22. ## draw points
  23. dx&lt;-diff(range(cells.x))
  24. dy&lt;-diff(range(cells.y))
  25. red.x&lt;-max(cells.x)
  26. red.y&lt;-min(cells.y)
  27. red&lt;-1-sqrt((((cells.x-red.x)/dx)^2+((cells.y-red.y)/dy)^2)/2)
  28. green.x&lt;-min(cells.x)
  29. green.y&lt;-min(cells.y)
  30. green&lt;-1-sqrt((((cells.x-green.x)/dx)^2+((cells.y-green.y)/dy)^2)/2)
  31. blue.x&lt;-min(cells.x)
  32. blue.y&lt;-max(cells.y)
  33. blue&lt;-1-sqrt((((cells.x-blue.x)/dx)^2+((cells.y-blue.y)/dy)^2)/2)
  34. points(cells.x,cells.y,col=gradient_colors, pch=15,cex=0.5)

答案1

得分: 1

以下是使用重心坐标的解决方案:

  1. # 三角形顶点
  2. A <- c(0, 0)
  3. B <- c(1, 0)
  4. C <- c(0.5, 1)
  5. # RGB颜色
  6. clrA <- c(1, 0, 0)
  7. clrB <- c(0, 1, 0)
  8. clrC <- c(0, 0, 1)
  9. clrs <- rbind(clrA, clrB, clrC)
  10. library(geometry)
  11. trgl <- rbind(A, B, C)
  12. cart2bary(trgl, rbind(c(1, 1)))
  13. M <- matrix(NA_character_, nrow = 512, ncol = 512)
  14. x <- y <- seq(0, 1, length.out = 512L)
  15. for(i in 1L:512L) {
  16. for(j in 1L:512L) {
  17. coords <- cart2bary(trgl, rbind(c(x[i], y[j])))[1L, ]
  18. if(all(coords >= 0)) {
  19. r <- crossprod(coords, clrs[1, ])
  20. g <- crossprod(coords, clrs[2, ])
  21. b <- crossprod(coords, clrs[3, ])
  22. M[i, j] <- rgb(r, g, b)
  23. }
  24. }
  25. }
  26. M[is.na(M)] <- "white"
  27. par(mar = c(0, 0, 0, 0))
  28. plot(
  29. NULL, asp = 1, xlim = c(0, 1), ylim = c(0, 1),
  30. xlab = NA, ylab = NA, axes = FALSE, xaxs = "i", yaxs = "i"
  31. )
  32. rasterImage(M, 0, 0, 1, 1)

三角形,每个顶点都有来自 r 的颜色渐变。

英文:

Here is a solution using barycentric coordinates:

  1. # triangle vertices
  2. A &lt;- c(0, 0)
  3. B &lt;- c(1, 0)
  4. C &lt;- c (0.5, 1)
  5. # rgb colors
  6. clrA &lt;- c(1, 0, 0)
  7. clrB &lt;- c(0, 1, 0)
  8. clrC &lt;- c(0, 0, 1)
  9. clrs &lt;- rbind(clrA, clrB, clrC)
  10. library(geometry)
  11. trgl &lt;- rbind(A, B, C)
  12. cart2bary(trgl, rbind(c(1,1)))
  13. M &lt;- matrix(NA_character_, nrow = 512, ncol = 512)
  14. x &lt;- y &lt;- seq(0, 1, length.out = 512L)
  15. for(i in 1L:512L) {
  16. for(j in 1L:512L) {
  17. coords &lt;- cart2bary(trgl, rbind(c(x[i], y[j])))[1L, ]
  18. if(all(coords &gt;= 0)) {
  19. r &lt;- crossprod(coords, clrs[1, ])
  20. g &lt;- crossprod(coords, clrs[2, ])
  21. b &lt;- crossprod(coords, clrs[3, ])
  22. M[i, j] &lt;- rgb(r, g, b)
  23. }
  24. }
  25. }
  26. M[is.na(M)] &lt;- &quot;white&quot;
  27. par(mar = c(0, 0, 0, 0))
  28. plot(
  29. NULL, asp = 1, xlim = c(0, 1), ylim = c(0, 1),
  30. xlab = NA, ylab = NA, axes = FALSE, xaxs = &quot;i&quot;, yaxs = &quot;i&quot;
  31. )
  32. rasterImage(M, 0, 0, 1, 1)

三角形,每个顶点都有来自 r 的颜色渐变。

huangapple
  • 本文由 发表于 2023年6月6日 14:57:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定