日期:2014-05-20 浏览次数:20726 次
[code=Java][code=Java]public class Point { public Point(double xVal, double yVal){ x = xVal; y = yVal; } public Point(Point Point){ x = Point.x; y = Point.y; } public String toString(){ return x + "," + y; } protected double x; protected double y; }
public class ListPoint { public ListPoint(Point Point){ this.Point = Point; next = null; } public void setNext(ListPoint next){ this.next = next; } public ListPoint getNext(){ return next; } public String toString(){ return"(" + Point + ")"; } private ListPoint next; private Point Point; }
public class PolyLine { public PolyLine(Point[] points){ if(points != null){ for(Point p : points){ addPoint(p); } } } public PolyLine(double[][] coords){ if(coords != null){ for(int i = 0; i<coords.length; i++){ addPoint(coords[i][0], coords[i][1]); } } } public void addPoint(Point Point){ ListPoint newEnd = new ListPoint(Point); if(start == null){ [color=#FF0000]//问题标签1~~ [/color] start = newEnd; }else{ end.setNext(newEnd); } end = newEnd; } public void addPoint(double x, double y){ addPoint(new Point(x , y)); } public String toString(){ StringBuffer str = new StringBuffer("Polyline:"); ListPoint nextPoint = start; while(nextPoint != null){ [color=#FF0000]//问题标签2~~~[/color] str.append(" " + nextPoint); nextPoint = nextPoint.getNext(); } return str.toString(); } private ListPoint start; private ListPoint end; }
public class TryPolyLine { public static void main(String[] args){ double[][] coords = {{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 Polygon = new PolyLine(coords); System.out.println(Polygon); Polygon.addPoint(10.0 , 10.0); System.out.println(Polygon); Point[] points = new Point[coords.length]; for(int i=0; i<points.length; i++){ points[i] = new Point(coords[i][0] , coords[i][1]); } PolyLine newPoly = new PolyLine(points); System.out.println(newPoly); } }