6.9 An Extended Example (Coordinate Geometry)

Example:

Write a library module to implement support for planar point coordinate geometry.

Discussion:

Points are represented in the plane by their horizontal and vertical coordinates (x, y). This representation can be rendered in Modula-2 as a one-dimensional array of length two.

TYPE
Point = ARRAY [1 .. 2] OFREAL;

Note that the word dimension in this context refers to the array, not to the geometry. There are several useful operations relating to single points. These fall into two main categories:

Computations:

Three of these will be considered here; they arise in connection with the line segment joining the point with the origin.

-->

Both the length r of this line segment, and also the angle that it makes with the positive x-axis are of interest in various problems. It may also be necessary to construct the rectangular coordinates (x, y) of a point from a knowledge of the length r and the angle (also called its polar coordinates). These are expressed as follows:

--> -->

Transformations:

These involve moving the point P according to some determined pattern. The first such patterns involve various reflections or symmetries. These can be done in the x-axis to yield (x, -y), in the y-axis to yield (-x, y), across the origin yielding (-x, -y), or in the line y = x producing (y, x). The possibilities are indicated in the figure 6.14 below:

-->

The second type of transformation scales the point in such a way that the length of the line segment it defines is multiplied by some number, s called the scale factor. If this is done, the x-coordinate and y-coordinate of the new point Q are also multiplied by the same scale factor, but the angle is unchanged.

-->

A third type of transformation on the point is via the segment it defines and consists of a rotation through an angle ø. The new point obtained is at the same distance r from the origin as the original, but its segment subtends an angle q + ø from the positive x-axis.

-->

The fourth and last transformation is a simple shift or translation of the point by h units horizontally, and k units vertically. That is, the point that starts at (x, y) is transformed to the point (x + h, y + k).

Finally, in order to maintain the integrity of items of type Point as an abstract data type, it is necessary to make provision for client programs to assign values to (and obtain values from) the coordinates of a point without directly using the knowledge of its structure as an array. This concern for integrity of the ADT necessitates three more procedures: assign, abscissa, and ordinate.

Thus the abstract data type Point and the operations and transformations on it may be defined in Modula-2 as follows:

Problem:

Write a routine to compute the distance between two points.

Discussion:

This problem can be solved, as suggested in exercise 4.19 using the formula

-->

and employing the coordinates of the points directly. In the context of this discussion, having imported the type Point and the procedure sqrt into the surrounding module, the requested routine could be expressed as:

A second approach is more complicated in some ways, but trades off some additional mathematical steps for a more abstract view of the type Point within the procedure. Rather than have the procedure made direct use of the structure of the type Point by basing its computations on the coordinates of the point, it can make the calculation by using facilities provided by the module Points alone. This approach is consistent with the inclusion in the module of procedures to extract the coordinates without directly using the data structure. In this particular instance the law of cosines is exploited. (A proof of the law of cosines is not presented here).

-->

The Law of Cosines: In any triangle ABC with sides opposite the angles labelled a, b, and c,

Here, the measure of angle C is - ø and the distance between the two points is the length of the third side in a triangle whose other sides are the segments from the origins to the points A and B. Thus one could write

Sample Run:

This program computes the distance between two points
in the coordinate plane.
First point, X coordinate: Please type in a real number ===> 5.5
Second point, X coordinate: Please type in a real number ===> 26.5
The distance between the two points is 29.00000 units.