Source code: jpicedt/graphic/PicPoint.java
1 /* jPicEdt version 1.3.2, a picture editor for LaTeX.
2 Copyright (C) 1999-2002 Sylvain Reynal
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 Sylvain Reynal
19 Département de Physique
20 Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
21 6, avenue du Ponceau
22 95014 CERGY CEDEX
23 FRANCE
24
25 Tel : 00 +33 130 736 245
26 Fax : 00 +33 130 736 667
27 e-mail : reynal@ensea.fr
28 jPicEdt web page : http://www.jpicedt.org
29 */
30
31 package jpicedt.graphic;
32
33 //import jpicedt.graphic.PEToolKit;
34
35 import java.awt.*;
36 import java.awt.geom.*;
37
38 /**
39 * Enhancement of Point2D.Double
40 * @author Sylvain Reynal
41 * @since PicEdt 1.0
42 */
43
44 public class PicPoint extends Point2D.Double {
45
46 /**
47 * Construct a (0,0) point.
48 * @author Sylvain Reynal
49 * @since jPicEdt 1.3.2
50 */
51 public PicPoint() {
52 super();
53 }
54
55 /**
56 * Clone the given point.
57 * @author Sylvain Reynal
58 * @since jPicEdt 1.3.2
59 */
60 public PicPoint(PicPoint picPoint){
61 super(picPoint.x, picPoint.y);
62 }
63
64 /**
65 * Clone the given point.
66 * @author Sylvain Reynal
67 * @since jPicEdt 1.3.2
68 */
69 public PicPoint(Point pt){
70 super(pt.x, pt.y);
71 }
72
73 /**
74 * Construct (x,y)
75 * @author Sylvain Reynal
76 * @since jPicEdt 1.3.2
77 */
78 public PicPoint(double x, double y){
79 super(x,y);
80 }
81
82 /**
83 * Construct a point from the given pair of Number (using their double value).
84 * @author Sylvain Reynal
85 * @since jPicEdt 1.3.2
86 */
87 public PicPoint(Number x, Number y){
88 super(x.doubleValue(), y.doubleValue());
89 }
90
91 /**
92 * Construct a point from the first two elements of the given array.
93 * @author Sylvain Reynal
94 * @since jPicEdt 1.3.2
95 */
96 public PicPoint(float[] f){
97 super(f[0], f[1]);
98 }
99
100 /**
101 * Construct a point from the first two elements of the given array.
102 * @author Sylvain Reynal
103 * @since jPicEdt 1.3.2
104 */
105 public PicPoint(double[] f){
106 super(f[0], f[1]);
107 }
108
109 /**
110 * @return a two-element array filled with x and y ; if f is null, a new array is allocated ; otherwise,
111 * the given array is directly modified and returned for convenience.
112 * @author Sylvain Reynal
113 * @since jPicEdt 1.3.2
114 */
115 public float[] toFloatArray(float[] f){
116 if (f==null) f = new float[2];
117 f[0] = (float)x;
118 f[1] = (float)y;
119 return f;
120 }
121
122 /**
123 * @return a two-element array filled with x and y ; if f is null, a new array is allocated ; otherwise,
124 * the given array is directly modified and returned for convenience.
125 * @author Sylvain Reynal
126 * @since jPicEdt 1.3.2
127 */
128 public double[] toDoubleArray(double[] f){
129 if (f==null) f = new double[2];
130 f[0] = x;
131 f[1] = y;
132 return f;
133 }
134
135 /**
136 * @return a "(x,y)" string representing this point. This use <code>PEToolKit.doubleToString</code>
137 * for number formating.
138 * @since PicEdt 1.1
139 */
140 public String toString(){
141 StringBuffer buf = new StringBuffer(15);
142 buf.append("(");
143 buf.append(PEToolKit.doubleToString(x));
144 buf.append(",");
145 buf.append(PEToolKit.doubleToString(y));
146 buf.append(")");
147 return buf.toString();
148 }
149
150 /**
151 * convert a PicPoint with coordinate in the given unitlenth (expressed in mm)
152 * to a new PicPoint in mm coordinate.
153 *
154 * @param unitLenght In mm
155 *
156 * @since PicEdt 1.0
157 */
158 public PicPoint toMm(double unitLength){
159 return new PicPoint(x * unitLength, y * unitLength);
160 }
161
162 /**
163 * convert a PicPoint with coordinate in the given unitlenths along X and Y axis (expressed in mm)
164 * to a new PicPoint in mm coordinate.
165 * @param xUnit In mm
166 * @param yUnit In mm
167 * @since jPicEdt 1.3.2
168 */
169 public PicPoint toMm(double xUnit, double yUnit){
170 return new PicPoint(xUnit * x, yUnit * y);
171 }
172
173
174 /**
175 * translates this point by (dx,dy)
176 * @since PicEdt 1.0
177 */
178 public void translate(double dx, double dy){
179 x += dx;
180 y += dy;
181 }
182
183 } // PicPoint