BennyS
BennyS

Reputation: 45

Create Circle tangent to two lines with radius r [geometry]

I want to put a circle between two lines However, I only want to use one line for the creation of the circle. So my approach is to select points on line 1. For each point on line 1: I want to create circles with increasing radius which are tangential to the point 1 on line. Increase the radius, until the circle intersects also the other line. Decrease afterwards the radius in smaller steps until the circle is tangential to both lines. Proceed with next point on line 1.

Somehow, I am not able to find a clean and crisp code to do that. The points on the line can be done using line.arbitrary_point. How can I create a circle with radius R to the point p1 so that it is tangential in the best way?

Thanks!

Upvotes: 2

Views: 3725

Answers (1)

Ripi2
Ripi2

Reputation: 7198

The center of a circle of radius R that is tangent to two lines s1 and s2 is the intersection of two other lines, each one is parallel by a distance d= R

enter image description here

Let line s1 from A1= {ax1,ay1} to B1= {bx1,by1} and s2 from A2= {ax2,ay2} to B2= {bx2,by2}

Using parametric representation, a point in s1 is

s1x= ax1 + k1·v1x
s1y= ay1 + k1·v1y

where k1 is a different value for each point and {v1x,v1y} is unitary vector in direction A1,B1:

le1 = sqrt((bx1-ax1)^2 + (by1-ay1)^2) //length of A1-B1 segment
v1x = (bx1-ax1) / le1
v1y = (by1-ay1) / le1

Same goes for line s2:

s2x= ax2 + k2·v2x
s2y= ay2 + k2·v2y
with
le2 = sqrt((bx2-ax2)^2 + (by2-ay2)^2) //length of A2-B2 segment
v2x = (bx2-ax2) / le2
v2y = (by2-ay2) / le2

Now, to get a parallel to s1 we just change point A1 by adding a vector vp1 perpendicular to A1-B1 with length d= R. The perpendicular unitary vector is: vpu1= {-v1y, v1x}. Attention: if you want the other solution, at the other side of both lines, then use {v1y, -v1x} instead.
So, with vp1 = vpu1·R and vp2= {-v2y, v2x}·R the new points for parallels to s1 and s2 are

px1= ax1 - v1y·R
py1= ay1 + v1x·R
px2= ax2 - v2y·R
py2= ay2 + v2x·R

Now we must find the intersection C of theses lines:

sp1x= px1 + k1·v1x
sp1y= py1 + k1·v1y
sp2x= px2 + k2·v2x
sp2y= py2 + k2·v2y
with
sp1x = sp2x
sp1y = sp2y

Solving for k1, k2, sp1x, sp1y we get:

den = v1x·v2y - v2x·v1y
k1 = (v2y·(px2-px1) - v2x·(py2-py1)) / den
k2 = (v1y·(px2-px1) - v1x·(py2-py1)) / den
cx = px1 + k1·v1x   =   px2 + k2·v2x   //choose one
cy = py1 + k1·v1y   =   py2 + k2·v2y   //choose one

Pay attention to the value of den. It abs(den) < smallValue (IOW, this denominator is close to zero), then s1 and s2 are parallel or the same straight. In this case, there isn't a solution.

Finally, the tangent points are now easy:

tx1 = ax1 + k1·v1x
ty1 = ay1 + k1·v1y
tx2 = ax2 + k2·v2x
ty2 = ay2 + k2·v2x

Upvotes: 11

Related Questions