英文:
Triangle with a color gradient from each vertex in 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:
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:
My current code:
## make a triangle of cell coordinates
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
}
}
# Define the three colors for the gradient
color1 <- "#1e88e5"
color2 <- "#d81b60"
color3 <- "#ffc107"
# Create a color gradient function between the three colors
color_gradient <- colorRampPalette(c(color1, color2, color3))
# Calculate the number of points
num_points <- length(cells.x)
# Generate the gradient colors based on the number of points
gradient_colors <- color_gradient(num_points)
## draw 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)
答案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)
英文:
Here is a solution using barycentric coordinates:
# triangle vertices
A <- c(0, 0)
B <- c(1, 0)
C <- c (0.5, 1)
# rgb colors
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论