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

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

Triangle with a color gradient from each vertex in r

问题

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

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

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

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

我的当前代码:

## 创建一个单元格坐标的三角形
cells.x <- cells.y <- vector()
k <- 1
asp <- abs(diff(par()$usr[4:3]) / diff(par()$usr[2:1])) * par()$pin[2] / par()$pin[1]
for (i in 1:50) {
  for (j in i:(100 - i + 1)) {
    cells.x[k] <- 0.1 + (j - 1) * 0.1 / 100
    cells.y[k] <- (-1) + (i - 1) * 2 * 0.1 * asp / 100
    k <- k + 1
  }
}

# 定义渐变的三种颜色
color1 <- "#1e88e5"
color2 <- "#d81b60"
color3 <- "#ffc107"

# 创建三种颜色之间的渐变色函数
color_gradient <- colorRampPalette(c(color1, color2, color3))

# 计算点的数量
num_points <- length(cells.x)

# 基于点的数量生成渐变颜色
gradient_colors <- color_gradient(num_points)

## 画点
dx <- diff(range(cells.x))
dy <- diff(range(cells.y))
red.x <- max(cells.x)
red.y <- min(cells.y)
red <- 1 - sqrt((((cells.x - red.x) / dx)^2 + ((cells.y - red.y) / dy)^2) / 2)
green.x <- min(cells.x)
green.y <- min(cells.y)
green <- 1 - sqrt((((cells.x - green.x) / dx)^2 + ((cells.y - green.y) / dy)^2) / 2)
blue.x <- min(cells.x)
blue.y <- max(cells.y)
blue <- 1 - sqrt((((cells.x - blue.x) / dx)^2 + ((cells.y - blue.y) / dy)^2) / 2)
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:

## make a triangle of cell coordinates
cells.x&lt;-cells.y&lt;-vector()
k&lt;-1
asp&lt;-abs(diff(par()$usr[4:3])/diff(par()$usr[2:1]))*par()$pin[2]/par()$pin[1]
for(i in 1:50){
  for(j in i:(100-i+1)){
    cells.x[k]&lt;-0.1+(j-1)*0.1/100
    cells.y[k]&lt;-(-1)+(i-1)*2*0.1*asp/100
    k&lt;-k+1
  }
}

# Define the three colors for the gradient
color1 &lt;- &quot;#1e88e5&quot;
color2 &lt;- &quot;#d81b60&quot;
color3 &lt;- &quot;#ffc107&quot;

# Create a color gradient function between the three colors
color_gradient &lt;- colorRampPalette(c(color1, color2, color3))

# Calculate the number of points
num_points &lt;- length(cells.x)

# Generate the gradient colors based on the number of points
gradient_colors &lt;- color_gradient(num_points)

## draw points
dx&lt;-diff(range(cells.x))
dy&lt;-diff(range(cells.y))
red.x&lt;-max(cells.x)
red.y&lt;-min(cells.y)
red&lt;-1-sqrt((((cells.x-red.x)/dx)^2+((cells.y-red.y)/dy)^2)/2)
green.x&lt;-min(cells.x)
green.y&lt;-min(cells.y)
green&lt;-1-sqrt((((cells.x-green.x)/dx)^2+((cells.y-green.y)/dy)^2)/2)
blue.x&lt;-min(cells.x)
blue.y&lt;-max(cells.y)
blue&lt;-1-sqrt((((cells.x-blue.x)/dx)^2+((cells.y-blue.y)/dy)^2)/2)
points(cells.x,cells.y,col=gradient_colors, pch=15,cex=0.5)

答案1

得分: 1

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

# 三角形顶点
A <- c(0, 0)
B <- c(1, 0)
C <- c(0.5, 1)

# RGB颜色
clrA <- c(1, 0, 0)
clrB <- c(0, 1, 0)
clrC <- c(0, 0, 1)
clrs <- rbind(clrA, clrB, clrC)

library(geometry)

trgl <- rbind(A, B, C)
cart2bary(trgl, rbind(c(1, 1)))

M <- matrix(NA_character_, nrow = 512, ncol = 512)
x <- y <- seq(0, 1, length.out = 512L)
for(i in 1L:512L) {
  for(j in 1L:512L) {
    coords <- cart2bary(trgl, rbind(c(x[i], y[j])))[1L, ]
    if(all(coords >= 0)) {
      r <- crossprod(coords, clrs[1, ])
      g <- crossprod(coords, clrs[2, ])
      b <- crossprod(coords, clrs[3, ])
      M[i, j] <- rgb(r, g, b)
    }
  }
}

M[is.na(M)] <- "white"

par(mar = c(0, 0, 0, 0))
plot(
  NULL, asp = 1, xlim = c(0, 1), ylim = c(0, 1), 
  xlab = NA, ylab = NA, axes = FALSE, xaxs = "i", yaxs = "i"
)
rasterImage(M, 0, 0, 1, 1)

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

英文:

Here is a solution using barycentric coordinates:

# triangle vertices
A &lt;- c(0, 0)
B &lt;- c(1, 0)
C &lt;- c (0.5, 1)

# rgb colors
clrA &lt;- c(1, 0, 0)
clrB &lt;- c(0, 1, 0)
clrC &lt;- c(0, 0, 1)
clrs &lt;- rbind(clrA, clrB, clrC)


library(geometry)

trgl &lt;- rbind(A, B, C)
cart2bary(trgl, rbind(c(1,1)))


M &lt;- matrix(NA_character_, nrow = 512, ncol = 512)
x &lt;- y &lt;- seq(0, 1, length.out = 512L)
for(i in 1L:512L) {
  for(j in 1L:512L) {
    coords &lt;- cart2bary(trgl, rbind(c(x[i], y[j])))[1L, ]
    if(all(coords &gt;= 0)) {
      r &lt;- crossprod(coords, clrs[1, ])
      g &lt;- crossprod(coords, clrs[2, ])
      b &lt;- crossprod(coords, clrs[3, ])
      M[i, j] &lt;- rgb(r, g, b)
    }
  }
}

M[is.na(M)] &lt;- &quot;white&quot;

par(mar = c(0, 0, 0, 0))
plot(
  NULL, asp = 1, xlim = c(0, 1), ylim = c(0, 1), 
  xlab = NA, ylab = NA, axes = FALSE, xaxs = &quot;i&quot;, yaxs = &quot;i&quot;
)
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:

确定