Example: If we are using snapping, then as the mouse cursor approaches an object, we would like to snap the mouse cursor to the nearest point on the object
Example: Finding the point on a circle where a line that connects the centers of two circles intersects the circle
F(x,y) = 0Example: The equation for a line is Ax + By + C = 0
Example: The implicit equation for a circle whose center lies at the origin is
F(x, y) = x2 + y2 - r2where r is the radius of the circle. The following statements are all true:
Example: A circle has the following parametric equations
x = xc + R*cos 2πt y = yc + R*sin 2πtwhere t is in the range [0,1)
y = slope * x + c.where c is often called the y-intercept (where the line intercepts the y-axis). In an interface, we typically are given the two endpoints of a line line as P1 and P2. However, the book uses A and B as the two endpoints so I will use them here as well. Then the above equation can be rewritten as:
B.y - A.y y = ----------- * x + c B.x - A.xMultiplying both sides by (B.x - A.x) yields:
(B.x - A.x) * y = (B.y - A.y) * x + cWe can now easily derive the implicit equation by moving y to the right side of the equation:
0 = (B.y - A.y) * x - (B.x - A.x) * y + cClearly A = B.y - A.y and B = -(B.x - A.x) or alternatively, (A.x - B.x).
How to derive c:
ax + by + c = 0 => c = -ax - by => c = -aA.x - bA.y (fill in one of the known pts for the line)
dist(M,a,b,c) = (a*M.x + b*M.y + c) / line seg length
Nearest point = (x,y) - Line(x,y) * [a,b] / size(a,b)or
Nx = x - a * Line(x,y) / size(a,b) Ny = y - b * Line(x,y) / size(a,b)
x = A.x + t(B.x-A.x) y = A.y + t(B.y-A.y)
Given a center point and a point on the circle, the radius can be derived as:
radius = sqrt((xp - xc)2 + (yp - yc)2)
x = xc + R * cos 2πt y = yc + R * sin 2πtwhere t is in the range [0,1)
dist(x,y) = sqrt((x - xc)2 + (y - yc)2) - R
[x - xc, y - yc] is the vectorLet len = sqrt((x - xc)2 + (y - yc)2)
xn = xc + R * (x - xc) / len yn = yc + R * (y - yc) / len
(x - xc)2 (y - yc)2 --------- + --------- - 1 = 0 a2 b2
x = xc + a * cos(2πt) y = yc + b * sin(2πt)
dy = M.y - C.y dx = M.x - C.xThe point that lies on the ellipse satisfies the equation (a*cos(2πt), b*sin(2πt)). Using similar triangles, we know that:
dy / dx = (b*sin(2πt)) / (a*cos(2πt)) => (a / b) * (dy / dx) = sin(2πt) / cos(2πt) => (a / b) * (dy / dx) = tan(2πt) => tan-1[(a / b) * (dy / dx)] = 2πt => arctan2(a * dy, b * dx) = 2πtYou can now plug the arctan2 expression into the parametric equations to get the (x,y) point corresponding to the nearest point on the ellipse
Here is one set of calculations for selecting the nearest point on an arc:
t = angle / (2*pi) angle = cos-1[(M.x - C.x / |M-C|] or sin-1[(M.y - C.y / |M-C|] or tan-1[(M.y - C.y) / (M.x - C.x)]where M.x is the x-coordinate of the mouse point and C.x is the x-coordinate of the center point. if t lies between sp and fp then we have the nearest point.
This last equation can be modified for our distance calculation.
t = cos-1[(x - xc) / len] / (2*pi)where len = sqrt((x - xc)2 + (y - yc)2)
x - xc cos-1[----------------------------] sqrt((x - xc)2 + (y - yc)2) t = ------------------------------------ 2*pi
Note that both C(1) and C(2) continuity allow the joined surface to continue either in the same direction, or to reverse direction. You might initially think that C(2) continuity precludes the joined surface from reversing direction, but that is not true (e.g., try joining x3 and -x3 at the origin with x3 going from (-∞,0] and -x3 going from [0,∞)).