Line.java
Geographic line object class
Size 4.4 kB - File type text/plainFile contents
// Name: Line.java
// Intent: geographic line object class
// Programmer: John P. Kavanagh #2748 2405
// Date Modified: 9 May 2002
// Audit: Clifford Meyers (cmeyers@buffalo.edu)
public class Line {
// myFirstSegment will only have a next segment,
// no previous segment
private LineSegment myFirstSegment;
// The line constructor
public Line() {
myFirstSegment = null;
//System.out.println("Instantiated null line.");
}
public Line(LineSegment firstSegment) {
myFirstSegment = firstSegment;
System.out.println("Instantiated line (" + GetBeginPoint().getXvalue() + "," + GetBeginPoint().getYvalue() + "),(" + GetEndPoint().getXvalue() + "," + GetEndPoint().getYvalue() + ")");
}
// Retrieve the beginning point of the entire line
public Point GetBeginPoint(){
//System.out.println("Get line begin point: (" + myFirstSegment.GetBeginPoint().getXvalue() + "," + myFirstSegment.GetBeginPoint().getYvalue() + ")");
return myFirstSegment.GetBeginPoint();
}
// Retrieve the beginning line segment of the entire line
public LineSegment GetBeginSegment(){
return myFirstSegment;
}
// Find next point
// Check each line segment to find the next point
public Point GetNextPoint(Point thisPoint){
LineSegment thisSegment = myFirstSegment;
// As long as the given point is not the first point
// in the given line segment, continue to the next segment
while ((thisSegment.IsBeginPoint(thisPoint) == false) && !(GetEndPoint().IsEqual(thisPoint))){
thisSegment = thisSegment.GetNextSegment();
}
// Now that you've found the right segment,
// return this segment's end point
// this is the "Next" point
if (GetEndPoint().IsEqual(thisPoint)) {
//System.out.println("Get line next point: NONE. End of line.");
return null;
}
else {
//System.out.println("Get line next point: (" + thisSegment.GetEndPoint().getXvalue() + "," + thisSegment.GetEndPoint().getYvalue() + ")");
return thisSegment.GetEndPoint();
}
}
// Retrieve the end point of the entire line
public Point GetEndPoint(){
if (myFirstSegment.GetNextSegment() == null){
//System.out.println("Get line end point: (" + myFirstSegment.GetEndPoint().getXvalue() + "," + myFirstSegment.GetEndPoint().getYvalue() + ")");
return myFirstSegment.GetEndPoint();
}
else {
//System.out.println("Get line end point: (" + FindLastSegment(myFirstSegment).GetEndPoint().getXvalue() + "," + FindLastSegment(myFirstSegment).GetEndPoint().getYvalue() + ")");
return FindLastSegment(myFirstSegment).GetEndPoint();
}
}
// Add segment to line object
public void AddLineSegment(LineSegment addMe){
//System.out.println("Adding line segment: (" + addMe.GetBeginPoint().getXvalue() + "," + addMe.GetBeginPoint().getYvalue() + ")");
// Find the last segment in the line object, and then add this new segment
if (myFirstSegment == null) {
myFirstSegment = addMe; // if empty line, make it the first segment
}
else {
FindLastSegment(myFirstSegment).AddNextSegment(addMe);
}
}
// Scale entire line object
public void Scale(String factor_value){
RecurseScale(myFirstSegment, factor_value);
}
// Recurse across entire line and scale each segment individually
public void RecurseScale(LineSegment lineSeg, String factor_value){
if (lineSeg.GetNextSegment() != null){
RecurseScale(lineSeg.GetNextSegment(), factor_value);
lineSeg.Scale(factor_value);
}
else {
lineSeg.Scale(factor_value);
}
}
// Destroy entire line
public void Destroy(){
if (myFirstSegment.GetNextSegment() != null){
RecurseDestroy(myFirstSegment.GetNextSegment());
}
myFirstSegment.Destroy();
}
// Recurse across line segments and destroy each segment
// from end to beginning
public void RecurseDestroy(LineSegment lineSeg){
if (lineSeg.GetNextSegment() != null){
RecurseDestroy(lineSeg.GetNextSegment());
}
lineSeg.Destroy();
}
// If line segment is adjacent to another continue until end of all
// segments is reached and return final segment
public LineSegment FindLastSegment(LineSegment lineSeg){
if (lineSeg.GetNextSegment() == null){
return lineSeg;
}
else {
return FindLastSegment(lineSeg.GetNextSegment());
}
}
}
Click here to get the file