Graphics Reference
In-Depth Information
Exercise 7.19: (a) You're given two nondegenerate line segments in the plane,
the first with endpoints A and B and the second with endpoints C and D , and all
coordinates are integers. Your job is to determine whether the segments intersect,
and the intersection point, if any.
(a) Write a short program to do this. If the segments are parallel, there are three
cases: They're disjoint (return false ), they share an endpoint (return false ), or
they overlap in an interval (return true , but don't return an intersection point, since
it's not unique). If the segments are nonparallel and intersect, they may share an
endpoint (return false ), the endpoint of one may be interior to the other (return
true , and the endpoint), or the interiors of the segments may intersect at some
point ( x , y ) whose coordinates may not be integers, but will be rational numbers.
In this case, you should return an integer triple ( x , y , w ) , where the intersection is
at ( x
w ) .
(b) Explain why returning an integer triple is more useful than returning a floating-
point representation of the rational coordinates.
(c) Suppose you have two integer triples of the form above, ( x 1 , y 1 , w 1 ) and
( x 2 , y 2 , w 2 ) . How would you test them for “equality,” that is, how would you test
whether they represent the same rational point in the plane?
(d) For an extra challenge, try to write all your code in part (a) so that there are
no divisions, and the code is as clean as possible. The sort of multiple-case mess
that's implicit in programs like this arises from the multiple possible ways that
segments can intersect; you can't really make the code too clean.
/
w , y
/
Search WWH ::




Custom Search