001/* 002 * Copyright 2006 - 2013 003 * Stefan Balev <stefan.balev@graphstream-project.org> 004 * Julien Baudry <julien.baudry@graphstream-project.org> 005 * Antoine Dutot <antoine.dutot@graphstream-project.org> 006 * Yoann Pigné <yoann.pigne@graphstream-project.org> 007 * Guilhelm Savin <guilhelm.savin@graphstream-project.org> 008 * 009 * This file is part of GraphStream <http://graphstream-project.org>. 010 * 011 * GraphStream is a library whose purpose is to handle static or dynamic 012 * graph, create them from scratch, file or any source and display them. 013 * 014 * This program is free software distributed under the terms of two licenses, the 015 * CeCILL-C license that fits European law, and the GNU Lesser General Public 016 * License. You can use, modify and/ or redistribute the software under the terms 017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following 018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by 019 * the Free Software Foundation, either version 3 of the License, or (at your 020 * option) any later version. 021 * 022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 024 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 025 * 026 * You should have received a copy of the GNU Lesser General Public License 027 * along with this program. If not, see <http://www.gnu.org/licenses/>. 028 * 029 * The fact that you are presently reading this means that you have had 030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. 031 */ 032package org.graphstream.ui.geom; 033 034/** 035 * 2D point. 036 * 037 * A Point2 is a 2D location in an affine space described by three values along 038 * the X, and Y axes. This differs from the Vector3 and Vector4 classes in that 039 * it is only 2D and has no vector arithmetic bound to it (to points cannot be 040 * added, this would have no mathematical meaning). 041 * 042 * @author Antoine Dutot 043 * @since 20001121 creation 044 * @version 0.1 045 */ 046public class Point2 implements java.io.Serializable { 047 // Attributes 048 049 private static final long serialVersionUID = 965985679540486895L; 050 051 /** 052 * X axis value. 053 */ 054 public double x; 055 056 /** 057 * Y axis value. 058 */ 059 public double y; 060 061 // Attributes -- Shared 062 063 /** 064 * Specific point at (0,0). 065 */ 066 public static final Point2 NULL_POINT2 = new Point2(0, 0); 067 068 // Constructors 069 070 /** 071 * New 2D point at (0,0). 072 */ 073 public Point2() { 074 } 075 076 /** 077 * New 2D point at (x,y). 078 */ 079 public Point2(double x, double y) { 080 set(x, y); 081 } 082 083 /** 084 * New copy of other. 085 */ 086 public Point2(Point2 other) { 087 copy(other); 088 } 089 090 /** 091 * New 2D point at (x,y). 092 */ 093 public void make(double x, double y) { 094 set(x, y); 095 } 096 097 // Accessors 098 099 /** 100 * Are all components to zero?. 101 */ 102 public boolean isZero() { 103 return (x == 0 && y == 0); 104 } 105 106 // /** 107 // * Is other equal to this ? 108 // */ 109 // public boolean 110 // equals( const Point2 < double > & other ) const 111 // { 112 // return( x == other.x 113 // and y == other.y 114 // and z == other.z ); 115 // } 116 117 /** 118 * Create a new point linear interpolation of this and <code>other</code>. 119 * The new point is located between this and <code>other</code> if 120 * <code>factor</code> is between 0 and 1 (0 yields this point, 1 yields the 121 * <code>other</code> point). 122 */ 123 public Point2 interpolate(Point2 other, double factor) { 124 Point2 p = new Point2(x + ((other.x - x) * factor), y 125 + ((other.y - y) * factor)); 126 127 return p; 128 } 129 130 /** 131 * Distance between this and <code>other</code>. 132 */ 133 public double distance(Point2 other) { 134 double xx = other.x - x; 135 double yy = other.y - y; 136 return Math.abs(Math.sqrt((xx * xx) + (yy * yy))); 137 } 138 139 // Commands 140 141 /** 142 * Make this a copy of other. 143 */ 144 public void copy(Point2 other) { 145 x = other.x; 146 y = other.y; 147 } 148 149 /** 150 * Like #moveTo(). 151 */ 152 public void set(double x, double y) { 153 this.x = x; 154 this.y = y; 155 } 156 157 // Commands -- moving 158 159 /** 160 * Move to absolute position (x,y). 161 */ 162 public void moveTo(double x, double y) { 163 this.x = x; 164 this.y = y; 165 } 166 167 /** 168 * Move of given vector (dx,dy). 169 */ 170 public void move(double dx, double dy) { 171 this.x += dx; 172 this.y += dy; 173 } 174 175 /** 176 * Move of given point <code>p</code>. 177 */ 178 public void move(Point2 p) { 179 this.x += p.x; 180 this.y += p.y; 181 } 182 183 /** 184 * Move horizontally of dx. 185 */ 186 public void moveX(double dx) { 187 x += dx; 188 } 189 190 /** 191 * Move vertically of dy. 192 */ 193 public void moveY(double dy) { 194 y += dy; 195 } 196 197 /** 198 * Scale of factor (sx,sy). 199 */ 200 public void scale(double sx, double sy) { 201 x *= sx; 202 y *= sy; 203 } 204 205 /** 206 * Scale by factor s. 207 */ 208 public void scale(Point2 s) { 209 x *= s.x; 210 y *= s.y; 211 } 212 213 /** 214 * Change only abscissa at absolute coordinate x. 215 */ 216 public void setX(double x) { 217 this.x = x; 218 } 219 220 /** 221 * Change only ordinate at absolute coordinate y. 222 */ 223 public void setY(double y) { 224 this.y = y; 225 } 226 227 /** 228 * Exchange the values of this and other. 229 */ 230 public void swap(Point2 other) { 231 double t; 232 233 if (other != this) { 234 t = this.x; 235 this.x = other.x; 236 other.x = t; 237 238 t = this.y; 239 this.y = other.y; 240 other.y = t; 241 } 242 } 243 244 // Commands -- misc. 245 246 @Override 247 public String toString() { 248 StringBuffer buf; 249 250 buf = new StringBuffer("Point2["); 251 252 buf.append(x); 253 buf.append('|'); 254 buf.append(y); 255 buf.append("]"); 256 257 return buf.toString(); 258 } 259}