1 /*
2 * Circle Curve
3 *
4 * Authors:
5 * Marco Cecchetti <mrcekets at gmail.com>
6 *
7 * Copyright 2008 authors
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it either under the terms of the GNU Lesser General Public
11 * License version 2.1 as published by the Free Software Foundation
12 * (the "LGPL") or, at your option, under the terms of the Mozilla
13 * Public License Version 1.1 (the "MPL"). If you do not alter this
14 * notice, a recipient may use your version of this file under either
15 * the MPL or the LGPL.
16 *
17 * You should have received a copy of the LGPL along with this library
18 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * You should have received a copy of the MPL along with this library
21 * in the file COPYING-MPL-1.1
22 *
23 * The contents of this file are subject to the Mozilla Public License
24 * Version 1.1 (the "License"); you may not use this file except in
25 * compliance with the License. You may obtain a copy of the License at
26 * http://www.mozilla.org/MPL/
27 *
28 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
29 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
30 * the specific language governing rights and limitations.
31 */
34 #include <2geom/circle.h>
35 #include <2geom/ellipse.h>
36 #include <2geom/svg-elliptical-arc.h>
37 #include <2geom/numeric/fitting-tool.h>
38 #include <2geom/numeric/fitting-model.h>
42 namespace Geom
43 {
45 void Circle::set(double A, double B, double C, double D)
46 {
47 if (A == 0)
48 {
49 THROW_RANGEERROR("square term coefficient == 0");
50 }
52 //std::cerr << "B = " << B << " C = " << C << " D = " << D << std::endl;
54 double b = B / A;
55 double c = C / A;
56 double d = D / A;
58 m_centre[X] = -b/2;
59 m_centre[Y] = -c/2;
60 double r2 = m_centre[X] * m_centre[X] + m_centre[Y] * m_centre[Y] - d;
62 if (r2 < 0)
63 {
64 THROW_RANGEERROR("ray^2 < 0");
65 }
67 m_ray = std::sqrt(r2);
68 }
71 void Circle::set(std::vector<Point> const& points)
72 {
73 size_t sz = points.size();
74 if (sz < 3)
75 {
76 THROW_RANGEERROR("fitting error: too few points passed");
77 }
78 NL::LFMCircle model;
79 NL::least_squeares_fitter<NL::LFMCircle> fitter(model, sz);
81 for (size_t i = 0; i < sz; ++i)
82 {
83 fitter.append(points[i]);
84 }
85 fitter.update();
87 NL::Vector z(sz, 0.0);
88 model.instance(*this, fitter.result(z));
89 }
92 SVGEllipticalArc
93 Circle::arc(Point const& initial, Point const& inner, Point const& final,
94 bool _svg_compliant)
95 {
96 Ellipse e(center(X), center(Y), ray(), ray(), 0);
97 return e.arc(initial, inner, final, _svg_compliant);
98 }
101 } // end namespace Geom
106 /*
107 Local Variables:
108 mode:c++
109 c-file-style:"stroustrup"
110 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
111 indent-tabs-mode:nil
112 fill-column:99
113 End:
114 */
115 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :