1
// Generic Geometry Library
3
// Copyright Barend Gehrels 1995-2009, Geodan Holding B.V. Amsterdam, the Netherlands.
4
// Copyright Bruno Lalande 2008, 2009
5
// Use, modification and distribution is subject to the Boost Software License,
6
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7
// http://www.boost.org/LICENSE_1_0.txt)
9
#ifndef GGL_ALGORITHMS_POINT_ON_LINE_HPP
10
#define GGL_ALGORITHMS_POINT_ON_LINE_HPP
12
#include <boost/concept/requires.hpp>
14
#include <ggl/core/concepts/point_concept.hpp>
15
#include <ggl/algorithms/distance.hpp>
20
//----------------------------------------------------------------------
21
// Function : point_on_linestring -> rename to alongLine NO, different
22
//----------------------------------------------------------------------
23
// Purpose : Calculates coordinates of a point along a given line
24
// on a specified distance
25
// Parameters : const L& : line,
26
// float position: position to calculate point
27
// P& point: point to calculate
28
// Return : true if point lies on line
29
//----------------------------------------------------------------------
30
// Author : Barend, Geodan BV Amsterdam
32
//----------------------------------------------------------------------
33
template <typename P, typename L>
34
bool point_on_linestring(const L& line, const double& position, P& point)
36
double current_distance = 0.0;
42
typename L::const_iterator vertex = line.begin();
43
typename L::const_iterator previous = vertex++;
45
while (vertex != line.end())
47
double const dist = distance(*previous, *vertex);
48
current_distance += dist;
50
if (current_distance > position)
52
// It is not possible that dist == 0 here because otherwise
53
// the current_distance > position would not become true (current_distance is increased by dist)
54
double const fraction = 1.0 - ((current_distance - position) / dist);
56
// point i is too far, point i-1 to near, add fraction of
57
// distance in each direction
58
point.x ( previous->x() + (vertex->x() - previous->x()) * fraction);
59
point.y ( previous->y() + (vertex->y() - previous->y()) * fraction);
66
// point at specified position does not lie on line
72
#endif // GGL_ALGORITHMS_POINT_ON_LINE_HPP