File: inversion_type.cc

package info (click to toggle)
kig 4%3A25.08.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,716 kB
  • sloc: cpp: 41,465; xml: 851; python: 486; perl: 23; sh: 17; makefile: 3
file content (655 lines) | stat: -rw-r--r-- 22,503 bytes parent folder | download
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// SPDX-FileCopyrightText: 2005 Maurizio Paolini <paolini@dmf.unicatt.it>

// SPDX-License-Identifier: GPL-2.0-or-later

#include "inversion_type.h"

#include <math.h>

#include "bogus_imp.h"
#include "circle_imp.h"
#include "line_imp.h"
#include "object_imp.h"
#include "other_imp.h"
#include "point_imp.h"
#include "special_imptypes.h"

#include "../misc/common.h"

static const KLazyLocalizedString str1 = kli18n("Invert with respect to this circle");
static const KLazyLocalizedString str2 = kli18n("Select the circle we want to invert against...");

static const ArgsParser::spec argsspecCircularInversion[] = {
    {&invertibleimptypeinstance, kli18n("Compute the inversion of this object"), kli18n("Select the object to invert..."), false},
    {CircleImp::stype(), str1, str2, false}};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(CircularInversionType)

CircularInversionType::CircularInversionType()
    : ArgsParserObjectType("CircularInversion", argsspecCircularInversion, 2)
{
}

CircularInversionType::~CircularInversionType()
{
}

const CircularInversionType *CircularInversionType::instance()
{
    static const CircularInversionType s;
    return &s;
}

const ObjectImpType *CircularInversionType::resultId() const
{
    return &invertibleimptypeinstance;
}

ObjectImp *CircularInversionType::calc(const Args &args, const KigDocument &) const
{
    if (args.size() == 2 && args[1]->inherits(LineImp::stype())) {
        /* we also accept the special case when the circle becomes a
         * straight line (this is not accepted during interactive construction,
         * but could happen dinamically when a construction can result either
         * with a circle or a line.
         * In this case we simply have a reflection
         */
        LineData d = static_cast<const AbstractLineImp *>(args[1])->data();
        Transformation t = Transformation::lineReflection(d);
        return args[0]->transform(t);
    }
    if (!margsparser.checkArgs(args))
        return new InvalidImp;

    const CircleImp *refcircle = static_cast<const CircleImp *>(args[1]);
    Coordinate refc = refcircle->center();
    double refrsq = refcircle->squareRadius();

    if (args[0]->inherits(PointImp::stype())) {
        Coordinate relp = static_cast<const PointImp *>(args[0])->coordinate() - refc;
        double normsq = relp.x * relp.x + relp.y * relp.y;
        if (normsq == 0)
            return new InvalidImp;
        return new PointImp(refc + (refrsq / normsq) * relp);
    }

    if (args[0]->inherits(AbstractLineImp::stype())) {
        bool isline = args[0]->inherits(LineImp::stype());
        bool issegment = args[0]->inherits(SegmentImp::stype());
        bool isray = args[0]->inherits(RayImp::stype());
        const LineData line = static_cast<const AbstractLineImp *>(args[0])->data();
        Coordinate rela = line.a - refc;
        Coordinate relb = line.b - refc;
        Coordinate ab = relb - rela;
        double t = (relb.x * ab.x + relb.y * ab.y) / (ab.x * ab.x + ab.y * ab.y);
        Coordinate relh = relb - t * ab;
        double normhsq = relh.x * relh.x + relh.y * relh.y;
        if (isline) {
            if (normhsq < 1e-12 * refrsq)
                return new LineImp(line.a, line.b);
            Coordinate newcenter = refc + 0.5 * refrsq / normhsq * relh;
            double newradius = 0.5 * refrsq / sqrt(normhsq);

            return new CircleImp(newcenter, newradius);
        }
        if (issegment || isray) {
            /*
             * now for the SegmentImp or RayImp
             * compute the inversion of the two endpoints
             */

            Coordinate newcenterrel = 0.5 * refrsq / normhsq * relh;
            Coordinate relainv = refrsq / rela.squareLength() * rela;
            Coordinate relbinv = Coordinate(0., 0.);
            if (issegment)
                relbinv = refrsq / relb.squareLength() * relb;

            if (normhsq < 1e-12 * refrsq) {
                if (rela.squareLength() < 1e-12) {
                    return new RayImp(relbinv + refc, 2 * relbinv + refc);
                }
                if (issegment && relb.squareLength() < 1e-12) {
                    return new RayImp(relainv + refc, 2 * relainv + refc);
                }
                if (relb.x * rela.x + relb.y * rela.y > 0) {
                    return new SegmentImp(relainv + refc, relbinv + refc);
                }
                return new InvalidImp();
            }
            double newradius = 0.5 * refrsq / sqrt(normhsq);

            relainv -= newcenterrel;
            relbinv -= newcenterrel;
            double angle1 = atan2(relainv.y, relainv.x);
            double angle2 = atan2(relbinv.y, relbinv.x);
            double angle = angle2 - angle1;
            if (ab.x * rela.y - ab.y * rela.x > 0) {
                angle1 = angle2;
                angle = -angle;
            }
            while (angle1 <= -M_PI)
                angle1 += 2 * M_PI;
            while (angle1 > M_PI)
                angle1 -= 2 * M_PI;
            while (angle < 0)
                angle += 2 * M_PI;
            while (angle >= 2 * M_PI)
                angle -= 2 * M_PI;
            return new ArcImp(newcenterrel + refc, newradius, angle1, angle);
        }
        return new InvalidImp;
    }

    if (args[0]->inherits(CircleImp::stype())) {
        const CircleImp *circle = static_cast<const CircleImp *>(args[0]);
        Coordinate c = circle->center() - refc;
        double clength = c.length();
        Coordinate cnorm = Coordinate(1., 0.);
        if (clength != 0.0)
            cnorm = c / clength;
        double r = circle->radius();
        Coordinate tc = r * cnorm;
        Coordinate b = c + tc; //(1 + t)*c;
        double bsq = b.x * b.x + b.y * b.y;
        Coordinate bprime = refrsq * b / bsq;
        if (std::fabs(clength - r) < 1e-6 * clength) // circle through origin -> line
        {
            return new LineImp(bprime + refc, bprime + refc + Coordinate(-c.y, c.x));
        }

        Coordinate a = c - tc;
        double asq = a.x * a.x + a.y * a.y;
        Coordinate aprime = refrsq * a / asq;

        Coordinate cprime = 0.5 * (aprime + bprime);
        double rprime = 0.5 * (bprime - aprime).length();

        return new CircleImp(cprime + refc, rprime);
    }

    if (args[0]->inherits(ArcImp::stype())) {
        const ArcImp *arc = static_cast<const ArcImp *>(args[0]);
        Coordinate c = arc->center() - refc;
        double clength = c.length();
        Coordinate cnorm = Coordinate(1., 0.);
        if (clength != 0.0)
            cnorm = c / clength;
        double r = arc->radius();
        /*
         * r > clength means center of inversion circle inside of circle supporting arc
         */
        Coordinate tc = r * cnorm;
        Coordinate b = c + tc;
        double bsq = b.x * b.x + b.y * b.y;
        Coordinate bprime = refrsq * b / bsq;
        if (std::fabs(clength - r) < 1e-6 * clength) // support circle through origin ->
                                                     // segment, ray or invalid
                                                     // (reversed segment, union of two rays)
        {
            bool valid1 = false;
            bool valid2 = false;
            Coordinate ep1 = arc->firstEndPoint() - refc;
            Coordinate ep2 = arc->secondEndPoint() - refc;
            Coordinate ep1inv = Coordinate::invalidCoord();
            Coordinate ep2inv = Coordinate::invalidCoord();
            double ep1sq = ep1.squareLength();
            if (ep1sq > 1e-12) {
                valid1 = true;
                ep1inv = refrsq / ep1sq * ep1;
            }
            Coordinate rayendp = ep1inv;
            int sign = 1;
            double ep2sq = ep2.squareLength();
            if (ep2sq > 1e-12) {
                valid2 = true;
                ep2inv = refrsq / ep2sq * ep2;
                rayendp = ep2inv;
                sign = -1;
            }
            if (valid1 || valid2) {
                if (valid1 && valid2) {
                    // this gives either a segment or the complement of a segment (relative
                    // to its support line).  We return a segment in any case (fixme)
                    double ang = atan2(-c.y, -c.x);
                    double sa = arc->startAngle();
                    if (ang < sa)
                        ang += 2 * M_PI;
                    if (ang - sa - arc->angle() < 0)
                        return new InvalidImp();
                    return new SegmentImp(ep1inv + refc, ep2inv + refc);
                } else
                    return new RayImp(rayendp + refc, rayendp + refc + sign * Coordinate(-c.y, c.x)); // this should give a Ray
            } else
                return new LineImp(bprime + refc, bprime + refc + Coordinate(-c.y, c.x));
        }

        Coordinate a = c - tc;
        double asq = a.x * a.x + a.y * a.y;
        Coordinate aprime = refrsq * a / asq;

        Coordinate cprime = 0.5 * (aprime + bprime);
        double rprime = 0.5 * (bprime - aprime).length();

        Coordinate ep1 = arc->firstEndPoint() - refc;
        double ang1 = arc->startAngle();
        double newstartangle = 2 * atan2(ep1.y, ep1.x) - ang1;
        Coordinate ep2 = arc->secondEndPoint() - refc;
        double ang2 = ang1 + arc->angle();
        double newendangle = 2 * atan2(ep2.y, ep2.x) - ang2;
        double newangle = newendangle - newstartangle;

        /*
         * newstartangle and newendangle might have to be exchanged:
         * this is the case if the circle supporting our arc does not
         * contain the center of the inversion circle
         */
        if (r < clength) {
            newstartangle = newendangle - M_PI;
            newangle = -newangle;
            // newendangle is no-longer valid
        }
        while (newstartangle <= -M_PI)
            newstartangle += 2 * M_PI;
        while (newstartangle > M_PI)
            newstartangle -= 2 * M_PI;
        while (newangle < 0)
            newangle += 2 * M_PI;
        while (newangle >= 2 * M_PI)
            newangle -= 2 * M_PI;
        return new ArcImp(cprime + refc, rprime, newstartangle, newangle);
    }

    return new InvalidImp;
}

/*
 * inversion of a point
 */

static const ArgsParser::spec argsspecInvertPoint[] = {
    {PointImp::stype(), kli18n("Compute the inversion of this point"), kli18n("Select the point to invert..."), false},
    {CircleImp::stype(), str1, str2, false}};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(InvertPointType)

InvertPointType::InvertPointType()
    : ArgsParserObjectType("InvertPoint", argsspecInvertPoint, 2)
{
}

InvertPointType::~InvertPointType()
{
}

const InvertPointType *InvertPointType::instance()
{
    static const InvertPointType s;
    return &s;
}

const ObjectImpType *InvertPointType::resultId() const
{
    return PointImp::stype();
}

ObjectImp *InvertPointType::calc(const Args &args, const KigDocument &) const
{
    if (args.size() == 2 && args[1]->inherits(LineImp::stype())) {
        /* we also accept the special case when the circle becomes a
         * straight line (see comment above)
         */
        LineData d = static_cast<const AbstractLineImp *>(args[1])->data();
        Transformation t = Transformation::lineReflection(d);
        return args[0]->transform(t);
    }
    if (!margsparser.checkArgs(args))
        return new InvalidImp;

    const CircleImp *c = static_cast<const CircleImp *>(args[1]);
    Coordinate center = c->center();
    Coordinate relp = static_cast<const PointImp *>(args[0])->coordinate() - center;
    double radiussq = c->squareRadius();
    double normsq = relp.x * relp.x + relp.y * relp.y;
    if (normsq == 0)
        return new InvalidImp;
    return new PointImp(center + (radiussq / normsq) * relp);
}

/*
 * old-style invertion types.  These can be safely removed, since trying
 * to load kig files that use these constructions are correctly converted
 * into the new CircularInversion.
 */

/*
 * inversion of a line
 */

static const ArgsParser::spec argsspecInvertLine[] = {
    {LineImp::stype(), kli18n("Compute the inversion of this line"), kli18n("Select the line to invert..."), false},
    {CircleImp::stype(), str1, str2, false}};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(InvertLineType)

InvertLineType::InvertLineType()
    : ArgsParserObjectType("InvertLineObsolete", argsspecInvertLine, 2)
{
}

InvertLineType::~InvertLineType()
{
}

const InvertLineType *InvertLineType::instance()
{
    static const InvertLineType s;
    return &s;
}

const ObjectImpType *InvertLineType::resultId() const
{
    return CircleImp::stype();
}

ObjectImp *InvertLineType::calc(const Args &args, const KigDocument &) const
{
    if (!margsparser.checkArgs(args))
        return new InvalidImp;

    const CircleImp *c = static_cast<const CircleImp *>(args[1]);
    Coordinate center = c->center();
    double radiussq = c->squareRadius();
    const LineData line = static_cast<const AbstractLineImp *>(args[0])->data();
    Coordinate relb = line.b - center;
    Coordinate ab = line.b - line.a;
    double t = (relb.x * ab.x + relb.y * ab.y) / (ab.x * ab.x + ab.y * ab.y);
    Coordinate relh = relb - t * ab;
    double normhsq = relh.x * relh.x + relh.y * relh.y;
    if (normhsq < 1e-12 * radiussq)
        return new LineImp(line.a, line.b);
    Coordinate newcenter = center + 0.5 * radiussq / normhsq * relh;
    double newradius = 0.5 * radiussq / sqrt(normhsq);

    return new CircleImp(newcenter, newradius);
}

/*
 * inversion of a segment
 */

static const ArgsParser::spec argsspecInvertSegment[] = {
    {SegmentImp::stype(), kli18n("Compute the inversion of this segment"), kli18n("Select the segment to invert..."), false},
    {CircleImp::stype(), str1, str2, false}};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(InvertSegmentType)

InvertSegmentType::InvertSegmentType()
    : ArgsParserObjectType("InvertSegmentObsolete", argsspecInvertSegment, 2)
{
}

InvertSegmentType::~InvertSegmentType()
{
}

const InvertSegmentType *InvertSegmentType::instance()
{
    static const InvertSegmentType s;
    return &s;
}

const ObjectImpType *InvertSegmentType::resultId() const
{
    return ArcImp::stype();
}

ObjectImp *InvertSegmentType::calc(const Args &args, const KigDocument &) const
{
    if (!margsparser.checkArgs(args))
        return new InvalidImp;

    const CircleImp *c = static_cast<const CircleImp *>(args[1]);
    Coordinate center = c->center();
    double radiussq = c->squareRadius();
    const LineData line = static_cast<const AbstractLineImp *>(args[0])->data();
    Coordinate rela = line.a - center;
    Coordinate relb = line.b - center;
    Coordinate ab = relb - rela;
    double t = (relb.x * ab.x + relb.y * ab.y) / (ab.x * ab.x + ab.y * ab.y);
    Coordinate relh = relb - t * ab;
    double normhsq = relh.x * relh.x + relh.y * relh.y;

    /*
     * compute the inversion of the two endpoints
     */

    Coordinate newcenterrel = 0.5 * radiussq / normhsq * relh;
    Coordinate relainv = radiussq / rela.squareLength() * rela;
    Coordinate relbinv = radiussq / relb.squareLength() * relb;

    if (normhsq < 1e-12 * radiussq) {
        if (rela.squareLength() < 1e-12) {
            return new RayImp(relbinv + center, 2 * relbinv + center);
        }
        if (relb.squareLength() < 1e-12) {
            return new RayImp(relainv + center, 2 * relainv + center);
        }
        if (relb.x * rela.x + relb.y * rela.y > 0) {
            return new SegmentImp(relainv + center, relbinv + center);
        }
        return new InvalidImp();
    }
    double newradius = 0.5 * radiussq / sqrt(normhsq);

    relainv -= newcenterrel;
    relbinv -= newcenterrel;
    double angle1 = atan2(relainv.y, relainv.x);
    double angle2 = atan2(relbinv.y, relbinv.x);
    double angle = angle2 - angle1;
    if (ab.x * rela.y - ab.y * rela.x > 0) {
        angle1 = angle2;
        angle = -angle;
    }
    while (angle1 < 0)
        angle1 += 2 * M_PI;
    while (angle1 >= 2 * M_PI)
        angle1 -= 2 * M_PI;
    while (angle < 0)
        angle += 2 * M_PI;
    while (angle >= 2 * M_PI)
        angle -= 2 * M_PI;
    return new ArcImp(newcenterrel + center, newradius, angle1, angle);
}

/*
 * inversion of a circle
 */

static const ArgsParser::spec argsspecInvertCircle[] = {
    {CircleImp::stype(), kli18n("Compute the inversion of this circle"), kli18n("Select the circle to invert..."), false},
    {CircleImp::stype(), str1, str2, false}};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(InvertCircleType)

InvertCircleType::InvertCircleType()
    : ArgsParserObjectType("InvertCircleObsolete", argsspecInvertCircle, 2)
{
}

InvertCircleType::~InvertCircleType()
{
}

const InvertCircleType *InvertCircleType::instance()
{
    static const InvertCircleType s;
    return &s;
}

const ObjectImpType *InvertCircleType::resultId() const
{
    return CircleImp::stype();
}

ObjectImp *InvertCircleType::calc(const Args &args, const KigDocument &) const
{
    if (!margsparser.checkArgs(args))
        return new InvalidImp;

    const CircleImp *refcircle = static_cast<const CircleImp *>(args[1]);
    Coordinate refc = refcircle->center();
    double refrsq = refcircle->squareRadius();
    const CircleImp *circle = static_cast<const CircleImp *>(args[0]);
    Coordinate c = circle->center() - refc;
    double clength = c.length();
    Coordinate cnorm = Coordinate(1., 0.);
    if (clength != 0.0)
        cnorm = c / clength;
    double r = circle->radius();
    Coordinate tc = r * cnorm;
    Coordinate b = c + tc; //(1 + t)*c;
    double bsq = b.x * b.x + b.y * b.y;
    Coordinate bprime = refrsq * b / bsq;
    if (std::fabs(clength - r) < 1e-6 * clength) // circle through origin -> line
    {
        return new LineImp(bprime + refc, bprime + refc + Coordinate(-c.y, c.x));
    }

    Coordinate a = c - tc;
    double asq = a.x * a.x + a.y * a.y;
    Coordinate aprime = refrsq * a / asq;

    Coordinate cprime = 0.5 * (aprime + bprime);
    double rprime = 0.5 * (bprime - aprime).length();

    return new CircleImp(cprime + refc, rprime);
}

/*
 * inversion of an arc
 */

static const ArgsParser::spec argsspecInvertArc[] = {
    {ArcImp::stype(), kli18n("Compute the inversion of this arc"), kli18n("Select the arc to invert..."), false},
    {CircleImp::stype(), str1, str2, false}};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(InvertArcType)

InvertArcType::InvertArcType()
    : ArgsParserObjectType("InvertArcObsolete", argsspecInvertArc, 2)
{
}

InvertArcType::~InvertArcType()
{
}

const InvertArcType *InvertArcType::instance()
{
    static const InvertArcType s;
    return &s;
}

const ObjectImpType *InvertArcType::resultId() const
{
    return ArcImp::stype();
}

ObjectImp *InvertArcType::calc(const Args &args, const KigDocument &) const
{
    if (!margsparser.checkArgs(args))
        return new InvalidImp;

    const CircleImp *refcircle = static_cast<const CircleImp *>(args[1]);
    Coordinate refc = refcircle->center();
    double refrsq = refcircle->squareRadius();
    const ArcImp *arc = static_cast<const ArcImp *>(args[0]);
    Coordinate c = arc->center() - refc;
    double clength = c.length();
    Coordinate cnorm = Coordinate(1., 0.);
    if (clength != 0.0)
        cnorm = c / clength;
    double r = arc->radius();
    /*
     * r > clength means center of inversion circle inside of circle supporting arc
     */
    Coordinate tc = r * cnorm;
    Coordinate b = c + tc;
    double bsq = b.x * b.x + b.y * b.y;
    Coordinate bprime = refrsq * b / bsq;
    if (std::fabs(clength - r) < 1e-6 * clength) // support circle through origin ->
                                                 // segment, ray or invalid
                                                 // (reversed segment, union of two rays)
    {
        bool valid1 = false;
        bool valid2 = false;
        Coordinate ep1 = arc->firstEndPoint() - refc;
        Coordinate ep2 = arc->secondEndPoint() - refc;
        Coordinate ep1inv = Coordinate::invalidCoord();
        Coordinate ep2inv = Coordinate::invalidCoord();
        double ep1sq = ep1.squareLength();
        if (ep1sq > 1e-12) {
            valid1 = true;
            ep1inv = refrsq / ep1sq * ep1;
        }
        Coordinate rayendp = ep1inv;
        int sign = 1;
        double ep2sq = ep2.squareLength();
        if (ep2sq > 1e-12) {
            valid2 = true;
            ep2inv = refrsq / ep2sq * ep2;
            rayendp = ep2inv;
            sign = -1;
        }
        if (valid1 || valid2) {
            if (valid1 && valid2) {
                // this gives either a segment or the complement of a segment (relative
                // to its support line).  We return a segment in any case (fixme)
                double ang = atan2(-c.y, -c.x);
                double sa = arc->startAngle();
                if (ang < sa)
                    ang += 2 * M_PI;
                if (ang - sa - arc->angle() < 0)
                    return new InvalidImp();
                return new SegmentImp(ep1inv + refc, ep2inv + refc);
            } else
                return new RayImp(rayendp + refc, rayendp + refc + sign * Coordinate(-c.y, c.x)); // this should give a Ray
        } else
            return new LineImp(bprime + refc, bprime + refc + Coordinate(-c.y, c.x));
    }

    Coordinate a = c - tc;
    double asq = a.x * a.x + a.y * a.y;
    Coordinate aprime = refrsq * a / asq;

    Coordinate cprime = 0.5 * (aprime + bprime);
    double rprime = 0.5 * (bprime - aprime).length();

    Coordinate ep1 = arc->firstEndPoint() - refc;
    double ang1 = arc->startAngle();
    double newstartangle = 2 * atan2(ep1.y, ep1.x) - ang1;
    Coordinate ep2 = arc->secondEndPoint() - refc;
    double ang2 = ang1 + arc->angle();
    double newendangle = 2 * atan2(ep2.y, ep2.x) - ang2;
    double newangle = newendangle - newstartangle;

    /*
     * newstartangle and newendangle might have to be exchanged:
     * this is the case if the circle supporting our arc does not
     * contain the center of the inversion circle
     */
    if (r < clength) {
        newstartangle = newendangle - M_PI;
        newangle = -newangle;
        // newendangle is no-longer valid
    }
    while (newstartangle < 0)
        newstartangle += 2 * M_PI;
    while (newstartangle >= 2 * M_PI)
        newstartangle -= 2 * M_PI;
    while (newangle < 0)
        newangle += 2 * M_PI;
    while (newangle >= 2 * M_PI)
        newangle -= 2 * M_PI;
    return new ArcImp(cprime + refc, rprime, newstartangle, newangle);
}