[R] continuous coloring of a polygon

Earl F. Glynn efg at stowers-institute.org
Fri Aug 15 18:23:33 CEST 2008


"Roger Leenders" <r.t.a.j.leenders at rug.nl> wrote in message 
news:48A56FE6.60304 at rug.nl...

> Now I want to fill the polygon with color, such that it starts on the left 
> with red and ends on the right with green, following the coloring of the 
> rainbow.

I'd recommend using polar coordinates, like shown below.

Instead of 2000 points, 200 or even 100 seem to be enough.

Break your arc (polygon) into smaller trapezoids and color them with a 
continuously changing palette:


red.to.green <- rainbow(N, start=0, end=0.35))

N <- 200  # points used for a full circle

radius <- 3
theta <- seq(0.0, 2*pi, length.out=N)

# Plot circle
plot(radius*cos(theta), radius*sin(theta),
     xlim=c(-radius,radius),ylim=c(-radius,radius),
     type="l", ylab="", xlab="", axes=F)

# Break arc into filled trapezoids

radius.outer <- 2.7
radius.inner <- 2.0

N2 <- N / 2
theta <- seq(pi, 0.0, length.out=N/2)

red.to.green <- rainbow(N2, start=0, end=0.35)

for (i in 1:(N2-1))
{
  x1 <- radius.outer*cos(theta[i])
  x2 <- radius.outer*cos(theta[i+1])
  x3 <- radius.inner*cos(theta[i+1])
  x4 <- radius.inner*cos(theta[i])

  y1 <- radius.outer*sin(theta[i])
  y2 <- radius.outer*sin(theta[i+1])
  y3 <- radius.inner*sin(theta[i+1])
  y4 <- radius.inner*sin(theta[i])

  polygon( c(x1, x2, x3, x4, x1),
           c(y1, y2, y3, y4, y1), col=red.to.green[i], border=NA)
}


# Draw outline of arc

x.outer <- radius.outer * cos(theta)
y.outer <- radius.outer * sin(theta)

x.inner <- radius.inner * cos(theta)
y.inner <- radius.inner * sin(theta)

polygon(c(x.outer, rev(x.inner), x.outer[1] ),
        c(y.outer, rev(y.inner), y.outer[1] ) )


efg

Earl F Glynn
Stowers Institute for Medical Reseach



More information about the R-help mailing list