```I. Topics Addressed in the Chapter

A. Geometry of almost all drawing objects is based on a set of control
points

Examples: A line is defined by its two endpoints
A rectangle is defined by its upper left and lower right
corners or, alternatively, by its upper left corner
and its width and height

B. Distance from a Point to an Object: Used for selection

C. Bounds of an Object

1. Bounding Box: The smallest rectangle that completely encloses
an object

2. Uses

a. display: determines the regions of the screen that must be
redrawn when the object is modified

b. simplifies containment checks: if we know the bounding box
for a complex object, we can check to see whether a point
lies within the bounding box before performing a more
mathematically complicated containment check

D. Nearest Point to an Object: Useful when we are not trying to select
an object but rather some point on the object

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

E. Intersections: Determines the point(s) where two objects intersect

Example: Finding the point on a circle where a line that connects the
centers of two circles intersects the circle

F. Inside/Outside: For filled objects we often want to know whether
a point is inside or outside the object

II. Geometric Equations: We need equations that we can solve to answer
the above questions

A. Implicit Equations: Implicit equations have the form

F(x,y) = 0

Example: The equation for a line is Ax + By + C = 0

a. Implicit equations often divide a plane into two regions and
can help us determine whether a point lies inside or outside
a region

Example: The implicit equation for a circle whose center lies
at the origin is

F(x, y) = x2 + y2 - r2

where r is the radius of the circle. The following statements
are all true:

F(x,y) > 0: point is outside the circle
F(x,y) < 0: point is inside the circle
F(x,y) = 0: point is on the boundary of the circle

b. We can often normalize an implicit equation so that it returns
a distance of a point from an object. Normalization is
accomplished by dividing by constant coefficients

B. Parametric Equations: Parametric equations are ones in which the
x and y points are determined by one or more parameters:

Example: A circle has the following parametric equations

x = xc + (R * cos [2 * pi * t])
y = yc + (R * sin [2 * pi * t])

t is in the range [0,1)

1. Parametric equations are good for calculating the nearest point on
a shape using a variation of binary search (Figure 12.19). The book's
algorithm divides the t-interval into 10 increments with 11
"dividers".
a. The leftmost divider is the low point and the right most divider
is the hight point
b. Each divider is a point on the parametric shape.
c. At each divider the algorithm computes the distance from the mouse
to the parametric shape. It selects the closest divider point from
the 11 it computes, then narrows the interval to the divider to the
left and right of this divider point.
d. The interval is again divided into 10 increments and the same
computation is performed.
e. When the distance between the low and high point is less than 1,
the algorithm returns the point, because at a distance of less than
1, you're close enough for pixel resolution

III. Path-Defined Shapes (i.e., shapes that are not filled)

A. Lines

1. Implicit Equation: ax + by + c = 0

a. The vector formed by [a, b] is perpendicular to the line.
The direction of the line is given by the vector [-b,a].
i. A vector has a direction and a magnitude
ii. The unit vector is a vector with a magnitude of 1: Is
useful because it can provide a standardized basis
for providing a direction

b. The implicit equation can be derived from the perpendicular
property and the endpoints (assume A and B are the endpoints)

a = B.y - A.y
b = -(B.x - A.x) = 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)

c. If we normalize the implicit equation by converting the
vector [b, -a] to a unit vector, then the implicit equation will
return the distance of a point from the line

i. line seg length = sqrt(a2 + b2)
ii. Line(x,y) = (ax + by + c) / line seg length
iii. Distance from a mouse point to a line is given by the
equation:

dist(M,a,b,c) = (a*M.x + b*M.y + c) / line seg length

d. Nearest point calculation

i. The normalized line yields the distance of the point from
the line

ii. This distance measures the perpendicular distance of the
point from the line

iii. The implication of points (i) and (ii) is that we can
follow the line's perpendicular vector to find the nearest
point:

Nearest point = (x,y) - Line(x,y) * [a,b]

or

Nx = x - A * Line(x,y)
Ny = y - B * Line(x,y)

iv. The disadvantage of this calculation is that a line is
infinite but your line segment is finite. It may well be
that (Nx, Ny) does not fall on your line segment because
it falls outside the endpoints, A and B.

2. Parametric Equations: L = A + t(B - A)

x = A.x + t(B.x-A.x)
y = A.y + t(B.y-A.y)

a. These equations are often used when animating the motion of
an object between two points. t represents the proportion of
time that has passed thus far in the animation.

b. Figure 12.22 in the book shows how to compute the nearest
point L, on a parametric line, to point M. The advantage of
this equation is that it solves for t and hence you know whether
or not L falls on the line segment (it does if t is in the
range [0,1]). The rationale for the calculation is that it
minimizes the distance of the distance calculation
dist2(L,M) by taking the derivative of the equation
with respect to t
and setting the derivative to 0. Remember that when the
derivative is 0 you have either a local min or local max. In
this case the distance has an unbounded max so you know that
when the derivative is 0, you have a min. Solving the equation
for t and plugging t back into your parametric equation for
the line gives you the nearest point.

3. Convex Hull Property: The convex hull of a set of points is the
smallest polygon that will contain all of the points

a. Convex Hull Property of a Geometric Object: All points on the
object are guaranteed to lie in the convex hull of that object's
control points

b. If an object satisfies the convex hull property then its bounding
box can be obtained from the control points

c. A line fulfills the convex hull property

B. Circles

1. A circle can be defined in two ways

a. By a center point and a radius
b. By a center point and a point on the circle

Given a center point and a point on the circle, the radius can
be derived as:

radius = sqrt((xp - xc)2 + (yp - yc)2)

2. Implicit equation: (x - xc)2 + (y - yc)2 - R2 = 0

3. Parametric Equations

x = xc + (R * cos [2 * pi * t])
y = yc + (R * sin [2 * pi * t])

t is in the range [0,1)

4. Distance from a Point to a Circle

dist(x,y) = sqrt((x - xc)2 + (y - yc)2) - R

5. Containment of a Point in the Circle: If F(x,y) represents the
implicit equation for the circle, then:

F(x,y) > 0: point is outside the circle
F(x,y) < 0: point is inside the circle
F(x,y) = 0: point is on the boundary of the circle

a. If containment is the only property we care about, then use
the implicit equation rather than finding the distance and
determining if the distance is positive or negative.
Computing the distance requires a square root to be computed
and that is an expensive operation

6. Nearest Point on a Circle

a. The nearest point on the circle is on the line that goes from
the center of the circle to (x,y).

b. If we think of this line as a vector, then we can

i. normalize its length to 1,
ii. multiply the resulting vector by R, and
iii. add the circle's center point to the resulting vector to
obtain the nearest point

[x - xc, y - yc] is the vector

Let len = sqrt((x - xc)2 + (y - yc)2)

Then:

the normalized vector is [(x - xc) / len, (y - yc) / len]

the nearest point on the circle is

xn = xc + R * (x - xc) / len
yn = yc + R * (y - yc) / len

C. Ellipses

1. Think of an ellipse as a stretched unit circle, with the
x dimension stretched for a distance of a and the y dimension
stretched for a distance of b. a equals half the actual width
of the ellipse and b equals half the acutal height of the
ellipse

2. Control points: upper left and lower right corners of the
ellipse

i. a upper left corner and width/height model is also often
used in graphics packages

3. Implicit Equation: The implicit equation for an ellipse can
be derived from the implicit equation for a circle. First
normalize the circle's equation by dividing by R, then replace
R by a and b, the new radii for the ellipse

(x - xc)2   (y - yc)2
--------- + --------- - 1 = 0
a2          b2

4. Parametric Equations

x = xc + a * cos(2*pi*t)
y = yc + b * sin(2*pi*t)

5. Nearest point: Ignore the book. There is an easy way to
find the nearest point based on similar triangles. Let M be
your mouse point and C be the center of your ellipse. You know
that the nearest point is on the line defined by the vector
M-C. Let:
dy = M.y - C.y
dx = M.x - C.x

The point that lies on the ellipse satisfies the equation
(a*cos(2*pi*t), b*sin(2*pi*t)). Using similar triangles, we
know that:

dy / dx = (b*sin(2*pi*t)) / (a*cos(2*pi*t))
=>  (a / b) * (dy / dx) = sin(2*pi*t) / cos(2*pi*t)
=>  (a / b) * (dy / dx) = tan(2*pi*t)
=>  (a / b) * (dy / dx) = tan(2*pi*t)
=>  tan-1[(a / b) * (dy / dx)] = 2*pi*t
=>  arctan2(a * dy, b * dx) = 2*pi*t

You can now plug the arctan2 expression into the parametric
equations to get the (x,y) point corresponding to the nearest
point on the ellipse

6. Distance calculation: Calculate the nearest point L, then
calculate the length of the line segment M-L.

7. Containment of a Point in the Ellipse: If F(x,y) represents the
implicit equation for the ellipse, then:

F(x,y) > 0: point is outside the ellipse
F(x,y) < 0: point is inside the ellipse
F(x,y) = 0: point is on the boundary of the ellipse

a. If containment is the only property we care about, then use
the implicit equation rather than finding the distance and
determining if the distance is positive or negative.
Computing the distance requires a square root to be computed
and that is an expensive operation

D. Arcs: It's easiest to use a parametric form for arcs since they
comprise a subpart of a circle or ellipse. For simplicity we will
only consider circular arcs. You can use the same mathematical
treatment shown below to derive similar equations for elliptical
arcs

a. Parametric Equations

Let sp = the start point (a real number between 0 and 1)

fp = the stop point

Then

x = xc + R * cos[2*pi*(sp + t * (fp - sp))]
y = yc + R * sin[2*pi*(sp + t * (fp - sp))]

b. Distance of a point from an arc/Nearest point: typically you
care about the nearest point only when the distance from the
point to the arc is small. You can use the distance and nearest
point computations for a circle with the caveat that your
point must be close to the arc for the calculation to make
sense. If the point is far away from the arc,
then the distance calculation will
yield the wrong result because the distance will be to a
point on the opposite side of a circle.

Here is one set of calculations for selecting the
nearest point on an arc:

1) Use the nearest point circle equation

2) Determine if the computed nearest point actually lies
on the arc by solving for the angle that the mouse point
makes with the center of the circle and calculating t as:

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.
1) Calculate the distance using the circle distance formula. If
the distance lies within the prescribed threshold, then

2) Determine if the point lies within the proper t coordinates
by calculating:

t = cos-1[(x - xc) / len] / (2*pi)

where len = sqrt((x - xc)2 + (y - yc)2)

Note that:

1) a nearest point calculation is not required--we can
use x rather than xn

2) you already have computed len when you did the
distance calculation so it does not introduce an

c. Bounds of an Arc

i. An arc does not satisfy the convex hull property

ii. Calculate the bounding box from the set
t = {sp, fp, 0.0, 0.25, 0.5, and 0.75}.
In this case use the parametric
equations for a circle, not the arc (e.g., use cos(2*pi*t)
not cos(2*pi*(sp + t * (fp - sp)))).
Eliminate any point that does not lie between sp and fp.
The values 0.0, 0.25, 0.5, and 0.75 will test the 0, 90,
180, and 270 degree values of the circle, which are its
4 extrema.

4. Containment of a point in an arc

a) Check for containment using the circle test

b) Check for angle containment by calculating the angle, converting
it to a number between 0 and 1, and comparing it with the bounds
for t:
x - xc
cos-1[----------------------------]
sqrt((x - xc)2 + (y - yc)2)
t = ------------------------------------
2*pi

E. Curves: Curves are normally constructed in graphical interfaces
using piecewise cubic curves. The reason for using cubic curves
is that curves with more points have polynomial equations that
become too complicated.

a. Control points: Cubic curves are defined using four control
points

b. Continuity requirements: When piecing together cubic curves,
you normally want the curves to flow smoothly at the
meeting points.

i. C(0) continuity: two adjacent curves share the same point
where they join.

ii. C(1) continuity: the first derivatives are identical at the
join point. This property ensures that there is a smooth
transition between curves with no sharp points, because the
tangent vectors of each curve are aligned at the join
point. This property is the most common requirement for
piecing together smooth curves

iii. C(2) continuity: the curvatures are the same across the
join point

c. Bezier curves

i. defined by two endpoints and two intermediate control points

ii. The curves can be made more pronounced by pulling on the
two intermediate control points

iii. C(0) continuity is obtained by making the P1 point of
the second curve be the P4 point of the first curve

iv. C(1) continuity is achieved by making P3-P4 of the first
curve collinear with P1-P2 of the second curve (i.e.,
P3-(P4/P1)-P2 lie on the same line). Most interactive
packages will automatically maintain this continuity

v. C(2) continuity is foregone

vi. Bezier curves obey the convex hull property

d. B-splines

i. Each curve shares three points with the preceding and
succeeding curve

ii. C(0), C(1), and C(2) continuity are all achieved

iii. The curve may not go through any of the control points

iv. B-splines obey the convex hull property

v. Simplifies the interaction with complex curves

e. Catmull-Rom curves

i. Guarantees that the curve goes through all but the first
and last control points

ii. Guarantees C(0) and C(1) continuity, but not C(2)
continuity

iii. Like B-splines in that each curve shares three points
with the preceding and succeeding curve

iv. Catmull-Rom curves do not satisfy the convex hull
property. However, Catmull-Rom geometry can be converted
into Bezier geometry using calculations shown in the
book and from the converted geometry a bounding box can
be calculated

F. Polygons
1. Nearest point/nearest distance: Compare point with each of the
line segments and use equations for a line
2. Bounding box: Is the maximum and minimum of the vertex points in
X/Y
3. Containment of a point:
a. First check the bounding box. If it's in the bounding box, then
do the odd-even test

b. Odd-Even test: Draw a horizontal line through the point and count
the number of intersections of the line with the shape to the right
of the point. If the number is odd then the point is inside the
shape, otherwise it is outside the shape

c. Refinements to the odd-even test

i. If the line intersects the shape at an edge intersection. then
count the intersection twice if the edges are tangential to
the line and once if the line passes through the edges

ii. to compute tangency, traverse the edges counterclockwise and
compute the dY of the point. If the sign changes you have
tangency; otherwise the line goes through the edges

```