circle_layout.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2004 The Trustees of Indiana University.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_CIRCLE_LAYOUT_HPP
  8. #define BOOST_GRAPH_CIRCLE_LAYOUT_HPP
  9. #include <boost/config/no_tr1/cmath.hpp>
  10. #include <boost/math/constants/constants.hpp>
  11. #include <utility>
  12. #include <boost/graph/graph_traits.hpp>
  13. #include <boost/graph/iteration_macros.hpp>
  14. #include <boost/graph/topology.hpp>
  15. #include <boost/static_assert.hpp>
  16. namespace boost
  17. {
  18. /**
  19. * \brief Layout the graph with the vertices at the points of a regular
  20. * n-polygon.
  21. *
  22. * The distance from the center of the polygon to each point is
  23. * determined by the @p radius parameter. The @p position parameter
  24. * must be an Lvalue Property Map whose value type is a class type
  25. * containing @c x and @c y members that will be set to the @c x and
  26. * @c y coordinates.
  27. */
  28. template < typename VertexListGraph, typename PositionMap, typename Radius >
  29. void circle_graph_layout(
  30. const VertexListGraph& g, PositionMap position, Radius radius)
  31. {
  32. BOOST_STATIC_ASSERT(
  33. property_traits< PositionMap >::value_type::dimensions >= 2);
  34. const double pi = boost::math::constants::pi< double >();
  35. #ifndef BOOST_NO_STDC_NAMESPACE
  36. using std::cos;
  37. using std::sin;
  38. #endif // BOOST_NO_STDC_NAMESPACE
  39. typedef typename graph_traits< VertexListGraph >::vertices_size_type
  40. vertices_size_type;
  41. vertices_size_type n = num_vertices(g);
  42. vertices_size_type i = 0;
  43. double two_pi_over_n = 2. * pi / n;
  44. BGL_FORALL_VERTICES_T(v, g, VertexListGraph)
  45. {
  46. position[v][0] = radius * cos(i * two_pi_over_n);
  47. position[v][1] = radius * sin(i * two_pi_over_n);
  48. ++i;
  49. }
  50. }
  51. } // end namespace boost
  52. #endif // BOOST_GRAPH_CIRCLE_LAYOUT_HPP