I'm using Processing to try to draw something similar to the image below:
I've seen some examples using bezierVertex
for regular shapes, like this one, but I would like to build a simple drawing method where I can specify my shape's vertex and then the corner radius. Something like myRoundedShape(x1, y1, x2, y2, x3, y3... xn, yn, cornerRadius)
Any ideas?
If you need rounded corners for arbitrary angles, Bezier curves are pretty much the worst possible choice, because the maths is stupidly complex. instead, let's use Catmull-Rom curves, because they're defined as "the curve goes through the following points: ..." instead of being simply controlled by points.
For any 2 edges between 3 points, p1, p2 and p3, we can create new points p2l and p2r on the left and right of p2, along the edges p1--p2 and p2--p3, at a fixed distances "radius" from p2:
and
now our edges will look like p1--p2l and p2r--p3, and the trick is to "connect" p2l and p2r in a nice, arc-ish way. So let's use the
curveVertex
:Of course, you're dealing with lots of edge pairs, so you'll need to preprocess your list of coordinates to become lists of tubles {p2l_guide,p2l,p2r,p2r_guide} instead, and then connect those as straight line p2r(n)--p2l(n+1) and then add the connection curve from p2l to p2r guided by the ..._guide points. In code: http://jsfiddle.net/drzp6L0g/3
This solution does leave us with the mystery variable
f
that controls the guide strength, so to solve that we would need bezier curves, and we'd need to determine the same p2l and p2r points, and then apply some trigonometry to figure out what our control points would need to be in order to effect an approximation a of circular arc. It'd be more accurate, but also more work.The ideal solution, of course, would be to simply use an arcTo command, but Processing doesn't actually have one. It has an
arc()
command, detached from creating shapes (so if you just need outlines... that might do just fine for you!) but if you need a filled shape, no luck.