circle_layout.hpp 1.9 KB

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