xref: /haiku/headers/libs/agg/agg_path_length.h (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 //          mcseemagg@yahoo.com
13 //          http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 #ifndef AGG_PATH_LENGTH_INCLUDED
16 #define AGG_PATH_LENGTH_INCLUDED
17 
18 #include "agg_math.h"
19 
20 namespace agg
21 {
22     template<class VertexSource>
23     double path_length(VertexSource& vs, unsigned path_id = 0)
24     {
25         double len = 0.0;
26         double start_x = 0.0;
27         double start_y = 0.0;
28         double x1 = 0.0;
29         double y1 = 0.0;
30         double x2 = 0.0;
31         double y2 = 0.0;
32         bool first = true;
33 
34         unsigned cmd;
35         vs.rewind(path_id);
36         while(!is_stop(cmd = vs.vertex(&x2, &y2)))
37         {
38             if(is_vertex(cmd))
39             {
40                 if(first || is_move_to(cmd))
41                 {
42                     start_x = x2;
43                     start_y = y2;
44                 }
45                 else
46                 {
47                     len += calc_distance(x1, y1, x2, y2);
48                 }
49                 x1 = x2;
50                 y1 = y2;
51                 first = false;
52             }
53             else
54             {
55                 if(is_close(cmd) && !first)
56                 {
57                     len += calc_distance(x1, y1, start_x, start_y);
58                 }
59             }
60         }
61         return len;
62     }
63 }
64 
65 #endif
66