agg_path_length.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include "agg_math.h"
  18. namespace agg
  19. {
  20. template<class VertexSource>
  21. double path_length(VertexSource& vs, unsigned path_id = 0)
  22. {
  23. double len = 0.0;
  24. double start_x = 0.0;
  25. double start_y = 0.0;
  26. double x1 = 0.0;
  27. double y1 = 0.0;
  28. double x2 = 0.0;
  29. double y2 = 0.0;
  30. bool first = true;
  31. unsigned cmd;
  32. vs.rewind(path_id);
  33. while(!is_stop(cmd = vs.vertex(&x2, &y2)))
  34. {
  35. if(is_vertex(cmd))
  36. {
  37. if(first || is_move_to(cmd))
  38. {
  39. start_x = x2;
  40. start_y = y2;
  41. }
  42. else
  43. {
  44. len += calc_distance(x1, y1, x2, y2);
  45. }
  46. x1 = x2;
  47. y1 = y2;
  48. first = false;
  49. }
  50. else
  51. {
  52. if(is_close(cmd) && !first)
  53. {
  54. len += calc_distance(x1, y1, start_x, start_y);
  55. }
  56. }
  57. }
  58. return len;
  59. }
  60. }
  61. #endif