|
-
Aug 25th, 2001, 04:18 PM
#25
Lively Member
i'm having problems understanding the following code:
public class Point
{
// Create a point from its coordinates
public Point(double xVal, double yVal)
{
x = xVal;
y = yVal;
}
// Create a point from another point
public Point(Point point)
{
x = point.x;
y = point.y;
}
// Convert a point to a string
public String toString()
{
return x+","+y;
}
// Coordinates of the point
protected double x;
protected double y;
}
public class ListPoint
{
// Constructor
public ListPoint(Point point)
{
this.point = point; // Store point reference
next = null; // Set next ListPoint as null
}
// Set the pointer to the next ListPoint
public void setNext(ListPoint next)
{
this.next = next; // Store the next ListPoint
}
// Get the next point in the list
public ListPoint getNext()
{
return next; // Return the next ListPoint
}
// Return String representation
public String toString()
{
return "(" + point + ")";
}
private ListPoint next; // Refers to next ListPoint in the list
private Point point; // The point for this list point
}
public class PolyLine
{
// Construct a polyline from an array of points
public PolyLine(Point[] points)
{
if(points != null) // Make sure there is an array
{
// Create a one point list
start = new ListPoint(points[0]); // 1st point is the start
end = start; // as well as the end
// Now add the other points
for(int i = 1; i < points.length; i++)
addPoint(points[i]);
}
}
// Construct a polyline from an array of coordinates
public PolyLine(double[][] coords)
{
if(coords != null)
{
// Create a one point list
start = new ListPoint(new Point(coords[0][0], coords[0][1]));
// First is start
end = start; // as well as end
// Now add the other points
for(int i = 1; i < coords.length ; i++)
addPoint(coords[i][0], coords[i][1]);
}
}
// Add a Point object to the list
public void addPoint(Point point)
{
ListPoint newEnd = new ListPoint(point); // Create a new ListPoint
if(start == null)
start = newEnd; // Start is same as end
else
end.setNext(newEnd); // Set next variable for old end as new end
end = newEnd; // Store new point as end
}
// Add a point to the list
public void addPoint(double x, double y)
{
addPoint(new Point(x, y));
}
// String representation of a polyline
public String toString()
{
StringBuffer str = new StringBuffer("Polyline:");
ListPoint nextPoint = start; // Set the 1st point as start
while(nextPoint != null)
{
str.append(" "+ nextPoint); // Output the current point
nextPoint = nextPoint.getNext(); // Make the next point current
}
return str.toString();
}
private ListPoint start; // First ListPoint in the list
private ListPoint end; // Last ListPoint in the list
}
public class TryPolyLine
{
public static void main(String[] args)
{
// Create an array of coordinate pairs
double[][] coords = { {1., 1.}, {1., 2.}, { 2., 3.},
{-3., 5.}, {-5., 1.}, {0., 0.} };
// Create a polyline from the coordinates and display it
PolyLine polygon = new PolyLine(coords);
System.out.println(polygon);
// Add a point and display the polyline again
polygon.addPoint(10., 10.);
System.out.println(polygon);
// Create Point objects from the coordinate array
Point[] points = new Point[coords.length];
for(int i = 0; i < points.length; i++)
points[i] = new Point(coords[i][0],coords[i][1]);
// Use the points to create a new polyline and display it
PolyLine newPoly = new PolyLine(points);
System.out.println(newPoly);
}
}
the output is:
Polyline: (1.0,1.0) (1.0,2.0) (2.0,3.0) (-3.0,5.0) (-5.0,1.0) (0.0,0.0)
Polyline: (1.0,1.0) (1.0,2.0) (2.0,3.0) (-3.0,5.0) (-5.0,1.0) (0.0,0.0) (10.0,10.0)
Polyline: (1.0,1.0) (1.0,2.0) (2.0,3.0) (-3.0,5.0) (-5.0,1.0) (0.0,0.0)
I dont understand how the getNext method actually gets the next point.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|