001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.graph.graph;
005    
006    import graphlab.platform.lang.FromStringProvider;
007    import graphlab.platform.StaticUtils;
008    
009    import java.awt.geom.Point2D;
010    import java.io.Serializable;
011    import java.util.Scanner;
012    
013    public class GraphPoint extends Point2D.Double implements Serializable, FromStringProvider {
014        static {
015            StaticUtils.setFromStringProvider(GraphPoint.class.getName(), new GraphPoint());
016        }
017    
018        private static final long serialVersionUID = -1000000001L;
019    
020        public GraphPoint() {
021            super();
022        }
023    
024        public GraphPoint(GraphPoint p) {
025            this.x = p.x;
026            this.y = p.y;
027        }
028    
029        public GraphPoint(double x, double y) {
030            this.x = x;
031            this.y = y;
032        }
033    
034        /**
035         * multiplies x and y by p (x=x*p, y=y*p)
036         *
037         * @param p
038         */
039        public void multiply(double p) {
040            x = x * p;
041            y = y * p;
042        }
043    
044        /**
045         * adds this with dp (x=x+dp.x, y=y+dp.y)
046         *
047         * @param p
048         */
049        public void add(GraphPoint dp) {
050            x = x + dp.x;
051            y = y + dp.y;
052        }
053    
054        /**
055         * adds this with dp (x=x+dx.x, y=y+dy)
056         *
057         * @return this
058         */
059        public GraphPoint add(double dx, double dy) {
060            x = x + dx;
061            y = y + dy;
062            return this;
063        }
064    
065    
066        public String toString() {
067            return x + " , " + y;
068        }
069    
070        public double distance(GraphPoint pt) {
071            double PX = pt.getX() - this.getX();
072            double PY = pt.getY() - this.getY();
073            return Math.sqrt(PX * PX + PY * PY);
074        }
075    
076        public GraphPoint fromString(String data) {
077            data = data.replace(',', ' ');
078            Scanner _ = new Scanner(data);
079            return new GraphPoint(_.nextDouble(), _.nextDouble());
080        }
081    }