circle_layout.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/property_map/property_map.hpp>
  16. #include <boost/static_assert.hpp>
  17. namespace boost
  18. {
  19. /**
  20. * \brief Layout the graph with the vertices at the points of a regular
  21. * n-polygon.
  22. *
  23. * The distance from the center of the polygon to each point is
  24. * determined by the @p radius parameter. The @p position parameter
  25. * must be an Lvalue Property Map whose value type is a class type
  26. * containing @c x and @c y members that will be set to the @c x and
  27. * @c y coordinates.
  28. */
  29. template < typename VertexListGraph, typename PositionMap, typename Radius >
  30. void circle_graph_layout(
  31. const VertexListGraph& g, PositionMap position, Radius radius)
  32. {
  33. BOOST_STATIC_ASSERT(
  34. property_traits< PositionMap >::value_type::dimensions >= 2);
  35. const double pi = boost::math::constants::pi< double >();
  36. #ifndef BOOST_NO_STDC_NAMESPACE
  37. using std::cos;
  38. using std::sin;
  39. #endif // BOOST_NO_STDC_NAMESPACE
  40. typedef typename graph_traits< VertexListGraph >::vertices_size_type
  41. vertices_size_type;
  42. vertices_size_type n = num_vertices(g);
  43. vertices_size_type i = 0;
  44. double two_pi_over_n = 2. * pi / n;
  45. BGL_FORALL_VERTICES_T(v, g, VertexListGraph)
  46. {
  47. position[v][0] = radius * cos(i * two_pi_over_n);
  48. position[v][1] = radius * sin(i * two_pi_over_n);
  49. ++i;
  50. }
  51. }
  52. } // end namespace boost
  53. #endif // BOOST_GRAPH_CIRCLE_LAYOUT_HPP