Skip to content

Instantly share code, notes, and snippets.

@knielsen
Created February 23, 2017 15:41
Show Gist options
  • Save knielsen/295ae92aad0b2ee1b091d58c968faef3 to your computer and use it in GitHub Desktop.
Save knielsen/295ae92aad0b2ee1b091d58c968faef3 to your computer and use it in GitHub Desktop.
function tangent_center(x0, x1, y0, y1, radius, otherside) =
let(tmpnx = (y1 - y0),
tmpny = -(x1 - x0),
l = sqrt(tmpnx*tmpnx+tmpny*tmpny)*(otherside ? -1 : 1),
nx = tmpnx/l,
ny = tmpny/l,
x3 = x0 + radius*nx,
y3 = y0 + radius*ny,
x4 = x1 + radius*nx,
y4 = y1 + radius*ny,
tmpvx = (x4 - x3),
tmpvy = (y4 - y3),
tmpv = sqrt(tmpvx*tmpvx+tmpvy*tmpvy),
vx = tmpvx/tmpv,
vy = tmpvy/tmpv,
del = (-radius-y3)/vy,
x2 = x3 + del*vx,
y2 = y3 + del*vy)
[x2,y2];
$fa = 8; $fs = 0.1;
x0 = 35;
y0 = 0;
x1 = 40;
y1 = -50;
radius = 4;
square(size=[100, 5], center=false);
polygon(points=[[x0, y0], [x1, y1], [x1+5, y1], [x0+5+2*(x1-x0), 0]]);
circ_center = tangent_center(x0,x1,y0,y1,radius,false);
#difference() {
translate(circ_center) circle(r=radius);
translate(circ_center) circle(r=radius-0.1);
}
echo(circ_center[0]);
echo(circ_center[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment