Hi,

See if this helps,

polygon.regular <-  # return a matrix of xy coordinates for a regular
polygon centered about (0,0)
function (sides = 5)
{
    n <- sides
    if (n < 3)
        n <- 3
    if (n > 8)
        n <- 50

        th <- pi * 2/n
        costh <- cos(th)
        sinth <- sin(th)
        xx <- yy <- rep(0, n + 1)
        if (n%%2) {
            xx[1] <- 0
            yy[1] <- 1
        }
        else {
            xx[1] <- -sin(th/2)
            yy[1] <- cos(th/2)
        }
        for (i in 2:(n + 1)) {
            xl <- xx[i - 1]
            yl <- yy[i - 1]
            xx[i] <- xl * costh - yl * sinth
            yy[i] <- xl * sinth + yl * costh
        }
        xy <- matrix(c(xx, yy), ncol = 2)

    return(xy)
}

pentagon <- polygon.regular(5) / 10 # scale down

rotate <- function (xy, theta = 0)
{
    matR <- matrix(c(cos(theta), -sin(theta), sin(theta), cos(theta)),
        nrow = 2)

    t(matR %*% t(xy))

}


all <- do.call(rbind,
lapply(seq(0, 2*pi, length=5), rotate, xy=pentagon))

large <-  pentagon * 2 # these will be the center positions

all[, 1] <- all[, 1] + rep(large[1:5, 1], each=nrow(pentagon)) # shift the
polygons
all[, 2] <- all[, 2] + rep(large[1:5, 2], each=nrow(pentagon))

all <- cbind(all, rep(1:5, each=nrow(pentagon))) # give them an ID

grid.newpage()
vp <- viewport(1, 1,   xscale=c(-1, 1), yscale=c(-1, 1))
pushViewport(vp)

grid.polygon(all[, 1], all[, 2], id=all[, 3], gp=gpar(fill=1:5))


HTH,

baptiste

2009/9/10 William Dunlap <wdun...@tibco.com>

> Try representing the pentagon as a set of complex
> numbers.  Translate them by adding a complex
> number and multiply by exp(1i*angle) to rotate them
> around the origin.  E.g. to rotate them around their
> center of gravity, mean(p), do
>
> > p<-complex(real=c(4,5,7,8,6), imag=c(5,3,3,5,7))
> > plot(p, xlim=c(0,10), ylim=c(0,10), type="n")
> > for(k in 0:10)polygon(border=k, lwd=k+1,
> (p-mean(p))*exp(k*1i/10*pi)+mean(p))
>
> Bill Dunlap
> TIBCO Software Inc - Spotfire Division
> wdunlap tibco.com
>
> > -----Original Message-----
> > From: r-help-boun...@r-project.org
> > [mailto:r-help-boun...@r-project.org] On Behalf Of Hemavathi Ramulu
> > Sent: Thursday, September 10, 2009 1:44 AM
> > To: Greg Snow
> > Cc: r-help@r-project.org
> > Subject: Re: [R] How to do rotation for polygon?
> >
> > Hi everyone,
> > I still couldn't get the diagram as I mentioned before. I try Grey and
> > Milton suggestion but
> > it confusing.
> > I hope anyone helped me.
> >
> > Thanks in advance.
> >
> > Regards,
> > Hema.
> >
> > On Thu, Sep 3, 2009 at 11:39 PM, Greg Snow
> > <greg.s...@imail.org> wrote:
> >
> > > The my.symbols and ms.polygon functions in the
> > TeachingDemos package may
> > > help.
> > >
> > > --
> > > Gregory (Greg) L. Snow Ph.D.
> > > Statistical Data Center
> > > Intermountain Healthcare
> > > greg.s...@imail.org
> > > 801.408.8111
> > >
> > >
> > > > -----Original Message-----
> > > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
> > > > project.org] On Behalf Of Hemavathi Ramulu
> > > > Sent: Wednesday, September 02, 2009 11:05 PM
> > > > To: r-help@r-project.org
> > > > Subject: [R] How to do rotation for polygon?
> > > >
> > > > Hi everyone,
> > > > I have coding for repeating pentagon as below:
> > > >
> > > > plot(0:11,type="n")
> > > > for (i in 1:10 )polygon(rep(c(4,5,7,8,6)),
> > i*c(.5,.3,.3,.5,.7), bor=2)
> > > >
> > > > which are increasing vertically.
> > > >
> > > > Now, I want to know how to rotate the pentagon, so that I will get
> > > > pattern
> > > > like flower.
> > > > Basicly, repeating pentagon in circle.
> > > >
> > > > Thanks alot for helping me to solve this problem.
> > > > --
> > > > Hemavathi
> > > >
> > > >       [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > R-help@r-project.org mailing list
> > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > PLEASE do read the posting guide http://www.R-project.org/posting-
> > > > guide.html
> > > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >
> >
> > --
> > Hemavathi Ramulu
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
_____________________________

Baptiste AuguiƩ

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to