Home | History | Annotate | Download | only in agg23
      1 
      2 //----------------------------------------------------------------------------
      3 // Anti-Grain Geometry - Version 2.3
      4 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
      5 //
      6 // Permission to copy, use, modify, sell and distribute this software
      7 // is granted provided this copyright notice appears in all copies.
      8 // This software is provided "as is" without express or implied
      9 // warranty, and with no claim as to its suitability for any purpose.
     10 //
     11 //----------------------------------------------------------------------------
     12 // Contact: mcseem (at) antigrain.com
     13 //          mcseemagg (at) yahoo.com
     14 //          http://www.antigrain.com
     15 //----------------------------------------------------------------------------
     16 #ifndef AGG_SHORTEN_PATH_INCLUDED
     17 #define AGG_SHORTEN_PATH_INCLUDED
     18 #include "agg_basics.h"
     19 #include "agg_vertex_sequence.h"
     20 namespace agg
     21 {
     22 template<class VertexSequence>
     23 void shorten_path(VertexSequence& vs, FX_FLOAT s, unsigned closed = 0)
     24 {
     25     typedef typename VertexSequence::value_type vertex_type;
     26     if(s > 0 && vs.size() > 1) {
     27         FX_FLOAT d;
     28         int n = int(vs.size() - 2);
     29         while(n) {
     30             d = vs[n].dist;
     31             if(d > s) {
     32                 break;
     33             }
     34             vs.remove_last();
     35             s -= d;
     36             --n;
     37         }
     38         if(vs.size() < 2) {
     39             vs.remove_all();
     40         } else {
     41             n = vs.size() - 1;
     42             vertex_type& prev = vs[n - 1];
     43             vertex_type& last = vs[n];
     44             d = (prev.dist - s) / prev.dist;
     45             FX_FLOAT x = prev.x + (last.x - prev.x) * d;
     46             FX_FLOAT y = prev.y + (last.y - prev.y) * d;
     47             last.x = x;
     48             last.y = y;
     49             if(!prev(last)) {
     50                 vs.remove_last();
     51             }
     52             vs.close(closed != 0);
     53         }
     54     }
     55 }
     56 }
     57 #endif
     58