LIBRCSC Docs
Documentation for HELIOS's BASE LIBRCSC library for RoboCup 2D Simulation League.
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
line_2d.h
Go to the documentation of this file.
1// -*-c++-*-
2
8/*
9 *Copyright:
10
11 Copyright (C) Hidehisa Akiyama
12
13 This code is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27 *EndCopyright:
28 */
29
31
32#ifndef RCSC_GEOM_LINE2D_H
33#define RCSC_GEOM_LINE2D_H
34
35#include <rcsc/geom/vector_2d.h>
36
37#include <cmath>
38
39namespace rcsc {
40
47class Line2D {
48public:
49
50 static const double EPSILON;
51 static const double ERROR_VALUE;
52
53private:
54
55 double M_a;
56 double M_b;
57 double M_c;
58
59 // never used
60 Line2D();
61
62public:
69 Line2D( const double a,
70 const double b,
71 const double c )
72 : M_a( a ),
73 M_b( b ),
74 M_c( c )
75 { }
76
82 Line2D( const Vector2D & p1,
83 const Vector2D & p2 )
84 {
85 assign( p1, p2 );
86 }
87
93 Line2D( const Vector2D & origin,
94 const AngleDeg & linedir )
95 {
96 assign( origin, linedir );
97 }
98
105 const Line2D & assign( const Vector2D & p1,
106 const Vector2D & p2 )
107 {
108 M_a = -( p2.y - p1.y );
109 M_b = p2.x - p1.x;
110 M_c = -M_a * p1.x - M_b * p1.y;
111 return *this;
112 }
113
120 const Line2D & assign( const Vector2D & origin,
121 const AngleDeg & linedir )
122 {
123 M_a = - linedir.sin();
124 M_b = linedir.cos();
125 M_c = -M_a * origin.x - M_b * origin.y;
126 return *this;
127 }
128
133 double a() const
134 {
135 return M_a;
136 }
137
142 double b() const
143 {
144 return M_b;
145 }
146
151 double c() const
152 {
153 return M_c;
154 }
155
161 double getX( const double y ) const
162 {
163 if ( std::fabs( M_a ) < EPSILON )
164 {
165 return ERROR_VALUE;
166 }
167 return -( M_b * y + M_c ) / M_a;
168 }
169
175 double getY( const double x ) const
176 {
177 if ( std::fabs( M_b ) < EPSILON )
178 {
179 return ERROR_VALUE;
180 }
181
182 return -( M_a * x + M_c ) / M_b;
183 }
184
190 double dist( const Vector2D & p ) const
191 {
192 return std::fabs( ( M_a * p.x + M_b * p.y + M_c )
193 / std::sqrt( M_a * M_a + M_b * M_b ) );
194 }
200 double dist2( const Vector2D & p ) const
201 {
202 double d = M_a * p.x + M_b * p.y + M_c;
203 return (d * d) / (M_a * M_a + M_b * M_b);
204 }
205
212 bool isParallel( const Line2D & line ) const
213 {
214 return std::fabs( a() * line.b() - line.a() * b() ) < EPSILON;
215 }
216
223 Vector2D intersection( const Line2D & line ) const
224 {
225 return intersection( *this, line );
226 }
227
233 Line2D perpendicular( const Vector2D & p ) const
234 {
235 return Line2D( b(), -a(), a() * p.y - b() * p.x );
236 }
237
243 Vector2D projection( const Vector2D & p ) const
244 {
245 return intersection( perpendicular( p ) );
246 }
247
248 // static utility
249
257 static
259 const Line2D & line2 );
260
268 static
270 const AngleDeg & left,
271 const AngleDeg & right )
272 {
273 return Line2D( origin, AngleDeg::bisect( left, right ) );
274 }
275
282 static
284 const Vector2D & p2 );
285
286};
287
288}
289
290#endif
degree wrapper class
Definition: angle_deg.h:45
double cos() const
calculate cosine
Definition: angle_deg.h:292
static AngleDeg bisect(const AngleDeg &left, const AngleDeg &right)
static utility that returns bisect angle of [left, right].
double sin() const
calculate sine
Definition: angle_deg.h:301
2d straight line class
Definition: line_2d.h:47
static const double ERROR_VALUE
error value
Definition: line_2d.h:51
double a() const
accessor
Definition: line_2d.h:133
static Vector2D intersection(const Line2D &line1, const Line2D &line2)
get the intersection point of 2 lines
Line2D(const Vector2D &origin, const AngleDeg &linedir)
construct from origin point + direction
Definition: line_2d.h:93
double getY(const double x) const
get Y-coordinate correspond to 'x'
Definition: line_2d.h:175
static const double EPSILON
tolerance threshold
Definition: line_2d.h:50
double b() const
accessor
Definition: line_2d.h:142
const Line2D & assign(const Vector2D &p1, const Vector2D &p2)
construct from 2 points
Definition: line_2d.h:105
const Line2D & assign(const Vector2D &origin, const AngleDeg &linedir)
construct from origin point + direction
Definition: line_2d.h:120
double dist2(const Vector2D &p) const
get squared distance from this line to point
Definition: line_2d.h:200
static Line2D angle_bisector(const Vector2D &origin, const AngleDeg &left, const AngleDeg &right)
make angle bisector line from two angles
Definition: line_2d.h:269
double c() const
accessor
Definition: line_2d.h:151
double dist(const Vector2D &p) const
calculate distance from point to this line
Definition: line_2d.h:190
double getX(const double y) const
get X-coordinate correspond to 'y'
Definition: line_2d.h:161
bool isParallel(const Line2D &line) const
check if the slope of this line is same to the slope of 'line'
Definition: line_2d.h:212
Vector2D projection(const Vector2D &p) const
calc projection point from p
Definition: line_2d.h:243
Line2D(const Vector2D &p1, const Vector2D &p2)
construct from 2 points
Definition: line_2d.h:82
static Line2D perpendicular_bisector(const Vector2D &p1, const Vector2D &p2)
make perpendicular bisector line from twt points
Line2D perpendicular(const Vector2D &p) const
calc perpendicular line (SUI-SEN)
Definition: line_2d.h:233
Vector2D intersection(const Line2D &line) const
get the intersection point with 'line'
Definition: line_2d.h:223
Line2D(const double a, const double b, const double c)
construct directly
Definition: line_2d.h:69
2D point vector class
Definition: vector_2d.h:46
double y
Y coordinate.
Definition: vector_2d.h:64
double x
X coordinate.
Definition: vector_2d.h:63
2d vector class Header File.