From: Jason Pearson on 2 Aug 2010 11:17 Roger, Thank you for this really helpful code. I have been looking for a code-efficient way to draw the tangent to two circles, and your code looks really promising. Unfortunately, when I tried your code, it seemed to draw the line at some distance from the circles, rather than as a tangent. Is there any chance that there is a typo in the code that you published? Also, would you be able to advise on how to alter the code to produce the internal (vs external) tangent? I suspect this, too, involves switching a few signs? Any assistance/advice you could provide would be very helpful. Sincerely, Jason Pearson "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <gp7br5$mpc$1(a)fred.mathworks.com>... > "kentavros babis" <akentavros(a)yahoo.com> wrote in message <gp3aei$l81$1(a)fred.mathworks.com>... > > I have a series of consecutive circles with characteristics ([x,y],radius) for example circle1 ([0,0],0.1) > > circle2 ([1.2,1],0.25) > > circle3 ([2.3,2],0.7) > > circle4 ([3.4,3],1.2) > > circle5 ([4.5,4],1.8) > > circle6 ([5.6,5],2.4) > > Is it possible to create two curves that are tangent to all of this circles. > > One above the circles and one below. If it is what should I do to create it? > > Thanks in advance. > > If I were doing that task carefully I would use three steps. The first would be to determine for each successive pair of circles, that line which is tangent to both circles and its two points of tangency. The second would be to find for each circle, except the end two, the half-way point on that circle between the two tangency points on it that were found in step one. For the two end circles the single point of tangency would have to suffice. The third task would be to construct a curve that runs through all these "midpoints" determined in step 2 with slopes equal to the slope of the corresponding circle at that point. > > Below are pieces of code that can be used in carrying out the first and second steps. > > Let [p1,q1] and [p2,q2] be two successive circle centers with r1 and r2 their respective radii. An "upper" line tangent to both circles will be tangent at the two points [x1,y1] and [x2,y2] of the respective circles as calculated by: > > d2 = (p2-p1)^2+(q2-q1)^2; > r = sqrt(d2-(r2-r1)^2); > s = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2; > c = ((p2-p1)*r-(q2-q1)*(r2-r1))/d2; > x1 = p1-r1*s; > y1 = q1+r1*c; > x2 = p2-r2*s; > y2 = q2+r2*c; > > This is more properly described as the line to the left as you travel from circle 1 to circle 2. The line to the right would be obtained by replacing r1 and r2 by reversing their signs to negative values in the same formulae. > > For step two, let P1 = [x1,y1] and P2 = [x2,y2] be two points of tangency on a given circle of radius r and center C = [p,q]. Then you can get the circular midpoint P = [x,y] lying on the circle with: > > P = (P1+P2)/2-C; > P = C + P/norm(P); > > and a line tangent to the circle at this point would be parallel to the vector P2-P1. > > For step three you need to use an interpolation method in which both the location and the slope values are prescribed for a discrete set of non-uniformly-spaced points. I leave that task to you. > > Roger Stafford
From: Matt J on 2 Aug 2010 11:25 "kentavros babis" <akentavros(a)yahoo.com> wrote in message <gp3aei$l81$1(a)fred.mathworks.com>... > Is it possible to create two curves that are tangent to all of this circles. ================== Two curves meaning two lines? If so, see here for a very related thread. http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#764396
From: Jason Pearson on 2 Aug 2010 11:34 Roger, As a followup... I've been experimenting with your code sample, and it looks like changing to the following fixes the offset problem I was having: x1 = p1-(r1*s)/2; y1 = q1+(r1*c)/2; x2 = p2-(r2*s)/2; y2 = q2+(r2*c)/2; However, as I play with different scenarios, it looks like this is not creating a true tangent line. It is creating a line that goes from a point on one circle to a point on the other, but the angle between the points of intersection and the respective centers of the circles are not right angles, so it would not appear to be a true tangent. Would you perchance have time to explain the geometry behind the code (the geometrical meaning of d2, r, s, c), so that I could try to address the issue myself? Thanks for any assistance you might be able to provide. Jason Pearson "Jason Pearson" wrote in message <i36nhi$10q$1(a)fred.mathworks.com>... > Roger, > Thank you for this really helpful code. I have been looking for a code-efficient way to draw the tangent to two circles, and your code looks really promising. > Unfortunately, when I tried your code, it seemed to draw the line at some distance from the circles, rather than as a tangent. > Is there any chance that there is a typo in the code that you published? > Also, would you be able to advise on how to alter the code to produce the internal (vs external) tangent? I suspect this, too, involves switching a few signs? > Any assistance/advice you could provide would be very helpful. > Sincerely, > Jason Pearson > > "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <gp7br5$mpc$1(a)fred.mathworks.com>... > > "kentavros babis" <akentavros(a)yahoo.com> wrote in message <gp3aei$l81$1(a)fred.mathworks.com>... > > > I have a series of consecutive circles with characteristics ([x,y],radius) for example circle1 ([0,0],0.1) > > > circle2 ([1.2,1],0.25) > > > circle3 ([2.3,2],0.7) > > > circle4 ([3.4,3],1.2) > > > circle5 ([4.5,4],1.8) > > > circle6 ([5.6,5],2.4) > > > Is it possible to create two curves that are tangent to all of this circles. > > > One above the circles and one below. If it is what should I do to create it? > > > Thanks in advance. > > > > If I were doing that task carefully I would use three steps. The first would be to determine for each successive pair of circles, that line which is tangent to both circles and its two points of tangency. The second would be to find for each circle, except the end two, the half-way point on that circle between the two tangency points on it that were found in step one. For the two end circles the single point of tangency would have to suffice. The third task would be to construct a curve that runs through all these "midpoints" determined in step 2 with slopes equal to the slope of the corresponding circle at that point. > > > > Below are pieces of code that can be used in carrying out the first and second steps. > > > > Let [p1,q1] and [p2,q2] be two successive circle centers with r1 and r2 their respective radii. An "upper" line tangent to both circles will be tangent at the two points [x1,y1] and [x2,y2] of the respective circles as calculated by: > > > > d2 = (p2-p1)^2+(q2-q1)^2; > > r = sqrt(d2-(r2-r1)^2); > > s = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2; > > c = ((p2-p1)*r-(q2-q1)*(r2-r1))/d2; > > x1 = p1-r1*s; > > y1 = q1+r1*c; > > x2 = p2-r2*s; > > y2 = q2+r2*c; > > > > This is more properly described as the line to the left as you travel from circle 1 to circle 2. The line to the right would be obtained by replacing r1 and r2 by reversing their signs to negative values in the same formulae. > > > > For step two, let P1 = [x1,y1] and P2 = [x2,y2] be two points of tangency on a given circle of radius r and center C = [p,q]. Then you can get the circular midpoint P = [x,y] lying on the circle with: > > > > P = (P1+P2)/2-C; > > P = C + P/norm(P); > > > > and a line tangent to the circle at this point would be parallel to the vector P2-P1. > > > > For step three you need to use an interpolation method in which both the location and the slope values are prescribed for a discrete set of non-uniformly-spaced points. I leave that task to you. > > > > Roger Stafford
From: Jason Pearson on 2 Aug 2010 13:34 Roger, Please ignore my previous posts. Your code works just fine without changes. It was an error in my translation of it that caused the problems. And I figured out how to do the internal tangents by changing a few signs here and there. Thanks again for this elegant solution. Best, Jason Pearson "Jason Pearson" <jason.c.pearson(a)gmail.com> wrote in message <i36nhi$10q$1(a)fred.mathworks.com>... > Roger, > Thank you for this really helpful code. I have been looking for a code-efficient way to draw the tangent to two circles, and your code looks really promising. > Unfortunately, when I tried your code, it seemed to draw the line at some distance from the circles, rather than as a tangent. > Is there any chance that there is a typo in the code that you published? > Also, would you be able to advise on how to alter the code to produce the internal (vs external) tangent? I suspect this, too, involves switching a few signs? > Any assistance/advice you could provide would be very helpful. > Sincerely, > Jason Pearson > > "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <gp7br5$mpc$1(a)fred.mathworks.com>... > > "kentavros babis" <akentavros(a)yahoo.com> wrote in message <gp3aei$l81$1(a)fred.mathworks.com>... > > > I have a series of consecutive circles with characteristics ([x,y],radius) for example circle1 ([0,0],0.1) > > > circle2 ([1.2,1],0.25) > > > circle3 ([2.3,2],0.7) > > > circle4 ([3.4,3],1.2) > > > circle5 ([4.5,4],1.8) > > > circle6 ([5.6,5],2.4) > > > Is it possible to create two curves that are tangent to all of this circles. > > > One above the circles and one below. If it is what should I do to create it? > > > Thanks in advance. > > > > If I were doing that task carefully I would use three steps. The first would be to determine for each successive pair of circles, that line which is tangent to both circles and its two points of tangency. The second would be to find for each circle, except the end two, the half-way point on that circle between the two tangency points on it that were found in step one. For the two end circles the single point of tangency would have to suffice. The third task would be to construct a curve that runs through all these "midpoints" determined in step 2 with slopes equal to the slope of the corresponding circle at that point. > > > > Below are pieces of code that can be used in carrying out the first and second steps. > > > > Let [p1,q1] and [p2,q2] be two successive circle centers with r1 and r2 their respective radii. An "upper" line tangent to both circles will be tangent at the two points [x1,y1] and [x2,y2] of the respective circles as calculated by: > > > > d2 = (p2-p1)^2+(q2-q1)^2; > > r = sqrt(d2-(r2-r1)^2); > > s = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2; > > c = ((p2-p1)*r-(q2-q1)*(r2-r1))/d2; > > x1 = p1-r1*s; > > y1 = q1+r1*c; > > x2 = p2-r2*s; > > y2 = q2+r2*c; > > > > This is more properly described as the line to the left as you travel from circle 1 to circle 2. The line to the right would be obtained by replacing r1 and r2 by reversing their signs to negative values in the same formulae. > > > > For step two, let P1 = [x1,y1] and P2 = [x2,y2] be two points of tangency on a given circle of radius r and center C = [p,q]. Then you can get the circular midpoint P = [x,y] lying on the circle with: > > > > P = (P1+P2)/2-C; > > P = C + P/norm(P); > > > > and a line tangent to the circle at this point would be parallel to the vector P2-P1. > > > > For step three you need to use an interpolation method in which both the location and the slope values are prescribed for a discrete set of non-uniformly-spaced points. I leave that task to you. > > > > Roger Stafford
From: Roger Stafford on 2 Aug 2010 14:48
"Jason Pearson" <jason.c.pearson(a)gmail.com> wrote in message <i36ohe$9im$1(a)fred.mathworks.com>... > Roger, > As a followup... > I've been experimenting with your code sample, and it looks like changing to the following fixes the offset problem I was having: > > x1 = p1-(r1*s)/2; > y1 = q1+(r1*c)/2; > x2 = p2-(r2*s)/2; > y2 = q2+(r2*c)/2; > > However, as I play with different scenarios, it looks like this is not creating a true tangent line. It is creating a line that goes from a point on one circle to a point on the other, but the angle between the points of intersection and the respective centers of the circles are not right angles, so it would not appear to be a true tangent. > > Would you perchance have time to explain the geometry behind the code (the geometrical meaning of d2, r, s, c), so that I could try to address the issue myself? > > Thanks for any assistance you might be able to provide. > > Jason Pearson - - - - - - - - Hello Jason. I had to struggle a bit to recall how that code from a year and a half ago works. I am convinced that step one is valid. I had saved some tests for it which I reran today and they still give correct results. However there is an obvious error in step two. Where I wrote P = C + P/norm(P); it should be P = C + r*P/norm(P); That is why the point P ends up offset from the circle. That previous way it would only be a unit distance from the center at C instead of r. It's too bad I didn't run a test for that step two. I suppose it seemed too easy to bother with at that time. Sorry about that. My alibi is old age. Here is an explanation of the quantities I used in step one as per your request. Call the centers of the two circles C1 =(p1,q1) and C2 = (p2,q2) and the two corresponding points of tangency T1 = (x1,y1) and T2 = (x2,y2). Then d2 is the square of the distance between the centers, while r is the distance between T1 and T2. You can see this latter by sliding the line segment connecting T1 and T2 to a parallel line segment running through C1 and apply pythagoras theorem. The quantity s is the sine of the angle measured counterclockwise from the x-axis to the above line segment (parallel to line T1T2) while c is the cosine of that angle. This angle is the sum of the angle from the x-axis to the line C1C2 and the angle from C1C2 to the above segment. Then x1, y1, x2, and y2 are obtained in the obvious way from that. Let me know if this explanation is too brief and I'll go into more detail. Roger Stafford |