1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** \file
3  * LPE "Circle through 3 points" implementation
4  */
5 
6 /*
7  * Authors:
8  *   Maximilian Albert
9  *
10  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
11  * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>
12  *
13  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
14  */
15 
16 #include "live_effects/lpe-circle_3pts.h"
17 
18 // You might need to include other 2geom files. You can add them here:
19 #include <2geom/circle.h>
20 #include <2geom/path-sink.h>
21 // TODO due to internal breakage in glibmm headers, this must be last:
22 #include <glibmm/i18n.h>
23 
24 namespace Inkscape {
25 namespace LivePathEffect {
26 
LPECircle3Pts(LivePathEffectObject * lpeobject)27 LPECircle3Pts::LPECircle3Pts(LivePathEffectObject *lpeobject) :
28     Effect(lpeobject)
29 {
30 }
31 
32 LPECircle3Pts::~LPECircle3Pts()
33 = default;
34 
_circle3(Geom::Point const & A,Geom::Point const & B,Geom::Point const & C,Geom::PathVector & path_out)35 static void _circle3(Geom::Point const &A, Geom::Point const &B, Geom::Point const &C, Geom::PathVector &path_out) {
36     using namespace Geom;
37 
38     Point D = (A + B)/2;
39     Point E = (B + C)/2;
40 
41     Point v = (B - A).ccw();
42     Point w = (C - B).ccw();
43 
44     double det = -v[0] * w[1] + v[1] * w[0];
45 
46     Point M;
47     if (!v.isZero()) {
48         Point F = E - D;
49         double lambda = det == 0 ? 0 : (-w[1] * F[0] + w[0] * F[1]) / det;
50         M = D + v * lambda;
51     } else {
52         M = E;
53     }
54 
55     double radius = L2(M - A);
56 
57     Geom::Circle c(M, radius);
58     path_out = Geom::Path(c);
59 }
60 
61 Geom::PathVector
doEffect_path(Geom::PathVector const & path_in)62 LPECircle3Pts::doEffect_path (Geom::PathVector const & path_in)
63 {
64     Geom::PathVector path_out = Geom::PathVector();
65 
66     // we assume that the path has >= 3 nodes
67     Geom::Point A = path_in[0].initialPoint();
68     Geom::Point B = path_in[0].pointAt(1);
69     Geom::Point C = path_in[0].pointAt(2);
70 
71     _circle3(A, B, C, path_out);
72 
73     return path_out;
74 }
75 
76 /* ######################## */
77 
78 } //namespace LivePathEffect
79 } /* namespace Inkscape */
80 
81 /*
82   Local Variables:
83   mode:c++
84   c-file-style:"stroustrup"
85   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
86   indent-tabs-mode:nil
87   fill-column:99
88   End:
89 */
90 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
91