3
* \brief Infinite Straight Line
5
* Copyright 2008 Marco Cecchetti <mrcekets at gmail.com>
7
* This library is free software; you can redistribute it and/or
8
* modify it either under the terms of the GNU Lesser General Public
9
* License version 2.1 as published by the Free Software Foundation
10
* (the "LGPL") or, at your option, under the terms of the Mozilla
11
* Public License Version 1.1 (the "MPL"). If you do not alter this
12
* notice, a recipient may use your version of this file under either
13
* the MPL or the LGPL.
15
* You should have received a copy of the LGPL along with this library
16
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
* You should have received a copy of the MPL along with this library
19
* in the file COPYING-MPL-1.1
21
* The contents of this file are subject to the Mozilla Public License
22
* Version 1.1 (the "License"); you may not use this file except in
23
* compliance with the License. You may obtain a copy of the License at
24
* http://www.mozilla.org/MPL/
26
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
28
* the specific language governing rights and limitations.
31
#ifndef _2GEOM_LINE_H_
32
#define _2GEOM_LINE_H_
37
#include <2geom/bezier-curve.h> // for LineSegment
38
#include <2geom/crossing.h>
39
#include <2geom/exception.h>
41
#include <2geom/ray.h>
51
: m_origin(0,0), m_versor(1,0)
55
Line(Point const& _origin, Coord angle )
56
: m_origin(_origin), m_versor(std::cos(angle), std::sin(angle))
60
Line(Point const& A, Point const& B)
66
Line(LineSegment const& _segment)
68
setBy2Points(_segment.initialPoint(), _segment.finalPoint());
73
: m_origin(_ray.origin()), m_versor(_ray.versor())
77
static Line fromNormalDistance(Point n, double c) {
78
Point P = n*c/(dot(n,n));
80
return Line(P, P+rot90(n));
82
static Line fromPointDirection(Point o, Point v) {
89
Line* duplicate() const
91
return new Line(*this);
104
void origin(Point const& _point)
109
void versor(Point const& _versor)
114
// return the angle described by rotating the X-axis in cw direction
115
// until it overlaps the line
116
// the returned value is in the interval [0, PI[
119
double a = std::atan2(m_versor[Y], m_versor[X]);
120
if (a < 0) a += M_PI;
121
if (a == M_PI) a = 0;
125
void angle(Coord _angle)
127
m_versor[X] = std::cos(_angle);
128
m_versor[Y] = std::sin(_angle);
131
void setBy2Points(Point const& A, Point const& B)
135
if ( are_near(m_versor, Point(0,0)) )
136
m_versor = Point(0,0);
138
m_versor.normalize();
141
bool isDegenerate() const
143
return ( m_versor[X] == 0 && m_versor[Y] == 0 );
146
Point pointAt(Coord t) const
148
return m_origin + m_versor * t;
151
Coord valueAt(Coord t, Dim2 d) const
154
THROW_RANGEERROR("Ray::valueAt, dimension argument out of range");
155
return m_origin[d] + m_versor[d] * t;
158
std::vector<Coord> roots(Coord v, Dim2 d) const
161
THROW_RANGEERROR("Ray::roots, dimension argument out of range");
162
std::vector<Coord> result;
163
if ( m_versor[d] != 0 )
165
result.push_back( (v - m_origin[d]) / m_versor[d] );
171
// require are_near(_point, *this)
172
// on the contrary the result value is meaningless
173
Coord timeAt(Point const& _point) const
176
if ( m_versor[X] != 0 )
178
t = (_point[X] - m_origin[X]) / m_versor[X];
180
else if ( m_versor[Y] != 0 )
182
t = (_point[Y] - m_origin[Y]) / m_versor[Y];
184
else // degenerate case
191
Coord timeAtProjection(Point const& _point) const
193
if ( isDegenerate() ) return 0;
194
return dot( _point - m_origin, m_versor );
197
Coord nearestPoint(Point const& _point) const
199
return timeAtProjection(_point);
205
result.origin(m_origin);
206
result.versor(-m_versor);
210
Curve* portion(Coord f, Coord t) const
212
LineSegment* seg = new LineSegment(pointAt(f), pointAt(t));
216
LineSegment segment(Coord f, Coord t) const
218
return LineSegment(pointAt(f), pointAt(t));
224
result.origin(pointAt(t));
225
result.versor(m_versor);
229
Line derivative() const
232
result.origin(m_versor);
233
result.versor(Point(0,0));
237
Line transformed(Matrix const& m) const
239
return Line(m_origin * m, (m_origin + m_versor) * m);
242
static Line from_normal_and_dist(Point const &n, double d) {
243
return Line(n*d, n*d + rot90(n));
254
double distance(Point const& _point, Line const& _line)
256
if ( _line.isDegenerate() )
258
return distance( _point, _line.origin() );
262
return fabs( dot(_point - _line.origin(), _line.versor().ccw()) );
267
bool are_near(Point const& _point, Line const& _line, double eps = EPSILON)
269
return are_near(distance(_point, _line), 0, eps);
273
bool are_parallel(Line const& l1, Line const& l2, double eps = EPSILON)
275
return ( are_near(l1.versor(), l2.versor(), eps)
276
|| are_near(l1.versor(), -l2.versor(), eps) );
280
bool are_same(Line const& l1, Line const& l2, double eps = EPSILON)
282
return are_parallel(l1, l2, eps) && are_near(l1.origin(), l2, eps);
286
bool are_orthogonal(Line const& l1, Line const& l2, double eps = EPSILON)
288
return ( are_near(l1.versor(), l2.versor().cw(), eps)
289
|| are_near(l1.versor(), l2.versor().ccw(), eps) );
293
bool are_collinear(Point const& p1, Point const& p2, Point const& p3,
294
double eps = EPSILON)
296
return are_near( cross(p3, p2) - cross(p3, p1) + cross(p2, p1), 0, eps);
299
// evaluate the angle between l1 and l2 rotating l1 in cw direction
300
// until it overlaps l2
301
// the returned value is an angle in the interval [0, PI[
303
double angle_between(Line const& l1, Line const& l2)
305
double angle = angle_between(l1.versor(), l2.versor());
306
if (angle < 0) angle += M_PI;
307
if (angle == M_PI) angle = 0;
312
double distance(Point const& _point, LineSegment const& _segment)
314
double t = _segment.nearestPoint(_point);
315
return L2(_point - _segment.pointAt(t));
319
bool are_near(Point const& _point, LineSegment const& _segment,
320
double eps = EPSILON)
322
return are_near(distance(_point, _segment), 0, eps);
325
// build a line passing by _point and orthogonal to _line
327
Line make_orthogonal_line(Point const& _point, Line const& _line)
331
l.versor(_line.versor().cw());
335
// build a line passing by _point and parallel to _line
337
Line make_parallel_line(Point const& _point, Line const& _line)
344
// build a line passing by the middle point of _segment and orthogonal to it.
346
Line make_bisector_line(LineSegment const& _segment)
348
return make_orthogonal_line( middle_point(_segment), Line(_segment) );
351
// build the bisector line of the angle between ray(O,A) and ray(O,B)
353
Line make_angle_bisector_line(Point const& A, Point const& O, Point const& B)
355
Point M = middle_point(A,B);
359
// prj(P) = rot(v, Point( rot(-v, P-O)[X], 0 )) + O
361
Point projection(Point const& _point, Line const& _line)
363
return _line.pointAt( _line.nearestPoint(_point) );
367
LineSegment projection(LineSegment const& _segment, Line const& _line)
369
return _line.segment( _line.nearestPoint(_segment.initialPoint()),
370
_line.nearestPoint(_segment.finalPoint()) );
379
OptCrossing intersection_impl(Ray const& r1, Line const& l2, unsigned int i);
380
OptCrossing intersection_impl( LineSegment const& ls1,
383
OptCrossing intersection_impl( LineSegment const& ls1,
390
OptCrossing intersection(Ray const& r1, Line const& l2)
392
return detail::intersection_impl(r1, l2, 0);
397
OptCrossing intersection(Line const& l1, Ray const& r2)
399
return detail::intersection_impl(r2, l1, 1);
403
OptCrossing intersection(LineSegment const& ls1, Line const& l2)
405
return detail::intersection_impl(ls1, l2, 0);
409
OptCrossing intersection(Line const& l1, LineSegment const& ls2)
411
return detail::intersection_impl(ls2, l1, 1);
415
OptCrossing intersection(LineSegment const& ls1, Ray const& r2)
417
return detail::intersection_impl(ls1, r2, 0);
422
OptCrossing intersection(Ray const& r1, LineSegment const& ls2)
424
return detail::intersection_impl(ls2, r1, 1);
428
OptCrossing intersection(Line const& l1, Line const& l2);
430
OptCrossing intersection(Ray const& r1, Ray const& r2);
432
OptCrossing intersection(LineSegment const& ls1, LineSegment const& ls2);
435
} // end namespace Geom
438
#endif // _2GEOM_LINE_H_
444
c-file-style:"stroustrup"
445
c-file-offsets:((innamespace . 0)(substatement-open . 0))
450
vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :