What is the simplest (namely: minimum number of libraries or conflicts) way to fill a polygon with a pattern? For example, suppose I have (in a graphic file, like a jpeg) the drawing of an "X". Then I fill a polygon (like a triangle) with this pattern, and get something like: X XXX XXXXX XXXXXXX (where the border "X"s could be truncated). I searched the rimage package, but it seems that there is no such function. Alberto Monteiro
The simplest approach using just R is probably to create the background by plotting the grid of your images, then draw with your background color over everything outside the polygon that you want. A possibly simpler approach is to draw the polygon in a solid color, then use an outside program such as Imagemagick or gimp2 to replace the polygon/color with your image. This post: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/109468.html may also give some ideas (similar question). Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Alberto Monteiro > Sent: Monday, October 08, 2007 3:42 PM > To: r-help at r-project.org > Subject: [R] Fill a polygon with a pattern > > What is the simplest (namely: minimum number of libraries or > conflicts) way to fill a polygon with a pattern? > > For example, suppose I have (in a graphic file, like a jpeg) > the drawing of an "X". Then I fill a polygon (like a > triangle) with this pattern, and get something like: > > X > XXX > XXXXX > XXXXXXX > > (where the border "X"s could be truncated). > > I searched the rimage package, but it seems that there is no > such function. > > Alberto Monteiro > > ______________________________________________ > R-help at 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. >
On 08/10/2007 5:42 PM, Alberto Monteiro wrote:> What is the simplest (namely: minimum number of libraries or conflicts) > way to fill a polygon with a pattern? > > For example, suppose I have (in a graphic file, like a jpeg) the > drawing of an "X". Then I fill a polygon (like a triangle) with > this pattern, and get something like: > > X > XXX > XXXXX > XXXXXXX > > (where the border "X"s could be truncated). > > I searched the rimage package, but it seems that there is no > such function.Depending on the polygon, the simplest way might be to use rgl, and use texture mapping to repeat the X. I think it would be something like this: x <- c(0, 0.5, 1) y <- c(0, 1, 0) triangles3d(x, 0, y, texcoords=cbind(x, y)*6, texture=system.file("textures/rgl2.png",package="rgl"), col="white", specular="black", ambient="white") (The col="white" says to paint the image on a white canvas; specular="black" turns off reflections, and ambient="white" makes it equally bright regardless of how you rotate it. The texcoords argument gives coordinates in the image (only the fractional part counts), corresponding to the geometry coordinates specified as the first 3 args.) You would replace the texture= argument with your own filename. It needs to be a .png file, not a JPEG. This is more difficult if the polygon isn't a triangle or a quadrilateral, because you'd have to break it down into those. There's a function in gpclib that will triangulate a general polygon. Duncan Murdoch