Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
ellipse.h
Go to the documentation of this file.
1/*
4 * Authors:
5 * Marco Cecchetti <mrcekets at gmail.com>
6 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
7 *
8 * Copyright 2008 authors
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it either under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation
13 * (the "LGPL") or, at your option, under the terms of the Mozilla
14 * Public License Version 1.1 (the "MPL"). If you do not alter this
15 * notice, a recipient may use your version of this file under either
16 * the MPL or the LGPL.
17 *
18 * You should have received a copy of the LGPL along with this library
19 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * You should have received a copy of the MPL along with this library
22 * in the file COPYING-MPL-1.1
23 *
24 * The contents of this file are subject to the Mozilla Public License
25 * Version 1.1 (the "License"); you may not use this file except in
26 * compliance with the License. You may obtain a copy of the License at
27 * http://www.mozilla.org/MPL/
28 *
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31 * the specific language governing rights and limitations.
32 */
33
34
35#ifndef LIB2GEOM_SEEN_ELLIPSE_H
36#define LIB2GEOM_SEEN_ELLIPSE_H
37
38#include <vector>
39#include <2geom/angle.h>
40#include <2geom/bezier-curve.h>
41#include <2geom/exception.h>
42#include <2geom/forward.h>
43#include <2geom/line.h>
44#include <2geom/transforms.h>
45
46namespace Geom {
47
48class EllipticalArc;
49class Circle;
50
61 : boost::multipliable< Ellipse, Translate
62 , boost::multipliable< Ellipse, Scale
63 , boost::multipliable< Ellipse, Rotate
64 , boost::multipliable< Ellipse, Zoom
65 , boost::multipliable< Ellipse, Affine
66 , boost::equality_comparable< Ellipse
67 > > > > > >
68{
72public:
74 Ellipse(Point const &c, Point const &r, Coord angle)
75 : _center(c)
76 , _rays(r)
77 , _angle(angle)
78 {}
79 Ellipse(Coord cx, Coord cy, Coord rx, Coord ry, Coord angle)
80 : _center(cx, cy)
81 , _rays(rx, ry)
82 , _angle(angle)
83 {}
84 Ellipse(double A, double B, double C, double D, double E, double F) {
85 setCoefficients(A, B, C, D, E, F);
86 }
88 Ellipse(Geom::Circle const &c);
89
91 void set(Point const &c, Point const &r, Coord angle) {
92 _center = c;
93 _rays = r;
94 _angle = angle;
95 }
97 void set(Coord cx, Coord cy, Coord rx, Coord ry, Coord a) {
98 _center[X] = cx;
99 _center[Y] = cy;
100 _rays[X] = rx;
101 _rays[Y] = ry;
102 _angle = a;
103 }
105 void setCoefficients(double A, double B, double C, double D, double E, double F);
107 void setCenter(Point const &p) { _center = p; }
109 void setCenter(Coord cx, Coord cy) { _center[X] = cx; _center[Y] = cy; }
111 void setRays(Point const &p) { _rays = p; }
113 void setRays(Coord x, Coord y) { _rays[X] = x; _rays[Y] = y; }
115 void setRay(Coord r, Dim2 d) { _rays[d] = r; }
118
119 Point center() const { return _center; }
120 Coord center(Dim2 d) const { return _center[d]; }
122 Point rays() const { return _rays; }
124 Coord ray(Dim2 d) const { return _rays[d]; }
126 Angle rotationAngle() const { return _angle; }
128 Point initialPoint() const;
130 Point finalPoint() const { return initialPoint(); }
131
134 void fit(std::vector<Point> const& points);
135
144 EllipticalArc *arc(Point const &ip, Point const &inner, Point const &fp);
145
149 Ellipse canonicalForm() const;
150 void makeCanonical();
151
162
163 LineSegment majorAxis() const { return ray(X) >= ray(Y) ? axis(X) : axis(Y); }
164 LineSegment minorAxis() const { return ray(X) < ray(Y) ? axis(X) : axis(Y); }
166 return ray(X) >= ray(Y) ? semiaxis(X, sign) : semiaxis(Y, sign);
167 }
169 return ray(X) < ray(Y) ? semiaxis(X, sign) : semiaxis(Y, sign);
170 }
171 LineSegment axis(Dim2 d) const;
172 LineSegment semiaxis(Dim2 d, int sign = 1) const;
173
175 Rect boundsExact() const;
176
182 Rect boundsFast() const;
183
185 std::vector<double> coefficients() const;
186 void coefficients(Coord &A, Coord &B, Coord &C, Coord &D, Coord &E, Coord &F) const;
187
191 Point pointAt(Coord t) const;
193 Coord valueAt(Coord t, Dim2 d) const;
194
199 Coord timeAt(Point const &p) const;
200
202 Point unitTangentAt(Coord t) const;
203
205 bool contains(Point const &p) const;
206
208 std::vector<ShapeIntersection> intersect(Line const &line) const;
210 std::vector<ShapeIntersection> intersect(LineSegment const &seg) const;
212 std::vector<ShapeIntersection> intersect(Ellipse const &other) const;
214 std::vector<ShapeIntersection> intersect(D2<Bezier> const &other) const;
215
217 _center *= t;
218 return *this;
219 }
221 _center *= s;
222 _rays *= s;
223 return *this;
224 }
226 _center *= z;
227 _rays *= z.scale();
228 return *this;
229 }
230 Ellipse &operator*=(Rotate const &r);
231 Ellipse &operator*=(Affine const &m);
232
234 bool operator==(Ellipse const &other) const;
235};
236
241bool are_near(Ellipse const &a, Ellipse const &b, Coord precision = EPSILON);
242
245std::ostream &operator<<(std::ostream &out, Ellipse const &e);
246
247} // end namespace Geom
248
249#endif // LIB2GEOM_SEEN_ELLIPSE_H
250
251/*
252 Local Variables:
253 mode:c++
254 c-file-style:"stroustrup"
255 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
256 indent-tabs-mode:nil
257 fill-column:99
258 End:
259*/
260// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Defines the different types of exceptions that 2geom can throw.
Various trigoniometric helper functions.
Bezier curve.
3x3 matrix representing an affine transformation.
Definition affine.h:70
Wrapper for angular values.
Definition angle.h:73
Set of all points at a fixed distance from the center.
Definition circle.h:55
Adaptor that creates 2D functions from 1D ones.
Definition d2.h:55
Set of points with a constant sum of distances from two foci.
Definition ellipse.h:68
Coord valueAt(Coord t, Dim2 d) const
Evaluate a single coordinate of a point on the ellipse.
Definition ellipse.cpp:365
void makeCanonical()
Definition ellipse.cpp:342
void setRay(Coord r, Dim2 d)
Set one of the rays of the ellipse.
Definition ellipse.h:115
Ellipse(Point const &c, Point const &r, Coord angle)
Definition ellipse.h:74
LineSegment semiaxis(Dim2 d, int sign=1) const
Definition ellipse.cpp:137
void setRays(Point const &p)
Set both rays of the ellipse.
Definition ellipse.h:111
LineSegment axis(Dim2 d) const
Definition ellipse.cpp:127
Point initialPoint() const
Get the point corresponding to the +X ray of the ellipse.
Definition ellipse.cpp:101
void setRotationAngle(Angle a)
Set the angle the X ray makes with the +X axis.
Definition ellipse.h:117
void setRays(Coord x, Coord y)
Set both rays of the ellipse as coordinates.
Definition ellipse.h:113
Point unitTangentAt(Coord t) const
Get the value of the derivative at time t normalized to unit length.
Definition ellipse.cpp:398
std::vector< double > coefficients() const
Get the coefficients of the ellipse's implicit equation.
Definition ellipse.cpp:173
Ellipse & operator*=(Scale const &s)
Definition ellipse.h:220
bool contains(Point const &p) const
Check whether the ellipse contains the given point.
Definition ellipse.cpp:406
Ellipse(Coord cx, Coord cy, Coord rx, Coord ry, Coord angle)
Definition ellipse.h:79
Ellipse canonicalForm() const
Return an ellipse with less degrees of freedom.
Definition ellipse.cpp:335
Rect boundsFast() const
Get a fast to compute bounding box which contains the ellipse.
Definition ellipse.cpp:162
Ellipse & operator*=(Zoom const &z)
Definition ellipse.h:225
Coord timeAt(Point const &p) const
Find the time value of a point on an ellipse.
Definition ellipse.cpp:382
LineSegment semiminorAxis(int sign=1) const
Definition ellipse.h:168
Angle _angle
Definition ellipse.h:71
Angle rotationAngle() const
Get the angle the X ray makes with the +X axis.
Definition ellipse.h:126
std::vector< ShapeIntersection > intersect(Line const &line) const
Compute intersections with an infinite line.
Definition ellipse.cpp:460
void setCenter(Point const &p)
Set the center.
Definition ellipse.h:107
Affine unitCircleTransform() const
Compute the transform that maps the unit circle to this ellipse.
Definition ellipse.cpp:110
void set(Coord cx, Coord cy, Coord rx, Coord ry, Coord a)
Set center, rays and angle as constituent values.
Definition ellipse.h:97
LineSegment minorAxis() const
Definition ellipse.h:164
void set(Point const &c, Point const &r, Coord angle)
Set center, rays and angle.
Definition ellipse.h:91
EllipticalArc * arc(Point const &ip, Point const &inner, Point const &fp)
Create an elliptical arc from a section of the ellipse.
Definition ellipse.cpp:226
Affine inverseUnitCircleTransform() const
Compute the transform that maps this ellipse to the unit circle.
Definition ellipse.cpp:117
LineSegment semimajorAxis(int sign=1) const
Definition ellipse.h:165
Point pointAt(Coord t) const
Evaluate a point on the ellipse.
Definition ellipse.cpp:358
Ellipse & operator*=(Translate const &t)
Definition ellipse.h:216
Coord ray(Dim2 d) const
Get one ray of the ellipse.
Definition ellipse.h:124
Rect boundsExact() const
Get the tight-fitting bounding box of the ellipse.
Definition ellipse.cpp:146
void setCenter(Coord cx, Coord cy)
Set the center by coordinates.
Definition ellipse.h:109
Ellipse(double A, double B, double C, double D, double E, double F)
Definition ellipse.h:84
Point rays() const
Get both rays as a point.
Definition ellipse.h:122
Point _rays
Definition ellipse.h:70
Point center() const
Definition ellipse.h:119
Point _center
Definition ellipse.h:69
Coord center(Dim2 d) const
Definition ellipse.h:120
LineSegment majorAxis() const
Definition ellipse.h:163
Point finalPoint() const
Get the point corresponding to the +X ray of the ellipse.
Definition ellipse.h:130
void setCoefficients(double A, double B, double C, double D, double E, double F)
Set an ellipse by solving its implicit equation.
Definition ellipse.cpp:48
Elliptical arc curve.
Infinite line on a plane.
Definition line.h:53
Two-dimensional point that doubles as a vector.
Definition point.h:66
Axis aligned, non-empty rectangle.
Definition rect.h:92
Rotation around the origin.
Definition transforms.h:187
Scaling from the origin.
Definition transforms.h:150
Translation by a vector.
Definition transforms.h:115
Combination of a translation and uniform scale.
Definition transforms.h:292
Coord scale() const
Definition transforms.h:316
double inner(valarray< double > const &x, valarray< double > const &y)
double c[8][4]
Contains forward declarations of 2geom types.
Dim2
2D axis enumeration (X or Y).
Definition coord.h:48
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
constexpr Coord EPSILON
Default "acceptably small" value.
Definition coord.h:84
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Infinite straight line.
Various utility functions.
Definition affine.h:22
static float sign(double number)
Returns +1 for positive numbers, -1 for negative numbers, and 0 otherwise.
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
Affine transformation classes.