Java Reference
In-Depth Information
PolyLine
ListPoint end;
ListPoint start;
refers to
refers to
ListPoint
ListPoint
ListPoint
ListPoint
double x;
double y;
ListPoint next;
double x;
double y;
ListPoint next;
double x;
double y;
ListPoint next;
double x;
double y;
ListPoint next;
null
This illustrates the basic structure we might have for a linked list of points stored as a PolyLine . The points
are stored as members of ListPoint objects. As well as constructors the PolyLine class will need a
method to add points, but before we look into that, let's consider the ListPoint class in more detail.
There are at least three approaches you could take to define the ListPoint class, and there are
arguments in favor of all three.
You could define the ListPoint class with the x and y coordinates stored explicitly. The
main argument against this would be that we have already encapsulated the properties of a
point in the Point class, so why not use it.
You could regard a ListPoint object as something that contains a reference to a Point
object, plus members that refer to previous and following ListPoint objects in the list. This
is not an unreasonable approach. It is easy to implement and not inconsistent with an intuitive
idea of a ListPoint .
You could view a ListPoint object as a specialized kind of Point so you would derive the
ListPoint class from Point . Whether or not this is reasonable depends on whether you see this
as valid. To my mind this is stretching the usual notion of a point somewhat - I would not use this.
The best option looks like the second approach. We could implement the ListPoint class with a data
member of type Point , which defines a basic point with its coordinates. A ListPoint object would
have an extra data member next of type ListPoint that is intended to contain a reference to the next
object in the list. With this arrangement, you can find all the points in a Polyline object by starting
with its start member that stores a reference to its first ListPoint object. This contains a reference
to the next ListPoint object in its next member, which in turn contains a reference to the next, and
so on through to the last ListPoint object. You will know it is the last one because its next member
that usually points to the next ListPoint object will be null . Let's try it.
Search WWH ::




Custom Search