001
002 package algo;
003
004 import java.io.Serializable;
005 import java.util.Random;
006
007 /**
008 * A Point in a 2d Graph.
009 * Defined by an x- and y-coordinate and given a number.
010 * @author Heinz Kredel.
011 */
012 public class Point implements Serializable {
013
014 public final int n;
015 public final double x;
016 public final double y;
017
018 /**
019 * @param n the number of the point
020 * @param x coordinate.
021 * @param y coordinate.
022 */
023 public Point(int n, double x, double y) {
024 this.n = n;
025 this.x = x;
026 this.y = y;
027 }
028
029
030 /* (non-Javadoc)
031 * @see java.lang.Object#toString()
032 */
033 public String toString() {
034 return "["+n+"]("+x+","+y+")";
035 }
036
037
038 /**
039 * @param n the number of the point
040 * @param r a random number generator.
041 * @param m maximal coordinate range.
042 * @return point with random x- and y-coordinates less than m.
043 */
044 public static Point random(int n, Random r, double m) {
045 return new Point(n,
046 Math.abs(r.nextDouble()*m),
047 Math.abs(r.nextDouble()*m));
048 }
049
050 /**
051 * @param p a Point to be scaled.
052 * @param scale a scale factor.
053 * @return a point with x- and y-coordinates multiplied by scale.
054 */
055 public static Point scaleTo(Point p, double scale) {
056 return new Point(p.n, p.x*scale, p.y*scale);
057 }
058
059 }