I’d like to create an SVG file of an arbitrarily-sided polygon that is filled with triangles (or other drawings, potentially.).
I have no idea how to do this in Processing or if it is even possible.
I was hoping someone here might be able to help me. I’ve provided the basic example I’m working with, which draws a shape and the intended texture - but I have no idea how to join them. Any help would be greatly, greatly appreciated.
Thanks!
import processing.svg.*;
void setup(){
size(500, 500);
noLoop();
}
void draw(){
beginRecord(SVG, "example.svg");
polygon(200, 200, random(100)+100, int(random(10)+4));
texturer();
endRecord();
}
void polygon(float x, float y, float radius, int npoints) {
float angle = TWO_PI / npoints;
noFill();
beginShape();
for (float a = 0; a < TWO_PI; a += angle) {
float sx = x + cos(a) * radius;
float sy = y + sin(a) * radius;
vertex(sx, sy);
}
endShape(CLOSE);
};
void texturer() {
for (int h=0; h<10; h++){
for (int v=0; v<10; v++){
push();
translate(h*10, v*10);
triangle(2, 8, 2, 2, 9, 4);
pop();
}
}
};
to fill a shape you need to use a bitmap image as a texture
the SVG renderer does not support texture(...), textureMode(...) etc. it needs the P2D or P3D renderers to use these statements.
I did have a play with this (code below) and the nearest I got was to display it on screen - I could not save it as a SVG (or PDF I tried that as well)
So as far as I can see they can’t be linked together.
Final thought, SVG does support clipping to a non-rectangular region but I don’t think this is supported in Processing, the only reference I could find was clip()
The following code examples will create a polygon with your triangle pattern. I’ll leave it to you to create an SVG from there; trying to create an SVG along with everything else causes the compiler to throw an exception. My approach would be to first create an image texture from your triangle pattern (I prefer the FX2D renderer for better resolution):
Wow - these examples both look great.
I do understand that Processing has a “texture()” function that requires bitmapped images - and I see that you both had really great, creative solutions for generated a bitmapped image out of the generated texture.
But if I specifically need to keep all this stuff vector-based, what are my options? I know it isn’t built-in to Processing, as it stands - but what would it take to find a workaround? Is it something Processing developers plan to do? Is it a reasonable request for a freelance assignment?
It is quite common to use bitmap images as textures and SVG supports embedded bitmaps and links to external bitmaps as part of its specification. SVG can also use vector graphics as textures but is more restrictive in capabilities compered to bitmaps. For instance Inkscape supports embedding and external links when saving SVG.
I have looked at how Processing implements SVG creation and it appears that when a PGraphics method (e.g. line(...) ) is executed it writes the SVG equivalent to a file. It means that many features in the SVG specification are inaccessible because there is no equivalent in PGrahics
So unfortunately there does not seem to be a simple work around.
If we use a mask to display the triangle texture inside of a polygon it is possible to create an .svg file. A two stage method is shown below. First the texture is created with a triangle pattern; it is important that this be 480x480 in size to avoid an array overrun in the minAlphas() function in the second step. The second step was taken from a Processing forum archive post and is the step which applies the mask and creates the .svg image.
I haven’t tried it for SVG export, but Geometry Suite for Processing (PGS) by @micycle has a mesh intersection feature which might do what you want, without the need for bitmap textures.