xref: /haiku/headers/libs/agg/agg_shorten_path.h (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
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 
16 #ifndef AGG_SHORTEN_PATH_INCLUDED
17 #define AGG_SHORTEN_PATH_INCLUDED
18 
19 #include "agg_basics.h"
20 #include "agg_vertex_sequence.h"
21 
22 namespace agg
23 {
24 
25     //===========================================================shorten_path
26     template<class VertexSequence>
27     void shorten_path(VertexSequence& vs, double s, unsigned closed = 0)
28     {
29         typedef typename VertexSequence::value_type vertex_type;
30 
31         if(s > 0.0 && vs.size() > 1)
32         {
33             double d;
34             int n = int(vs.size() - 2);
35             while(n)
36             {
37                 d = vs[n].dist;
38                 if(d > s) break;
39                 vs.remove_last();
40                 s -= d;
41                 --n;
42             }
43             if(vs.size() < 2)
44             {
45                 vs.remove_all();
46             }
47             else
48             {
49                 n = vs.size() - 1;
50                 vertex_type& prev = vs[n-1];
51                 vertex_type& last = vs[n];
52                 d = (prev.dist - s) / prev.dist;
53                 double x = prev.x + (last.x - prev.x) * d;
54                 double y = prev.y + (last.y - prev.y) * d;
55                 last.x = x;
56                 last.y = y;
57                 if(!prev(last)) vs.remove_last();
58                 vs.close(closed != 0);
59             }
60         }
61     }
62 
63 
64 }
65 
66 #endif
67