Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

1.7 The Line Segment ADT

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

ADT Definition

The Line Segment ADT

A line segment is part of a straight line that is bounded by two end points. The endpoints are specified in the two-dimensional Cartesian coordinate system.

  • LineSegment(x0, y0, x1, y1)

    Creates a new line Segment instance defined by the two Cartesian coordinates (x0, y0) and (x1, y1).

  • endPointA()

    Returns the first endpoint of the line as a 2-tuple (x, y).

  • endPointB()

    Returns the second endpoint of the line as a 2-tuple (x, y).

  • isVertical()

    Returns a Boolean value that indicates whether the line segment is parallel to the y-axis.

  • isHorizontal()

    Returns a Boolean value that indicates whether the line segment is parallel to the x-axis.

  • isParallel(otherLine)

    Returns a Boolean value that indicates whether the line segment is parallel to the otherLine.

  • length()

    Returns the length of the line segment given as the Euclidean distance between the two endpoints.

  • shift(xInc, yInc)

    Returns a new LineSegment instance that is the result of shifting this line segment by xInc amount along the x-axis and yInc amount along the y-axis.

  • slope()

    Returns the slope of the line segment given as the rise over the run. The line segment can not be vertical.

  • midpoint()

    Returns the midpoint of the line segment as a 2-tuple (x, y).

  • toString()

    Returns a string representation of the line segment in the format (Ax, Ay)\#(Bx, By).

Using the Line Segment ADT

To 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 distance.py program below. This program uses a function that reads and returns the x- and y-coordinates of each move. See the Special Topic later on the page for a refresher on functions that return multiple values.

Program Listing
Program: distance.py
  1. # Compute the total distance moved in a game using instances of
  2. # the Line Segment ADT.  
  3. from line import LineSegment
  4.  
  5. def main() :
  6.    # Read the position of the first move from the user.  
  7.   firstMove = getPosition("first")
  8.   prevMove = firstMove
  9.  
  10.    # Read succeeding positions and compute the total distance.
  11.   totalDist = 0
  12.   nextMove = getPosition("next")
  13.   while nextMove is not None :
  14.     line = LineSegment(prevMove[0], prevMove[1], nextMove[0], nextMove[1])
  15.     dist = line.length()
  16.     totalDist = totalDist + dist
  17.     prevMove = nextMove
  18.     nextMove = getPosition("next")
  19.    
  20.    # Print the results.
  21.   print("The total distance moved =", totalDist)
  22.  
  23. # Read the coordinates of a move from the user and return None or (x, y).
  24. def getPosition(whichMove) :
  25.   print("Enter the coordinates of the", whichMove, "move." )
  26.   xCoord = int(input("Enter the x-coord (or < 0 to quit):"))
  27.  
  28.    # If x-coord is negative return None to flag the end.
  29.   if xCoord < 0 :
  30.     return None
  31.    
  32.    # Otherwise, read the y-coord and return a 2-tuple.
  33.   yCoord = int(input("Enter the y-coord:"))
  34.   return (xCoord, yCoord)
  35.  
  36. main()
Special Topic
Returning Multiple Values
Page last modified on July 30, 2023, at 02:18 PM