The shape in the example comes from
this Inkscape tutorial, showing the program's ability to make insets and outsets (even if it seems to me that if you look carefully enough, you can spot at least one defect in the above example).
Anyway, need to do that in Processing and thanks to your useful feedbacks, I discovered some geometrically fascinating and
scary stuff.
To make a long story short, I couldn't help but trying to reinvent MY wheel.
I often regret this approach later, but this time I like what I got and I think it's quite simple and effective, though there are still some problems.
I'm using the Geomerative library and the basic idea is to translate the original shape clockwise and make unions at each step.
Here's the code if you want to give it a look:
- import geomerative.*;
import processing.opengl.*;
//import processing.xml.*;
void setup() {
String svgFile = "monss.svg";
RShape sx, sxofs;
// Initilaize the sketch
size(400, 400);
// Choice of colors
background(255);
noFill();
strokeWeight(1);
smooth();
// VERY IMPORTANT: Allways initialize the library in the setup
RG.init(this);
RG.ignoreStyles();
RG.setPolygonizer(RG.ADAPTATIVE);
RG.setPolygonizerAngle(10.0);
//svgFile = selectInput();
sx = RG.loadShape(svgFile);
stroke(100);
for (int i = 0; i<10;i++) {
if (i!=5) {
sxofs = shapeOutset(sx, i*10-50);
sxofs.draw();
println(sxofs.getPoints().length);
}
}
stroke(200, 10, 10);
sx.draw();
println(sx.getPoints().length);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// shapeOutset
//
RShape shapeOutset(RShape s, float ofs) {
boolean inset = false;
int ast = 25; // number of "steps" while translating clockwise
// affects: -speed, +smoothness, +number of points
// becomes less critical if "ts" is simlifyed
RShape mks;
RShape ts = new RShape();
if (ofs<0) { //inset required
inset = true;
ofs = -ofs;
mks = RG.diff(RG.getRect(s.getX()-ofs, s.getY()-ofs, s.getWidth()+2*ofs, s.getHeight()+2*ofs), s);
}
else { //outset required
mks = s;
}
for (int i = 0; i<ast*2; i++) {
float tx = cos(PI/float(ast)*float(i))*ofs;
float ty = sin(PI/float(ast)*float(i))*ofs;
mks.translate( tx, ty );
ts = ts.union( mks );
// SIMPLIFY SHAPE HERE!!!
// .polygonize(), .update()... ?
mks.translate( -tx, -ty );
}
if (inset)
ts = RG.intersection(ts, s);
else
ts = RG.diff(ts, s);
// REMOVE s PATHS FROM ts
return ts;
}
You may notice that I still can't get line numbers and indentation while pasting code, but most importantly I'd really like to hear your opinion on this approach and on how to implement the two comments
-
// SIMPLIFY SHAPE HERE and
- // REMOVE s PATHS FROM ts
I don't think these are hard things to do, even if I'm not sure on how to do them
..
Thanks!