- Topics Addressed in the Chapter
- 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 (selection handles added to the edges of a rectangle typically permit only the width or height to be adjusted).

- Distance from a Point to an Object: Used for selection
- Bounds of an Object
- Bounding Box: The smallest rectangle that completely encloses
an object
- Uses
- display: determines the regions of the screen that must be
redrawn when the object is modified
- 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

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

- Bounding Box: The smallest rectangle that completely encloses
an object
- 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

- 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

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

- Geometry of almost all drawing objects is based on a set of control
points
- Geometric Equations: We need equations that we can solve to answer
the above questions
- Implicit Equations: Implicit equations have the form
F(x,y) = 0

Example: The equation for a line is Ax + By + C = 0- Advantages
- 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) = x

where r is the radius of the circle. The following statements are all true:^{2}+ y^{2}- r^{2}- 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

- 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

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

- Advantages
- 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 = x

where t is in the range [0,1)_{c}+ R*cos 2πt y = y_{c}+ R*sin 2πt- 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". The initial dividers are 0, .1, .2, ..., .9, 1.0.
- Each divider represents a point on the parametric shape.
- 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. For example if the mouse is closest to the parametric shape at t = 0.6, then the next interval will be chosen as t = [0.5, 0.7], which are the intervals to the left and right of 0.6.
- The interval is again divided into 10 increments and the same computation is performed.
- When the distance between the points represented by the beginning and ending of the t-range is less than 1, the algorithm returns the point at the middle of the t-range, because at a distance of less than 1, you're close enough for pixel resolution. For example, suppose the interval has been reduced to t = [0.54, 0.56] and that the distance between the points at (x(.54), y(.54)) and (x(.56), y(.56)) is less than 1
- Then we would return the point (x(.55), y(.55)) as the nearest point to the mouse.

- Parametric equations are also good for handling animated movement, because it is easy to vary the t parameter

- 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". The initial dividers are 0, .1, .2, ..., .9, 1.0.

- Implicit Equations: Implicit equations have the form
- Path-Defined Shapes (i.e., shapes that are not filled)
- Lines
- Implicit Equation: ax + by + c = 0
- To derive the coefficients, remember that the
familiar version of the line is:
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.x

Multiplying both sides by (B.x - A.x) yields:(B.x - A.x) * y = (B.y - A.y) * x + c

We 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 + c

Clearly 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)

- We can normalize the implicit equation by dividing
the coefficients of the implicit equation by the
length of the line segment. If we do so, then
the implicit equation will
return the distance of a point from the line segment.
Note that the coefficients a and b capture the x and
y increments of the line segment, so we can obtain
the line segment length via the following equation:
- line seg length = sqrt(a
^{2}+ b^{2}) - Line(x,y) = (ax + by + c) / line seg length
- 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

- line seg length = sqrt(a
- Nearest point calculation
- The normalized line yields the distance of the point from
the line
- This distance measures the perpendicular distance of the
point from the line
- The implication of points (i) and (ii) is that we can
follow the line's normalized perpendicular vector to find the nearest
point:
Nearest point = (x,y) - Line(x,y) * [a,b] / size(a,b)

orNx = x - a * Line(x,y) / size(a,b) Ny = y - b * Line(x,y) / size(a,b)

- 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.

- The normalized line yields the distance of the point from
the line

- To derive the coefficients, remember that the
familiar version of the line is:
- Parametric Equations: L = A + t(B - A)
x = A.x + t(B.x-A.x) y = A.y + t(B.y-A.y)

- 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.
- 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
dist
^{2}(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.

- 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.
- Convex Hull Property: The convex hull of a set of points is the
smallest polygon that will contain all of the points
- 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
- If an object satisfies the convex hull property then its bounding
box can be obtained from the control points
- A line fulfills the convex hull property

- 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

- Implicit Equation: ax + by + c = 0
- Circles
- A circle can be defined in two ways
- By a center point and a radius
- 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((x

_{p}- x_{c})^{2}+ (y_{p}- y_{c})^{2})

- Implicit equation: (x - x
_{c})^{2}+ (y - y_{c})^{2}- R^{2}= 0 - Parametric Equations
x = x

where t is in the range [0,1)_{c}+ R * cos 2πt y = y_{c}+ R * sin 2πt - Distance from a Point to a Circle
dist(x,y) = sqrt((x - x

_{c})^{2}+ (y - y_{c})^{2}) - R - 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

- 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

- Nearest Point on a Circle
- The nearest point on the circle is on the line that goes from
the center of the circle to (x,y).
- If we think of this line as a vector, then we can
- normalize its length to 1,
- multiply the resulting vector by R, and
- add the circle's center point to the resulting vector to
obtain the nearest point
[x - x

Let len = sqrt((x - x_{c}, y - y_{c}] is the vector_{c})^{2}+ (y - y_{c})^{2})Then:

- the normalized vector is [(x - x
_{c}) / len, (y - y_{c}) / len] - the nearest point on the circle is
x

_{n}= x_{c}+ R * (x - x_{c}) / len y_{n}= y_{c}+ R * (y - y_{c}) / len

- the normalized vector is [(x - x

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

- A circle can be defined in two ways
- Ellipses
- 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
- Control points: upper left and lower right corners of the
ellipse
- a upper left corner and width/height model is also often used in graphics packages

- 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 - x

_{c})^{2}(y - y_{c})^{2}--------- + --------- - 1 = 0 a^{2}b^{2} - Parametric Equations
x = x

_{c}+ a * cos(2πt) y = y_{c}+ b * sin(2πt) - 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π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

You can now plug the atan2 expression into the parametric equations to get the (x,y) point corresponding to the nearest point on the ellipse. Note that atan2 is a function used in many programming languages that takes two arguments, so that it knows the signs of the numerator and denominator and hence can place the angle in the proper quadrant. With just one argument, there are two possible solutions.^{-1}[(a / b) * (dy / dx)] = 2πt => atan2(a * dy, b * dx) = 2πt - Distance calculation: Calculate the nearest point L, then
calculate the length of the line segment M-L.
- 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

- 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

- 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
- 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
- Parametric Equations
Let:

- s
_{p}= the start point (a real number between 0 and 1) - f
_{p}= the stop point

Then:

- x = x
_{c}+ R * cos[2*pi*(s_{p}+ t * (f_{p}- s_{p}))] - y = y
_{c}+ R * sin[2*pi*(s_{p}+ t * (f_{p}- s_{p}))]

- s
- 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:

- Use the nearest point circle equation
- 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

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 s^{-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)]_{p}and f_{p}then we have the nearest point.

This last equation can be modified for our distance calculation.

- Calculate the distance using the circle distance formula. If
the distance lies within the prescribed threshold, then
- Determine if the point lies within the proper t coordinates
by calculating:
t = cos

where len = sqrt((x - x^{-1}[(x - x_{c}) / len] / (2*pi)_{c})^{2}+ (y - y_{c})^{2})Note that:

- a nearest point calculation is not required--we can
use x rather than x
_{n} - you already have computed len when you did the distance calculation so it does not introduce an additional calculation.

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

- Use the nearest point circle equation
- Bounds of an Arc
- An arc does not satisfy the convex hull property
- Calculating the bounding box
- Calculate the bounding box from the set
t = {s
_{p}, f_{p}, 0.0, 0.25, 0.5, and 0.75}. - Use the parametric
equations for a circle, not the arc (i.e., use cos(2πt)
not cos(2π(s
_{p}+ t * (f_{p}- s_{p})))). - Eliminate any point that does not lie between s
_{p}and f_{p}. 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.

- Calculate the bounding box from the set
t = {s

- An arc does not satisfy the convex hull property
- Containment of a point in an arc
- Check for containment using the circle test
- 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 - x

_{c}cos^{-1}[----------------------------] sqrt((x - x_{c})^{2}+ (y - y_{c})^{2}) t = ------------------------------------ 2*pi

- Check for containment using the circle test

- Parametric Equations
- 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.
- Control points: Cubic curves are defined using four control
points
- Continuity requirements: When piecing together cubic curves,
you normally want the curves to flow smoothly at the
meeting points.
- C(0) continuity: two adjacent curves share the same point
where they join.
- 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
- C(2) continuity: the second derivatives are identical at the join point. Recall that in physics, the first derivative represents acceleration and the second derivative represents the change in velocity of the first derivative. hence when the second derivatives are equal, the two curves are accelerating at the same rate across the join point. This makes the joined surface look especially smooth. Wikipedia describes the joined surfaces as looking "perfectly smooth" and as causing the two joined surfaces to "appear as one".

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 x

^{3}and -x^{3}at the origin with x^{3}going from (-∞,0] and -x^{3}going from [0,∞)). - C(0) continuity: two adjacent curves share the same point
where they join.
- Bezier curves
- defined by two endpoints and two intermediate control points.
- The curves can be made more pronounced by pulling on the
two intermediate control points
- C(0) continuity is obtained by making the P1 point of
the second curve be the P4 point of the first curve
- 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
- C(2) continuity is foregone
- Bezier curves obey the convex hull property
- Advantages of Bezier curves
- An advantage of Bezier curves is that the curves
always go through the two endpoints so it is easy
to join two curves in precise places on the screen.
- The Bezier representation has a very efficient
algorithm for scan-converting cubic curves that
avoids repeated evaluation of the cubic polynomial
and thus increases drawing speed.
- You can obtain a bounding box for a Bezier curve
from its control points because it obeys the
convex hull property.
- Graphical editors make it easy to manipulate Bezier curves by automatically keeping the control points on two adjacent curves co-linear when interior control points (P2/P3) are manipulated.

- An advantage of Bezier curves is that the curves
always go through the two endpoints so it is easy
to join two curves in precise places on the screen.

- defined by two endpoints and two intermediate control points.
- B-splines
- Each curve shares three points with the preceding and
succeeding curve
- C(0), C(1), and C(2) continuity are all achieved
- The curve may not go through any of the control points,
which makes controlling B-splines more difficult because
it can make it difficult to join two splines at a
precise point on the screen and also harder to control
where the B-spline goes.
- B-splines obey the convex hull property
- Advantages of B-splines
- They have C(2) continuity, so they can produce
extremely smooth curves at the join points
- You can obtain a bounding box for a B-spline from its control points because it obeys the convex hull property.

- They have C(2) continuity, so they can produce
extremely smooth curves at the join points

- Each curve shares three points with the preceding and
succeeding curve
- Catmull-Rom curves
- Guarantees that the curve goes through all the control
points. However, a segment is usually thought of as being
defined by two control points and two additional control
points on either side of the endpoints. Thus the first and
last segments may have an additional control point to
their left and right respectively. In this case the
curve would pass through all but the first and last control
points.
- Guarantees C(0) and C(1) continuity, but not C(2)
continuity
- Like B-splines in that each curve shares three points
with the preceding and succeeding curve
- 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
- Advantage of Catmull-Rom curves: They pass through every control point, thus making them easy to control interactively.

- Guarantees that the curve goes through all the control
points. However, a segment is usually thought of as being
defined by two control points and two additional control
points on either side of the endpoints. Thus the first and
last segments may have an additional control point to
their left and right respectively. In this case the
curve would pass through all but the first and last control
points.

- Control points: Cubic curves are defined using four control
points
- Polygons
- Nearest point/nearest distance: Compare point with each of the
line segments and use equations for a line
- Bounding box: Is the maximum and minimum of the vertex points in
X/Y
- Containment of a point:
- First check the bounding box. If it's in the bounding box, then
do the odd-even test
- Odd-Even test: Draw a horizontal line through the point and count
the number of intersections of the line with the shape whose
x-coordinate is less than the mouse-point (i.e., to the left
of the point). If the number is odd then the point is inside the
shape, otherwise it is outside the shape. Intuitively, if you
start from outside the polygon, each time you cross a boundary
you transition from outside to inside, inside to outside, and
so on.
- Refinements to the odd-even test
- 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
- 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

- 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

- First check the bounding box. If it's in the bounding box, then
do the odd-even test

- Nearest point/nearest distance: Compare point with each of the
line segments and use equations for a line

- Lines