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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
|
/*
* $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/bin/utility.c,v 1.2 2009-05-25 18:11:03 m_fischer Exp $
*/
/* XTrkCad - Model Railroad CAD
* Copyright (C) 2005 Dave Bullis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#ifndef WINDOWS
#include <unistd.h>
#endif
#include <math.h>
#include "common.h"
#include "utility.h"
/*****************************************************************************
*
* VARIABLES
*
*/
double radiusGranularity = 1.0/8.0;
DEBUGF_T debugIntersection = 0;
#define CLOSE (1.0)
/*****************************************************************************
*
* UTLITY FUNCTIONS
*
*/
#ifndef min
double max( double a, double b )
{
if (a>b) return a;
return b;
}
double min( double a, double b )
{
if (a<b) return a;
return b;
}
#endif
double FindDistance( coOrd p0, coOrd p1 )
{
double dx = p1.x-p0.x, dy = p1.y-p0.y;
return sqrt( dx*dx + dy*dy );
}
double NormalizeAngle( double a )
{
while (a<0.0) a += 360.0;
while (a>=360.0) a -= 360.0;
if ( a > 360.0-EPSILON ) a = 0.0;
return a;
}
int IsAligned( double a1, double a2 )
{
a1 = NormalizeAngle( a1 - a2 + 90.0 );
return ( a1 < 180 );
}
double D2R( double D )
{
D = NormalizeAngle(D);
if (D >= 180.0) D = D - 360.0;
return D * (M_PI*2) / 360.0;
}
double R2D( double R )
{
return NormalizeAngle( R * 360.0 / (M_PI*2) );
}
void Rotate( coOrd *p, coOrd orig, double angle )
{
double x=p->x,y=p->y;
x -= orig.x;
y -= orig.y;
p->x = (POS_T)(x * cos(D2R(angle)) + y * sin(D2R(angle)));
p->y = (POS_T)(y * cos(D2R(angle)) - x * sin(D2R(angle)));
p->x += orig.x;
p->y += orig.y;
}
/**
* Translate coordinates.
*
* \param res OUT new (translated) position
* \param orig IN old position
* \param a IN angle
* \param d IN distance
*/
void Translate( coOrd *res, coOrd orig, double a, double d )
{
res->x = orig.x + (POS_T)(d * sin( D2R(a)) );
res->y = orig.y + (POS_T)(d * cos( D2R(a)) );
}
double FindAngle( coOrd p0, coOrd p1 )
{
double dx = p1.x-p0.x, dy = p1.y-p0.y;
if (small(dx)) {
if (dy >=0) return 0.0;
else return 180.0;
}
if (small(dy)) {
if (dx >=0) return 90.0;
else return 270.0;
}
return R2D(atan2( dx,dy ));
}
BOOL_T PointOnCircle( coOrd * resP, coOrd center, double radius, double angle )
{
double r;
r = sin(D2R(angle));
r = radius * r;
resP->x = center.x + (POS_T)(radius * sin(D2R(angle)));
resP->y = center.y + (POS_T)(radius * cos(D2R(angle)));
return 1;
}
double ConstrainR( double r )
{
double ret;
ret = r / radiusGranularity;
ret = floor( ret + 0.5 );
ret = ret * radiusGranularity;
return ret;
}
void FindPos( coOrd * res, double * beyond, coOrd pos, coOrd orig, double angle, double length )
{
double a0, a1;
double d;
#ifdef __linux
static volatile double x;
#else
double x;
#endif
a0 = FindAngle( orig, pos );
a1 = NormalizeAngle( a0 - angle );
d = FindDistance( orig, pos );
x = d * cos( D2R( a1 ) );
if ( x < 0.0 ) {
res->x = (POS_T)0.0;
} else if (x > length) {
res->x = (POS_T)length;
} else {
res->x = (POS_T)x;
}
if (beyond) *beyond = x - res->x;
res->y = (POS_T)(d * sin( D2R( a1 )) );
}
/* Find intersection:
Given 2 lines each described by a point and angle (P0,A0) (P1,A1)
there exists a common point PC.
d0x = sin(A0)
d0y = cos(A0)
d1x = sin(A1)
d1y = cos(A1)
Pc.x = P0.x + N0 * d0x
Pc.y = P0.y + N0 * d0y
Pc.x = P1.x + N1 * d1x
Pc.y = P1.y + N1 * d1y
Combining:
(1) Pc.x = P0.x + N0 * d0x = P1.x + N1 * d1y
(2) Pc.y = P0.y + N0 * d0y = P1.y + N1 * d1y
Solve Pc.y for N0:
P0.y + N0 * d0y = P1.y + N1 * d1y
N0 * d0y = P1.y + N1 * d1y - P0.y
N0 = (P1.y + N1 * d1y - P0.y) / d0y
(3) N0 = (P1.y - P0.y + N1 * d1y) / d0y
Solve Pc.x for N1:
P0.x + N0 * d0x = P1.x + N1 * d1x
P0.x + N0 * d0x - P1.x = N1 * d1x
(P0.x + N0 * d0x - P1.x) / d1x = N1
(4) (P0.x - P1.x + N0 * d0x) / d1x = N1
Substitute (3) into (4):
(P0.x - P1.x + [(P1.y - P0.y + N1 * d1y) / d0y ] * d0x)
N1 = -----------------------------------------------------------
d1x
Regroup:
(P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x [ N1 * d1y / d0y ] * d0x
N1 = -------------------------------------------- + ------------------------
d1x d1x
(P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x N1 * (d1y * d0x / d0y)
N1 = -------------------------------------------- + ------------------------
d1x d1x
(P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x (d1y * d0x / d0y)
N1 = -------------------------------------------- + N1 * --------------------
d1x d1x
(d1y * d0x / d0y) (P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x
N1 * ( 1 - ----------------- ) = --------------------------------------------
d1x d1x
(P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x
--------------------------------------------
d1x
N1 = ============================================
d1y * d0x / d0y
1 - ---------------
d1x
(P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x
--------------------------------------------
d1x
N1 = ============================================
d1x - d1y * d0x / d0y
---------------------
d1x
d1x cancel
(P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x
N1 = ============================================
d1x - d1y * d0x / d0y
(P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x
N1 = ============================================
d1x*d0y - d1y*d0x
-------------------
d0y
Bring up d0y:
{ ((P0.x - P1.x + [(P1.y - P0.y)/d0y] * d0x } * d0y
N1 = =======================================================
d1x*d0y - d1y*d0x
Distribute and cancel:
(P0.x - P1.x) * d0y + (P1.y - P0.y) * d0x
N1 = =============================================
d1x*d0y - d1y*d0x
if (d1x*d0y - d1y*d0x) = 0 then lines are parallel
*/
BOOL_T FindIntersection( coOrd *Pc, coOrd P0, double A0, coOrd P1, double A1 )
{
double dx0, dy0, dx1, dy1, N1;
double d;
#ifndef WINDOWS
if (debugIntersection >= 3)
printf("FindIntersection( [%0.3f %0.3f] A%0.3f [%0.3f %0.3f] A%0.3f\n",
P0.x, P0.y, A0, P1.x, P1.y, A1 );
#endif
dx0 = sin( D2R( A0 ) );
dy0 = cos( D2R( A0 ) );
dx1 = sin( D2R( A1 ) );
dy1 = cos( D2R( A1 ) );
d = dx1 * dy0 - dx0 * dy1;
if (d < EPSILON && d > -EPSILON) {
#ifndef WINDOWS
if (debugIntersection >=3 ) printf("dx1 * dy0 - dx0 * dy1 = %0.3f\n", d );
#endif
return FALSE;
}
/*
* (P0.x - P1.x) * d0y + (P1.y - P0.y) * d0x
* N1 = =============================================
* d1x*d0y - d1y*d0x
*/
N1 = dy0 * (P0.x - P1.x) + dx0 * (P1.y - P0.y );
N1 = N1 / d;
Pc->x = P1.x + (POS_T)(N1*dx1);
Pc->y = P1.y + (POS_T)(N1*dy1);
#ifndef WINDOWS
if (debugIntersection >=3 ) printf( " [%0.3f,%0.3f]\n", Pc->x, Pc->y );
#endif
return TRUE;
}
EPINX_T PickArcEndPt( coOrd pc, coOrd p0, coOrd p1 )
{
double a;
a = NormalizeAngle( FindAngle( pc, p1 ) - FindAngle( pc, p0 ) );
if (a > 180.0)
return 0;
else
return 1;
}
EPINX_T PickLineEndPt( coOrd p0, double a0, coOrd p1 )
{
double a;
a = NormalizeAngle( FindAngle( p0, p1 ) - a0 );
if (a < 90.0 || a > 270 )
return 0;
else
return 1;
}
double LineDistance( coOrd *p, coOrd p0, coOrd p1 )
{
double d, a;
coOrd pp, zero;
zero.x = zero.y = (POS_T)0.0;
d = FindDistance( p0, p1 );
a = FindAngle( p0, p1 );
pp.x = p->x-p0.x;
pp.y = p->y-p0.y;
Rotate( &pp, zero, -a );
if (pp.y < 0.0-EPSILON) {
d = FindDistance( p0, *p );
*p = p0;
return d;
} else if (pp.y > d+EPSILON ) {
d = FindDistance( p1, *p );
*p = p1;
return d;
} else {
p->x = p0.x + (POS_T)(pp.y*sin(D2R(a)));
p->y = p0.y + (POS_T)(pp.y*cos(D2R(a)));
return pp.x>=0? pp.x : -pp.x;
}
}
double CircleDistance( coOrd *p, coOrd c, double r, double a0, double a1 )
{
double d;
double a,aa;
coOrd pEnd;
d = FindDistance( c, *p );
a = FindAngle( c, *p );
aa = NormalizeAngle( a - a0 );
if (a1 >= 360.0 || aa <= a1) {
d = fabs(d-r);
PointOnCircle( p, c, r, a );
} else {
if ( aa < a1+(360.0-a1)/2.0 ) {
PointOnCircle( &pEnd, c, r, a0+a1 );
} else {
PointOnCircle( &pEnd, c, r, a0 );
}
d = FindDistance( *p, pEnd );
*p = pEnd;
}
return d;
}
coOrd AddCoOrd( coOrd p0, coOrd p1, double a )
{
coOrd res, zero;
zero.x = zero.y = (POS_T)0.0;
Rotate(&p1, zero, a );
res.x = p0.x + p1.x;
res.y = p0.y + p1.y;
return res;
}
BOOL_T InRect( coOrd pos, coOrd rect )
{
if (pos.x >= 0.0 && pos.x <= rect.x && pos.y >= 0.0 && pos.y <= rect.y)
return 1;
else
return 0;
}
static BOOL_T IntersectLine( POS_T *fx0, POS_T *fy0, POS_T x1, POS_T y1, POS_T x, POS_T y )
{
POS_T x0=*fx0, y0=*fy0, dx, dy;
BOOL_T rc;
#ifdef TEST
printf(" IntersectLine( P0=[%0.2f %0.2f] P1=[%0.2f %0.2f] X=%0.2f Y=%0.2f\n",
x0, y0, x1, y1, x, y );
#endif
dx = x1-x0;
dy = y1-y0;
if (dy==0.0) {
if (y0 == y)
rc = TRUE;
else
rc = FALSE;
} else {
x0 += (y-y0) * dx/dy;
if (x0 < -EPSILON || x0 > x) {
rc = FALSE;
} else {
*fx0 = x0;
*fy0 = y;
rc = TRUE;
}
}
#ifdef TEST
if (rc)
printf(" = TRUE [%0.2f %0.2f]\n", *fx0, *fy0 );
else
printf(" = FALSE\n");
#endif
return rc;
}
/*
* intersectBox - find point on box boundary ([0,0],[size]) where
* line from p0 (interior) to p1 (exterior) intersects
*/
static void IntersectBox( coOrd *p1, coOrd p0, coOrd size, int x1, int y1 )
{
#ifdef TEST
printf(" IntersectBox( P1=[%0.2f %0.2f] P0=[%0.2f %0.2f] S=[%0.2f %0.2f] X1=%d Y1=%d\n",
p1->x, p1->y, p0.x, p0.y, size.x, size.y, x1, y1 );
#endif
if ( y1!=0 &&
IntersectLine( &p1->x, &p1->y, p0.x, p0.y, size.x, (y1==-1?(POS_T)0.0:size.y) ))
return;
else if ( x1!=0 &&
IntersectLine( &p1->y, &p1->x, p0.y, p0.x, size.y, (x1==-1?(POS_T)0.0:size.x) ))
return;
#ifndef WINDOWS
else
fprintf(stderr, "intersectBox bogus\n" );
#endif
}
BOOL_T ClipLine( coOrd *fp0, coOrd *fp1, coOrd orig, double angle, coOrd size )
{
coOrd p0 = *fp0, p1 = * fp1;
int x0, y0, x1, y1;
#ifdef TEST
printf("ClipLine( P0=[%0.2f %0.2f] P1=[%0.2f %0.2f] O=[%0.2f %0.2f] A=%0.2f S=[%0.2f %0.2f]\n",
p0.x, p0.y, p1.x, p1.y, orig.x, orig.y, angle, size.x, size.y );
#endif
Rotate( &p0, orig, -angle );
Rotate( &p1, orig, -angle );
p0.x -= orig.x; p0.y -= orig.y;
p1.x -= orig.x; p1.y -= orig.y;
/* categorize point as to sector:
-1,1 | 0,1 | 1,1
------------+-------------S----------
-1,0 | 0,0 | 1,0
------------O-------------+----------
-1,-1 | 0,-1 + 1,-1
*/
if ( p0.x < 0.0-EPSILON ) x0 = -1;
else if ( p0.x > size.x+EPSILON ) x0 = 1;
else x0 = 0;
if ( p0.y < 0.0-EPSILON ) y0 = -1;
else if ( p0.y > size.y+EPSILON ) y0 = 1;
else y0 = 0;
if ( p1.x < 0.0-EPSILON ) x1 = -1;
else if ( p1.x > size.x+EPSILON ) x1 = 1;
else x1 = 0;
if ( p1.y < 0.0-EPSILON ) y1 = -1;
else if ( p1.y > size.y+EPSILON ) y1 = 1;
else y1 = 0;
#ifdef TEST
printf(" X0=%d Y0=%d X1=%d Y1=%d\n", x0, y0, x1, y1 );
#endif
/* simple cases: one or both points within box */
if ( x0==0 && y0==0 ) {
if ( x1==0 && y1==0 ) {
/* both within box */
return 1;
}
/* p0 within, p1 without */
IntersectBox( &p1, p0, size, x1, y1 );
p1.x += orig.x; p1.y += orig.y;
Rotate( &p1, orig, angle );
*fp1 = p1;
return 1;
}
if ( x1==0 && y1==0 ) {
/* p1 within, p0 without */
IntersectBox( &p0, p1, size, x0, y0 );
p0.x += orig.x; p0.y += orig.y;
Rotate( &p0, orig, angle );
*fp0 = p0;
return 1;
}
/* both points without box and cannot intersect */
if ( (x0==x1 && y0==y1) || /* within same sector (but not the middle one) */
(x0!=0 && x0==x1) || /* both right or left */
(y0!=0 && y0==y1) ) /* both above or below */
return 0;
#ifdef TEST
printf(" complex intersection\n");
#endif
/* possible intersection */
if ( y0!=0 &&
IntersectLine( &p0.x, &p0.y, p1.x, p1.y, size.x, (y0==-1?(POS_T)0.0:size.y) ))
IntersectBox( &p1, p0, size, x1, y1 );
else if ( y1!=0 &&
IntersectLine( &p1.x, &p1.y, p0.x, p0.y, size.x, (y1==-1?(POS_T)0.0:size.y) ))
IntersectBox( &p0, p1, size, x0, y0 );
else if ( x0!=0 &&
IntersectLine( &p0.y, &p0.x, p1.y, p1.x, size.y, (x0==-1?(POS_T)0.0:size.x) ))
IntersectBox( &p1, p0, size, x1, y1 );
else if ( x1!=0 &&
IntersectLine( &p1.y, &p1.x, p0.y, p0.x, size.y, (x1==-1?(POS_T)0.0:size.x) ))
IntersectBox( &p0, p1, size, x0, y0 );
else {
return 0;
}
p0.x += orig.x; p0.y += orig.y;
p1.x += orig.x; p1.y += orig.y;
Rotate( &p0, orig, angle );
Rotate( &p1, orig, angle );
*fp0 = p0;
*fp1 = p1;
return 1;
}
#ifdef LATER
BOOL_T ClipArc( double a0, double a1, coOrd pos, double radius, coOrd orig, double angle, double size )
{
i = -1;
state = unknown;
if (pos.y + radius < 0.0 ||
pos.y - radius > size.y ||
pos.x + radius < 0.0 ||
pos.x - radius > size.x )
return 0;
if (pos.y + radius <= size.y ||
pos.y - radius >= 0.0 ||
pos.x + radius <= size.x ||
pos.x - radius >= 0.0 )
return 1;
if (pos.y + radius > size.y) {
a = R2D(acos( (size.y-pos.y) / radius ) ));
if (pos.x + radius*cos(R2D(a)) > size.x) {
state = outside;
} else {
state = inside;
i++;
aa[i].a0 = a;
}
} else {
state = inside;
i++;
aa[i].a0 = 0;
}
}
#endif
#ifdef TEST
void Test( double p0x, double p0y, double p1x, double p1y, double origx, double origy, double angle, double sizex, double sizey )
{
coOrd p0, p1, orig, size, p0a, p1a;
BOOL_T rc;
p0.x = p0x; p0.y = p0y; p1.x = p1x; p1.y = p1y;
orig.x = origx; orig.y = origy;
size.x = sizex; size.y = sizey;
p0a = p0; p1a = p1;
rc = ClipLine( &p0, &p1, orig, angle, size );
printf("clipLine=%d P0=[%0.3f %0.3f] P1=[%0.3f %0.3f]\n", rc,
p0.x, p0.y, p1.x, p1.y );
}
INT_T Main( INT_T argc, char *argv[] )
{
double a[9];
int i;
if (argc != 10) {
printf("usage: a x0 y0 x1 y1 xo yo a xs ys\n");
Exit(1);
}
argv++;
for (i=0;i<9;i++)
a[i] = atof( *argv++ );
Test( a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8] );
}
#endif
|