x01_qt_example.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. //
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // Qt Example
  8. // Qt is a well-known and often used platform independent windows library
  9. // To build and run this example:
  10. // 1) download (from http://qt.nokia.com), configure and make QT
  11. // 2) if necessary, adapt Qt clause in include path (note there is a Qt property sheet)
  12. #include <sstream>
  13. #include <QtGui>
  14. #include <boost/geometry/geometry.hpp>
  15. #include <boost/geometry/geometries/register/point.hpp>
  16. #include <boost/geometry/geometries/register/ring.hpp>
  17. // Adapt a QPointF such that it can be handled by Boost.Geometry
  18. BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(QPointF, double, cs::cartesian, x, y, setX, setY)
  19. // Adapt a QPolygonF as well.
  20. // A QPolygonF has no holes (interiors) so it is similar to a Boost.Geometry ring
  21. BOOST_GEOMETRY_REGISTER_RING(QPolygonF)
  22. int main(int argc, char *argv[])
  23. {
  24. // This usage QApplication and QLabel is adapted from
  25. // http://en.wikipedia.org/wiki/Qt_(toolkit)#Qt_hello_world
  26. QApplication app(argc, argv);
  27. // Declare a Qt polygon. The Qt Polygon can be used
  28. // in Boost.Geometry, just by its oneline registration above.
  29. QPolygonF polygon;
  30. // Use Qt to add points to polygon
  31. polygon
  32. << QPointF(10, 20) << QPointF(20, 30)
  33. << QPointF(30, 20) << QPointF(20, 10)
  34. << QPointF(10, 20);
  35. // Use Boost.Geometry e.g. to calculate area
  36. std::ostringstream out;
  37. out << "Boost.Geometry area: " << boost::geometry::area(polygon) << std::endl;
  38. // Some functionality is defined in both Qt and Boost.Geometry
  39. QPointF p(20,20);
  40. out << "Qt contains: "
  41. << (polygon.containsPoint(p, Qt::WindingFill) ? "yes" : "no")
  42. << std::endl
  43. << "Boost.Geometry within: "
  44. << (boost::geometry::within(p, polygon) ? "yes" : "no")
  45. << std::endl;
  46. // Detail: if point is ON boundary, Qt says yes, Boost.Geometry says no.
  47. // Qt defines an iterator
  48. // (which is required for of the Boost.Geometry ring-concept)
  49. // such that Boost.Geometry can use the points of this polygon
  50. QPolygonF::const_iterator it;
  51. for (it = polygon.begin(); it != polygon.end(); ++it)
  52. {
  53. // Stream Delimiter-Separated, just to show something Boost.Geometry can do
  54. out << boost::geometry::dsv(*it) << std::endl;
  55. }
  56. // Stream the polygon as well
  57. out << boost::geometry::dsv(polygon) << std::endl;
  58. // Just show what we did in a label
  59. QLabel label(out.str().c_str());
  60. label.show();
  61. return app.exec();
  62. }