~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to include/ggl/algorithms/point_on_line.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Generic Geometry Library
 
2
//
 
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)
 
8
 
 
9
#ifndef GGL_ALGORITHMS_POINT_ON_LINE_HPP
 
10
#define GGL_ALGORITHMS_POINT_ON_LINE_HPP
 
11
 
 
12
#include <boost/concept/requires.hpp>
 
13
 
 
14
#include <ggl/core/concepts/point_concept.hpp>
 
15
#include <ggl/algorithms/distance.hpp>
 
16
 
 
17
namespace ggl
 
18
{
 
19
 
 
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
 
31
// Date         : spring 1996
 
32
//----------------------------------------------------------------------
 
33
template <typename P, typename L>
 
34
bool point_on_linestring(const L& line, const double& position, P& point)
 
35
{
 
36
    double current_distance = 0.0;
 
37
    if (line.size() < 2)
 
38
    {
 
39
        return false;
 
40
    }
 
41
 
 
42
    typename L::const_iterator vertex = line.begin();
 
43
    typename L::const_iterator previous = vertex++;
 
44
 
 
45
    while (vertex != line.end())
 
46
    {
 
47
        double const dist = distance(*previous, *vertex);
 
48
        current_distance += dist;
 
49
 
 
50
        if (current_distance > position)
 
51
        {
 
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);
 
55
 
 
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);
 
60
 
 
61
            return true;
 
62
        }
 
63
        previous = vertex++;
 
64
    }
 
65
 
 
66
    // point at specified position does not lie on line
 
67
    return false;
 
68
}
 
69
 
 
70
} // namespace ggl
 
71
 
 
72
#endif // GGL_ALGORITHMS_POINT_ON_LINE_HPP