summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/adrians-java/Polygon-java/rr_line.java
blob: ade3753b1b1bb68a7ee5911e77d6757d495bc050 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*

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/

=====================================================================

rr_line: 2D parametric line

First version 20 May 2005
This version: 2 October 2005 (translation to Java)

*/

// Teeny class to hold bisectors of line pairs

class rr_bisector
{
	public rr_line b;      // The bisecting line
	public double s_angle; // The sine of the angle 
	
	public rr_bisector(rr_line l, double a)
	{
		b = l;
		s_angle = a;
	}
}


// Exception for when trying to intersect parallel lines

class rr_ParallelLineException extends Exception
{
    public rr_ParallelLineException(String s)
    {
	super(s);
    }
}

// Class to hold and manipulate linear half-planes

class rr_half_plane
{

    // The half-plane is normal*(x, y) + offset < 0

    public rr_2point normal; 
    public double offset;


    // Convert a parametric line

    public rr_half_plane(rr_line l)
    {
	double rsq = 1/(l.direction.mul(l.direction));
	normal = new rr_2point(-l.direction.y*rsq, l.direction.x*rsq);
	rr_2point p = l.origin.add(l.direction);
	offset = rsq*l.origin.op(p);
    }


    // Make one from two points on its edge

    public rr_half_plane(rr_2point a, rr_2point b)
    {
	this(new rr_line(a, b));
    }   

    // Deep copy

    public rr_half_plane(rr_half_plane a)
    {
	normal = new rr_2point(a.normal);
	offset = a.offset;
    }


    // Convert to a string

    public String toString()
    {
	return "|" + normal.toString() + ", " + Double.toString(offset) + "|";
    } 


    // Change the sense

    public rr_half_plane complement()
    {
	rr_half_plane r = new rr_half_plane(this);
	r.normal.x = -r.normal.x;
	r.normal.y = -r.normal.y;
	r.offset = -r.offset;
	return r;
    }


    // Find the potential value of a point

    public double value(rr_2point p)
    {
	return offset + normal.mul(p);
    }


    // Find the potential interval of a box

    public rr_interval value(rr_box b)
    {
	return ((b.x.mul(normal.x)).add(b.y.mul(normal.y))).add(offset);
    }


    // The point where another line crosses

    public rr_2point cross_point(rr_half_plane a) throws rr_ParallelLineException
    {
	double det = normal.op(a.normal);
	if(det == 0)
	    throw new rr_ParallelLineException("cross_point: parallel lines.");
	det = 1/det;
	double x = normal.y*a.offset - a.normal.y*offset;
	double y = a.normal.x*offset - normal.x*a.offset;
	return new rr_2point(x*det, y*det);
    }

    public double cross_t(rr_line a) throws rr_ParallelLineException 
    {
	double det = a.direction.mul(normal);
        if (det == 0)
	    throw new rr_ParallelLineException("cross_t: parallel lines.");  
	return value(a.origin)/det;
    }

    public rr_2point cross_point(rr_line a) throws rr_ParallelLineException
    {
	return a.point(cross_t(a));
    }

}


// Class to hold and manipulate parametric lines

class rr_line
{
    public rr_2point direction;
    public rr_2point origin;

    public rr_line(rr_2point a, rr_2point b)
    {
	origin = new rr_2point(a);
	direction = b.sub(a);
    }

    public rr_line(rr_line r)
    {
	origin = new rr_2point(r.origin);
	direction = new rr_2point(r.direction);
    }

    // Convert to a string

    public String toString()
    {
	return "<" + origin.toString() + ", " + direction.toString() + ">";
    }


    // The point at a given parameter value

    public rr_2point point(double t)
    {
	return origin.add(direction.mul(t));
    }


    // Normalise the direction vector

    public void norm()
    {
	direction = direction.norm();
    }


    // Arithmetic

    public rr_line neg()
    {
	rr_line a = new rr_line(this);
	a.direction = direction.neg();
	return a;
    }

    public rr_line add(rr_2point b)
    {
	rr_2point a = origin.add(b);
	rr_line r = new rr_line(a, a.add(direction));
	return r;
    }

    public rr_line sub(rr_2point b)
    {
	rr_2point a = origin.sub(b);
	rr_line r = new rr_line(a, a.add(direction));
	return r;
    }

    /*
    public rr_line add(rr_line a)
    {
	a.origin = a.origin.add(origin);
	return a;
    }
    */

    //public rr_line sub(rr_line a, rr_line b)
    //{
    //	return add(a, b.neg());
    //}

    // The parameter value where another line crosses

    public double cross_t(rr_line a) throws rr_ParallelLineException 
    {
	double det = a.direction.op(direction);
        if (det == 0)
	    throw new rr_ParallelLineException("cross_t: parallel lines.");  
        rr_2point d = a.origin.sub(origin);
	return a.direction.op(d)/det;
    }


    // The point where another line crosses

    public rr_2point cross_point(rr_line a) throws rr_ParallelLineException
    {
	return point(cross_t(a));
    }


    // The squared distance of a point from a line

    public rr_2point d_2(rr_2point p)
    {
        double fsq = direction.x*direction.x;
        double gsq = direction.y*direction.y;
        double finv = 1.0/(fsq + gsq);
        rr_2point j0 = p.sub(origin);
        double fg = direction.x*direction.y;
        double dx = gsq*j0.x - fg*j0.y;
        double dy = fsq*j0.y - fg*j0.x;
        double d2 = (dx*dx + dy*dy)*finv*finv;
        double t = direction.mul(j0)*finv;
        return new rr_2point(d2, t);
    }

    // Normalised line that bisects the angle between 
    // two others joining points a, b, and c and the sine
    // of the angle it makes with them.
    
    static public rr_bisector bisect(rr_2point a, rr_2point b, rr_2point c)
    {
    	rr_2point ab = (a.sub(b)).norm();
    	rr_2point cb = (c.sub(b)).norm();
    	rr_2point d = (ab.add(cb)).norm();
    	double ang = ab.op(d);
    	rr_line r = new rr_line(b, b.add(d));
    	return new rr_bisector(r, ang);
    }
}