Editing Computational geometry

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 303: Line 303:
  
 
==Translation along a line==
 
==Translation along a line==
Given some line <math>Ax + By + C = 0</math> and a point <math>(x_0, y_0)</math> known to be on the line, we wish to locate another point on the line that is distance <math>h</math> away from <math>(x_0, y_0)</math>. We can do this by observing that the general point is given by <math>(x_0 - Bt, y_0 + At)</math>, which is distance <math>\sqrt{(Bt)^2 + (At)^2} = t \sqrt{A^2+B^2} = h</math> away from <math>(x_0, y_0)</math>. Therefore, we want <math>t = \frac{h}{\sqrt{A^2+B^2}}</math>, and finally obtain <math>\left(x_0 - \frac{Bh}{\sqrt{A^2+B^2}}, y_0 + \frac{Ah}{\sqrt{A^2+B^2}}\right)</math>. Note that unless <math>h=0</math>, there is another possible answer, which is simply "on the other side": <math>\left(x_0 + \frac{Bh}{\sqrt{A^2+B^2}}, y_0 - \frac{Ah}{\sqrt{A^2+B^2}}\right)</math>.
+
Given some line <math>Ax + By + C = 0</math> and a point <math>(x_0, y_0)</math> known to be on the line, we wish to locate another point on the line that is distance <math>h</math> away from <math>(x_0, y_0)</math>. We can do this by observing that the general point is given by <math>(x_0 - Bt, y_0 + At)</math>, which is distance <math>\sqrt{(Bt)^2 + (At)^2} = t \sqrt{A^2+B^2}</math> away from <math>(x_0, y_0)</math>. Therefore, we want <math>t = \frac{h}{A^2+B^2}</math>, and finally obtain <math>\left(x_0 - \frac{Bh}{A^2+B^2}, y_0 + \frac{Ah}{A^2+B^2}\right)</math>. Note that unless <math>h=0</math>, there is another possible answer, which is simply "on the other side": <math>\left(x_0 + \frac{Bh}{A^2+B^2}, y_0 - \frac{Ah}{A^2+B^2}\right)</math>.
  
 
==Dropping a perpendicular==
 
==Dropping a perpendicular==
Line 441: Line 441:
 
\end{array}
 
\end{array}
 
</math>
 
</math>
 
==Translating a line==
 
Suppose we are given a line <math>Ax + By + C = 0</math> and we wish to find a line parallel to it such that the distance between these two lines is <math>h</math>. To do this, we suppose that the new line has equation <math>Ax + By + D = 0</math>. Since the two lines have the same <math>A</math> and <math>B</math> value, they must be parallel (or coincident of <math>C = D</math> also). Then, the distance between them will be given by <math>\frac{|C-D|}{\sqrt{A^2+B^2}}</math>, as the previous section suggests. To obtain a value of <math>h</math> for this expression, it is not hard to see that we should choose <math>D = C \pm h\sqrt{A^2+B^2}</math>.
 
  
 
=Line segments=
 
=Line segments=
Line 764: Line 761:
  
 
===Two points of intersection===
 
===Two points of intersection===
When the closest distance from the centre of the circle to the line is less than the circle's radius, the line intersects the circle twice.
+
When the closest distance from the centre of the circle to the line is less than the circle's radius, the line intersects the circle twice. The algebraic method must be used to find these points of intersection.
 
+
To find the two points of intersection, first drop a perpendicular from the centre of the circle to the line. Call the foot of the perpendicular <math>P</math> and denote the distance <math>|OP|</math> by <math>d</math>. Now, let the two points of intersection be <math>Q_1</math> and <math>Q_2</math>. Then, the triangles <math>\triangle OPQ_1</math> and <math>\triangle OPQ_2</math> are right-angled, and hence we see that <math>|OP|^2 + |Q_1P|^2 = |OQ_1|^2 = r^2</math> and <math>|OP|^2 + |Q_2P|^2 = |OQ_2|^2 = r^2</math>. So we compute the value <math>h = \sqrt{r^2 - |OP|^2}</math>; now, if we move <math>h</math> units in either direction along the line from <math>P</math>, we will reach either <math>Q_1</math> or <math>Q_2</math>. (The technique to do so is given in section [[#Translation along a line|Translation along a line]].)
+
  
 
==Intersection of a circle with a circle==
 
==Intersection of a circle with a circle==
Line 775: Line 770:
  
 
===One point of intersection===
 
===One point of intersection===
Let the two circles have centres <math>O_1</math> and <math>O_2</math> and radii <math>r_1</math> and <math>r_2</math>.
+
When the distance between the centres of the circles is exactly the difference between their radii, the two circles will be internally tangent. When the distance between the centres of the circles is exactly the sum of their radii, they will be externally tangent.
* When the distance between the centres of the circles is exactly the difference between their radii, the two circles will be internally tangent. Suppose that the first circle is larger. Then the point of intersection lies on the line between <math>O_1</math> and <math>O_2</math>. The intersection point and <math>O_1</math> are on opposite sides of <math>O_2</math>, and its distances from <math>O_1</math> and <math>O_2</math> are <math>r_1</math> and <math>r_2</math>, respectively. This gives <math>P = \frac{O_2 r_1 - O_1 r_2}{r_1 - r_2}</math>. (It turns out that if we swap the 1's and 2's in this formula, we get the same value; hence we can use this formula without regard to which circle is labelled 1 and which one is 2.)
+
* When the distance between the centres of the circles is exactly the sum of their radii, they will be externally tangent; they will have one point of intersection located between <math>O_1</math> and <math>O_2</math>; it will be <math>r_1</math> units away from the former and <math>r_2</math> units away from the latter, giving the formula <math>P = \frac{O_2 r_1 + O_1 r_2}{r_1 + r_2}</math>.
+
  
 
===Two points of intersection===
 
===Two points of intersection===
 
In all other cases, there will be two points of intersection.
 
In all other cases, there will be two points of intersection.
 
To find them, it will be helpful to draw a diagram. Denote the centres of the circles <math>O_1, O_2</math>, their radii <math>r_1, r_2</math>, and the two points of intersection <math>P_1, P_2</math>. First draw the ''radical axis'', the line that passes through <math>P_1</math> and <math>P_2</math>. This passes through the midpoint <math>M</math> between <math>P_1</math> and <math>P_2</math>, and here it intersects perpendicularly the line between <math>O_1</math> and <math>O_2</math>. Denote <math>|O_1M|</math> and <math>|O_2M|</math> by <math>d_1, d_2</math>, respectively. Clearly
 
:<math>d_1 + d_2 = |O_1O_2|</math>. (1)
 
Also, from the Pythagorean theorem, <math>|MP_1|^2 = |O_1P_1|^2 - |O_1M|^2 = |O_2P_1|^2 - |O_2M|^2</math>, or <math>r_1^2 - d_1^2 = r_2^2 - d_2^2</math>. Rearranging this gives
 
:<math>d_1^2 - d_2^2 = r_1^2 - r_2^2</math>. (2)
 
 
Now we solve for <math>d_1</math> and <math>d_2</math>. To do this, we factor the left side of (2), giving <math>(d_1 - d_2)(d_1 + d_2) = r_1^2 - r_2^2</math>, which allows us to divide through by (1), thus obtaining
 
:<math>d_1 - d_2 = \frac{r_1^2 - r_2^2}{|O_1O_2|}</math>. (3)
 
Equations (1) and (3) are linear and may be used to solve for <math>d_1, d_2</math> in a straightforward fashion.
 
 
Now we can compute the coordinates of <math>M</math> as <math>M = \frac{O_2 d_1 + O_1 d_2}{d_1 + d_2}</math>, using the argument from the previous section. Then construct the line perpendicular to <math>O_1O_2</math> that passes through <math>M</math> (the radical axis). Finally, we can find the distance <math>|MP_1| = |MP_2| = \sqrt{r_1^2 - d_1^2} = \sqrt{r_2^2 - d_2^2}</math>, and move this distance in either direction along the radical axis (see [[#Translation along a line|Translation along a line]]) to obtain <math>P_1</math> and <math>P_2</math>.
 
 
It should be noted that two circles that intersect at two points cut each other into two arcs each: one arc that lies within the other circle, and one that lies outside it. It is not immediately obvious which is which. However, you should be able to convince yourself from drawing a diagram that if <math>(P_1-O_1) \times (O_2-O_1) > 0</math>, then the arc of the first circle that is inside the second circle is counterclockwise from <math>P_1</math> to <math>P_2</math>, but if that determinant is negative, then the inside arc is counterclockwise from <math>P_2</math> to <math>P_1</math>. (For an example of where this is important, see {{Problem|detectors|Detectors}}.)
 
 
==Tangents==
 
A line is said to be ''tangent'' to a circle if it intersects the circle at exactly one point.
 
 
The tangent has an important property: if a circle with centre <math>O</math> has a tangent line <math>l</math> that touches it at <math>P</math>, then <math>l \perp OP</math>. The proof of this fact is simple: we assume that the two lines are not perpendicular, then drop a perpendicular from <math>O</math> to <math>l</math> at <math>Q</math>. We observe that <math>\triangle OPQ</math> is right-angled at <math>Q</math>, and that therefore <math>|OP| > |OQ|</math>. But this implies that <math>Q</math> is inside the circle, a contradiction.
 
 
To actually find the tangent line to one circle at a given point on its circumference is easy; it is simply the line passing through that point that is perpendicular to the radius.
 
 
===From point to circle===
 
Sometimes we may also be given a circle with centre and radius <math>(O, r)</math> plus a point <math>P</math> and asked to find a tangent to the circle that passes through the point. There may be either zero, one, or two such tangents.
 
 
====Point is inside circle====
 
When the point is inside the circle, there are no tangents to the circle through that point, because any line through the point must intersect the circle twice.
 
 
====Point is on circle====
 
When the point is on the circumference of the circle, the line passing through <math>P</math> that is perpendicular to <math>OP</math> is the unique tangent to the circle passing through that point.
 
 
====Point is outside circle====
 
When the point is outside the circle, there are two tangents to the circle passing through the point. To construct one such tangent, we use a trick called ''inversion''. We let one of the points of tangency be denoted <math>Q</math>. Now <math>\triangle OQP</math> is right-angled at <math>Q</math>. If we now drop a perpendicular from <math>Q</math> to <math>OP</math> at <math>R</math>, we obtain triangle <math>\triangle OQR</math> which is similar to <math>\triangle OPQ</math>. This means that <math>\frac{|OP|}{|OQ|} = \frac{|OQ|}{|OR|}</math>, so <math>|OR| = \frac{r^2}{|OP|}</math>. Note that <math>R</math> is ''inside'' the circle, unlike <math>P</math> which is ''outside'' (hence the name ''inversion''.) This allows us to locate point <math>R</math> on the line segment <math>OP</math>. Then, we see that the line segment <math>RQ</math> is perpendicular to <math>OP</math> and has length <math>\sqrt{r^2 - |OR|^2}</math> from the Pythagorean theorem in <math>\triangle OQR</math>. This gives us enough information to compute the coordinates of <math>Q</math>. There will be two possibilities, since we can draw two rays through <math>R</math> that are perpendicular to <math>OP</math>.
 
 
===Circle-circle tangents===
 
A pair of circles may have either zero, one, two, three, or four mutual tangents.
 
 
====Zero mutual tangents====
 
If one circle lies within the other (that is, the difference between their radii is greater than the distance between their centres), then they have no mutual tangents, since every tangent to the outer circle lies outside it, and hence cannot touch the inner circle.
 
 
====One mutual tangent====
 
Two circles will have one mutual tangent if they are internally tangent (that is, the difference between their radii equals the distance between their centres). This tangent can easily be found after constructing the point <math>P</math> of internal tangency (see section [[#One point of intersection|One point of intersection]] above).
 
 
====Two mutual tangents====
 
A pair of circles that intersect at two points has two mutual tangents, one on either "side". We will consider the problem of how to determine one of them.
 
 
Assume the circles have centres <math>O_1, O_2</math> and radii <math>r_1, r_2</math>. Assume further that they have some mutual tangent <math>l</math> and that <math>l</math> is tangent to the circles at <math>P_1, P_2</math>, respectively. We see that <math>O_1P_1 \perp l</math> and <math>O_2P_2 \perp l</math>. Now, assume without loss of generality that <math>r_2 \geq r_1</math>. Then drop the perpendicular from <math>O_1</math> to <math>O_2P_2</math> at <math>Q</math>, and obtain <math>|O_2Q| = r_2 - r_1</math>. Then <math>\theta = \angle O_1O_2Q = \cos^{-1} \frac{r_2-r_1}{|O_1O_2|}</math>. Also, we can solve for <math>h = |O_1Q|</math> using the Pythagorean theorem, obtaining <math>h = \sqrt{|O_1O_2|^2 - (r_2-r_1)^2}</math>. Notice that because <math>O_1QP_2P_1</math> is a rectangle, <math>h = |P_1P_2|</math>.
 
 
There are two possibilities for the location of <math>P_2</math>. One of them is obtained by rotating the ray <math>O_1 - O_2</math> clockwise through angle <math>\theta</math>, the other obtained by rotating it counterclockwise, and in either case determining where the ray cuts the second circle. (So it doesn't actually matter which circle is bigger; the formula <math>\cos^{-1} \frac{r_2-r_1}{|O_1O_2|}</math> will give the same result up to sign.)
 
 
We can then argue that the coordinates of one of the possible <math>P_2</math>'s is given by <math>O_2 + r_2 [\cos (\operatorname{atan2}(O_1-O_2) + \theta), \sin (\operatorname{atan2}(O_1-O_2) + \theta)]</math>, because <math>\operatorname{atan2}(O_1-O_2)</math> represents the directed angle from the positive x-axis to the ray from <math>O_2</math> to <math>O_1</math>, and we are adding onto this directed angle the value <math>\theta</math> to get our final position. The other possible <math>P_2</math> will be given by subtracting <math>\theta</math> instead of adding.
 
 
This formula as written is slow, since it involves trigonometric functions, which have a higher [[invisible constant factor]] than algebraic operations and square roots. Therefore, we will rewrite it in terms of only algebraic operations. Define <math>\Delta x = O_{1_x} - O_{2_x}</math> and <math>\Delta y</math> analogously.
 
 
Verify the identities <math>\cos \operatorname{atan2} \frac{y}{x} = \frac{x}{x^2 + y^2}</math> and <math>\sin \operatorname{atan2} \frac{y}{x} = \frac{y}{x^2 + y^2}</math>. (These are actually obvious from the definition of <code>atan2</code>). Then:
 
:<math>\cos( \operatorname{atan2}(O_1 - O_2) + \theta)</math>
 
:<math> = \cos \operatorname{atan2}(O_1 - O_2) \cos \theta - \sin \operatorname{atan2}(O_1 - O_2) \sin \theta</math>
 
:<math> = \frac{\Delta x}{\Delta x^2 + \Delta y^2} \frac{r_2-r_1}{|O_1O_2|} - \frac{\Delta y}{\Delta x^2 + \Delta y^2} \frac{h}{|O_1O_2|}</math>
 
:<math> = \frac{(r_2-r_1)\Delta x - h\Delta y}{|O_1O_2|^2}</math>
 
And likewise
 
:<math>\sin( \operatorname{atan2}(O_1 - O_2) + \theta)</math>
 
:<math> = \sin \operatorname{atan2}(O_1 - O_2) \cos \theta + \cos \operatorname{atan2}(O_1 - O_2) \sin \theta</math>
 
:<math> = \frac{\Delta y}{\Delta x^2 + \Delta y^2} \frac{r_2-r_1}{|O_1O_2|} + \frac{\Delta x}{\Delta x^2 + \Delta y^2} \frac{h}{|O_1O_2|}</math>
 
:<math> = \frac{h\Delta x + (r_2-r_1)\Delta y}{|O_1O_2|^2}</math>
 
After locating <math>P_2</math>, we just construct the line passing through <math>P_2</math> that is perpendicular to <math>O_2P_2</math> and this will also be tangent to the first circle at <math>P_1</math>.
 
 
====Three mutual tangents====
 
The case of three mutual tangents occurs when the two circles are externally tangent that is, when the sum of their radii equals the distance between their centres. In this case two of the three tangents (the "outer" ones) can be found using the formula in the previous section, and the third, which passes "between" the circles, may easily be found by first finding the point of mutual tangency (see section [[#One point of intersection|One point of intersection]] above).
 
 
====Four mutual tangents====
 
If two circles do not intersect or overlap at all, that is, the distance between their centres is greater than the sum of their radii, they will have four mutual tangents. Two of the mutual tangents can be found using the formulas two sections above. These are the two "outer" tangents, with the property that both circles lie on the same side of an outer tangent. There are also two "inner" tangents that "separate" the two circles from each other.
 
 
The procedure for computing these "inner" tangents is similar to the procedure for computing the "outer" tangents. We begin by letting <math>l</math> be one of these tangents, that touches the first circle at <math>P_1</math> and the second at <math>P_2</math>. Let the point of intersection of <math>l</math> with <math>O_1O_2</math> be denoted <math>Q</math>. Then, right-angled <math>\triangle O_1P_1Q</math> is similar to right-angled <math>\triangle O_2P_2Q</math>. Let <math>d_1 = |O_1Q|</math> and <math>d_2 = |O_2Q|</math>. Then, by similarity, we see that <math>\frac{d_1}{r_1} = \frac{d_2}{r_2}</math>. Along with the equation <math>d_1 + d_2 = |O_1O_2|</math>, we can solve: <math>d_1 = \frac{r_1}{r_1+r_2}|O_1O_2|, d_2 = \frac{r_2}{r_1+r_2}|O_1O_2|</math>. Then, letting <math>\theta = \angle P_2O_2Q</math>, we have <math>\cos \theta = \frac{r_2}{d_2} = \frac{r_1+r_2}{|O_1O_2|}</math>. Let <math>k = |P_1P_2|</math>, and see that <math>k = |QP_1| + |QP_2| = \sqrt{d_1^2 - r_1^2} + \sqrt{d_2^2 - r_2^2}</math>, which, after some messy algebra, will turn out to be <math>\sqrt{|O_1O_2|^2 - (r_1+r_2)^2}</math>. Also observe that <math>\sin \theta = \frac{|P_2Q|}{|O_2Q|} = \frac{|P_1Q|}{|O_1Q|} = \frac{|P_1Q|+|P_2Q|}{|O_1Q|+|O_2Q|} = \frac{k}{|O_1O_2|}</math>.
 
 
Moving along, we have <math>P_2 = O_2 + r_2 [\cos (\operatorname{atan2}(O_1-O_2) + \theta), \sin (\operatorname{atan2}(O_1-O_2) + \theta)]</math> as before. Defining <math>\Delta x = O_{1_x}-O_{2_x}</math> and <math>\Delta y</math> analogously, we obtain:
 
:<math>\cos( \operatorname{atan2}(O_1 - O_2) + \theta)</math>
 
:<math> = \cos \operatorname{atan2}(O_1 - O_2) \cos \theta - \sin \operatorname{atan2}(O_1 - O_2) \sin \theta</math>
 
:<math> = \frac{\Delta x}{\Delta x^2 + \Delta y^2} \frac{r_1+r_2}{|O_1O_2|} - \frac{\Delta y}{\Delta x^2 + \Delta y^2} \frac{k}{|O_1O_2|}</math>
 
:<math> = \frac{(r_1+r_2)\Delta x - k\Delta y}{|O_1O_2|^2}</math>
 
and likewise
 
:<math>\sin( \operatorname{atan2}(O_1 - O_2) + \theta)</math>
 
:<math> = \sin \operatorname{atan2}(O_1 - O_2) \cos \theta + \cos \operatorname{atan2}(O_1 - O_2) \sin \theta</math>
 
:<math> = \frac{\Delta y}{\Delta x^2 + \Delta y^2} \frac{r_1+r_2}{|O_1O_2|} + \frac{\Delta x}{\Delta x^2 + \Delta y^2} \frac{k}{|O_1O_2|}</math>
 
:<math> = \frac{k\Delta x + (r_1+r_2)\Delta y}{|O_1O_2|^2}</math>
 
 
Careful! The value of <math>h</math> in this case is not the same as the value of <math>h</math> for the outer tangents.
 
 
====Summary and discussion====
 
We will summarize the results below. (Note that we have exchanged the two circles.) We define <math>h = \sqrt{|O_1O_2|^2 - (r_1-r_2)^2}</math> and <math>k = \sqrt{|O_1O_2|^2 - (r_1+r_2)^2}</math>, and then the points of tangency of the two outer tangents with the first circle are given by:
 
:<math>P_1 = O_1 + \frac{r_1}{|O_1O_2|^2} [(r_1-r_2)\Delta x - h\Delta y, h\Delta x + (r_1-r_2)\Delta y]</math>
 
:<math>P_2 = O_1 + \frac{r_1}{|O_1O_2|^2} [(r_1-r_2)\Delta x + h\Delta y, -h\Delta x + (r_1-r_2)\Delta y]</math>
 
and likewise for the two inner tangents:
 
:<math>P_3 = O_1 + \frac{r_1}{|O_1O_2|^2} [(r_1+r_2)\Delta x - k\Delta y, k\Delta x + (r_1+r_2)\Delta y]</math>
 
:<math>P_4 = O_1 + \frac{r_1}{|O_1O_2|^2} [(r_1+r_2)\Delta x + k\Delta y, -k\Delta x + (r_1+r_2)\Delta y]</math>
 
 
The first pair of formulas will be invalid when <math>|r_1 - r_2| > |O_1O_2|</math>, because <math>h</math> will be undefined. Obviously, in this case, <math>r_1 + r_2 > |O_1O_2|</math> also, so <math>k</math> will also be undefined, and the second pair of formulas will be invalid.
 
 
When <math>|r_1 - r_2| = |O_1O_2|</math>, we will obtain <math>h = 0</math>, so <math>P_1</math> and <math>P_2</math> will coincide. The second pair of formulas will still be invalid, so we will obtain a total of one point of tangency.
 
 
When <math>|r_1 - r_2| < |O_1O_2| < r_1 + r_2</math>, we will obtain <math>h > 0</math>, so <math>P_1</math> and <math>P_2</math> will be distinct, but <math>k</math> will still be undefined, so we obtain a total of two points of tangency.
 
 
When <math>|O_1O_2| = r_1 + r_2</math>, we will obtain <math>h > 0, k = 0</math> giving two distinct points <math>P_1, P_2</math>, but <math>P_3 = P_4</math>, a total of three points of tangency.
 
 
Finally, when <math>|O_1O_2| > r_1 + r_2</math>, then <math>h, k \geq 0</math> and all four points <math>P_1, P_2, P_3, P_4</math> will be distinct, as expected.
 
  
 
[[Category:Algorithms]]
 
[[Category:Algorithms]]
 
[[Category:Geometry]]
 
[[Category:Geometry]]
 
[[Category:Pages needing diagrams]]
 
[[Category:Pages needing diagrams]]

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)

Templates used on this page: