summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/geometry/polygons/Rr2Point.java
blob: acbd57dc118d56a0e683c6832a0a2112f5c8581a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 
 RepRap
 ------
 
 The Replicating Rapid Prototyper Project
 
 
 Copyright (C) 2005
 Adrian Bowyer & The University of Bath
 
 http://reprap.org
 
 Principal author:
 
 Adrian Bowyer
 Department of Mechanical Engineering
 Faculty of Engineering and Design
 University of Bath
 Bath BA2 7AY
 U.K.
 
 e-mail: A.Bowyer@bath.ac.uk
 
 RepRap is free; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 Licence as published by the Free Software Foundation; either
 version 2 of the Licence, or (at your option) any later version.
 
 RepRap is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public Licence for more details.
 
 For this purpose the words "software" and "library" in the GNU Library
 General Public Licence are taken to mean any and all computer programs
 computer files data results documents and other copyright information
 available from the RepRap project.
 
 You should have received a copy of the GNU Library General Public
 Licence along with RepRap; if not, write to the Free
 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA,
 or see
 
 http://www.gnu.org/
 
 =====================================================================
 
 
 Rr2Point: 2D vectors
 
 First version 20 May 2005
 This version: 1 May 2006 (Now in CVS - no more comments here)
 
 
 */

package org.reprap.geometry.polygons;

/**
 * Class for (x, y) points and vectors
 */
public class Rr2Point
{
	/**
	 * 
	 */
	private double x, y;
	
	/**
	 * Default to the origin
	 */
	public Rr2Point()
	{
		x = 0;
		y = 0;
	}
	
	/**
	 * Usual constructor
	 * @param a
	 * @param b
	 */
	public Rr2Point(double a, double b)
	{
		x = a;
		y = b;
	}
	
	/**
	 * Copy
	 * @param r Rr2Point to copy from
	 */
	public Rr2Point(Rr2Point r)
	{
		x = r.x;
		y = r.y;
	}
	
	/**
	 * Overwrite
	 * @param p Rr2Point containing values to take
	 */
	public void set(Rr2Point p)
	{
		x = p.x;
		y = p.y;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return Double.toString(x) + " " + Double.toString(y);
	}
	
	/**
	 * Coordinates
	 */
	public double x() { return x; }
	public double y() { return y; }
	
	/**
	 * Arithmetic
	 * @return neg of point
	 */
	public Rr2Point neg()
	{
		return new Rr2Point(-x, -y);
	}
	
	/**
	 * @return orthogonal of (this) point
	 */
	public Rr2Point orthogonal()
	{
		return new Rr2Point(y, -x);
	}
	
	/**
	 * @param a
	 * @param b
	 * @return a new point based on a vector addition of points a and b
	 */
	public static Rr2Point add(Rr2Point a, Rr2Point b)
	{
		Rr2Point r = new Rr2Point(a);
		r.x += b.x;
		r.y += b.y;
		return r;
	}
	
	/**
	 * @param a
	 * @param b
	 * @return a new point based on a vector subtraction of a - b
	 */
	public static Rr2Point sub(Rr2Point a, Rr2Point b)
	{
		return add(a, b.neg());
	}
	
	
	/**
	 * Scale a point
	 * @param b An R2rPoint
	 * @param factor A scale factor
	 * @return The point Rr2Point scaled by a factor of factor
	 */
	public static Rr2Point mul(Rr2Point b, double factor)
	{
		return new Rr2Point(b.x*factor, b.y*factor);
	}
	
	/**
	 * @param a
	 * @param b
	 * @return the point Rr2Point scaled by a factor of a
	 */
	public static Rr2Point mul(double a, Rr2Point b)
	{
		return mul(b, a);
	}
	
	/**
	 * Downscale a point
	 * @param b An R2rPoint
	 * @param factor A scale factor
	 * @return The point Rr2Point divided by a factor of a
	 */
	public static Rr2Point div(Rr2Point b, double factor)
	{
		return mul(b, 1/factor);
	}
	
	/**
	 * Inner product
	 * @param a
	 * @param b
	 * @return The point Rr2Point scaled by a factor of point b (?)
	 */
	public static double mul(Rr2Point a, Rr2Point b)
	{
		return a.x*b.x + a.y*b.y;
	}
	
	
	/**
	 * Modulus
	 * @return modulus
	 */
	public double mod()
	{
		return Math.sqrt(mul(this, this));
	}
	
	
	/**
	 * Unit length normalization
	 * @return normalized unit lenght 
	 */
	public Rr2Point norm()
	{
		return div(this, mod());
	}
	
	
	/**
	 * Outer product
	 * @param a
	 * @param b
	 * @return oute product
	 */
	public static double op(Rr2Point a, Rr2Point b)
	{
		return a.x*b.y - a.y*b.x;
	}
	
	/**
	 * Gradient
	 * @return gradient
	 */
	public double gradient()
	{
		double g;
		if(x == 0)
		{
			if(y > 0)
				g = Double.POSITIVE_INFINITY;
			else
				g = Double.NEGATIVE_INFINITY;
		} else
			g = y/x;
		return g;
	}
	
	/**
	 * Squared distance
	 * @param a
	 * @param b
	 * @return squared distance
	 */
	public static double d_2(Rr2Point a, Rr2Point b)
	{
		Rr2Point c = sub(a, b);
		return mul(c, c);
	}
	
	/**
	 * The same, withing tolerance?
	 * @param a
	 * @param b
	 * @param tol_2
	 * @return true if the squared distance between points a and b 
	 * is within tolerance tol_2, otherwise false
	 */
	public static boolean same(Rr2Point a, Rr2Point b, double tol_2)
	{
		return d_2(a, b) < tol_2;
	}
}