Anti-Grain Geometry - AGG (libagg)  2.5
agg-2.5/include/agg_shorten_path.h
Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 // Anti-Grain Geometry (AGG) - Version 2.5
00003 // A high quality rendering engine for C++
00004 // Copyright (C) 2002-2006 Maxim Shemanarev
00005 // Contact: mcseem@antigrain.com
00006 //          mcseemagg@yahoo.com
00007 //          http://antigrain.com
00008 // 
00009 // AGG is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 // 
00014 // AGG is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU General Public License
00020 // along with AGG; if not, write to the Free Software
00021 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00022 // MA 02110-1301, USA.
00023 //----------------------------------------------------------------------------
00024 
00025 #ifndef AGG_SHORTEN_PATH_INCLUDED
00026 #define AGG_SHORTEN_PATH_INCLUDED
00027 
00028 #include "agg_basics.h"
00029 #include "agg_vertex_sequence.h"
00030 
00031 namespace agg
00032 {
00033 
00034     //===========================================================shorten_path
00035     template<class VertexSequence> 
00036     void shorten_path(VertexSequence& vs, double s, unsigned closed = 0)
00037     {
00038         typedef typename VertexSequence::value_type vertex_type;
00039 
00040         if(s > 0.0 && vs.size() > 1)
00041         {
00042             double d;
00043             int n = int(vs.size() - 2);
00044             while(n)
00045             {
00046                 d = vs[n].dist;
00047                 if(d > s) break;
00048                 vs.remove_last();
00049                 s -= d;
00050                 --n;
00051             }
00052             if(vs.size() < 2)
00053             {
00054                 vs.remove_all();
00055             }
00056             else
00057             {
00058                 n = vs.size() - 1;
00059                 vertex_type& prev = vs[n-1];
00060                 vertex_type& last = vs[n];
00061                 d = (prev.dist - s) / prev.dist;
00062                 double x = prev.x + (last.x - prev.x) * d;
00063                 double y = prev.y + (last.y - prev.y) * d;
00064                 last.x = x;
00065                 last.y = y;
00066                 if(!prev(last)) vs.remove_last();
00067                 vs.close(closed != 0);
00068             }
00069         }
00070     }
00071 
00072 
00073 }
00074 
00075 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines