1.7 The Line Segment ADTA line segment is part of a straight line that is bounded by two endpoints. While line segments are commonly used in geometry, they are also quite useful in computer applications that graphically display data. For example, line segments are commonly used to display data in a line graph and for graphically representing the stops and turns along a route or path on maps. We can define an abstract data type to represent a line segment for use in computer applications.
The Line Segment ADT
Using the Line Segment ADTTo illustrate the use of the abstract data type without knowing how it is implemented, consider the following problem. Compute the total distance of a path created from moves in a game by reading a sequence of 2-D Cartesian coordinates from the user that represents each move in the game. Assume there is at least one move and that all coordinates are in the first quadrant (both coordinates will be non-negative). To indicate the end of the sequence, the user enters a negative x-coordinate. Even though we are not going to display the path of the moves in a graphical application, we can still use the Line Segment ADT to compute the distance between individual moves. A solution for this problem is provided in Program Listing
|