qwt_pixel_matrix.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #ifndef QWT_PIXEL_MATRIX_H
  10. #define QWT_PIXEL_MATRIX_H
  11. #include "qwt_global.h"
  12. #include <qbitarray.h>
  13. #include <qrect.h>
  14. /*!
  15. \brief A bit field corresponding to the pixels of a rectangle
  16. QwtPixelMatrix is intended to filter out duplicates in an
  17. unsorted array of points.
  18. */
  19. class QWT_EXPORT QwtPixelMatrix: public QBitArray
  20. {
  21. public:
  22. QwtPixelMatrix( const QRect& rect );
  23. ~QwtPixelMatrix();
  24. void setRect( const QRect& rect );
  25. QRect rect() const;
  26. bool testPixel( int x, int y ) const;
  27. bool testAndSetPixel( int x, int y, bool on );
  28. int index( int x, int y ) const;
  29. private:
  30. QRect d_rect;
  31. };
  32. /*!
  33. \brief Test if a pixel has been set
  34. \param x X-coordinate
  35. \param y Y-coordinate
  36. \return true, when pos is outside of rect(), or when the pixel
  37. has already been set.
  38. */
  39. inline bool QwtPixelMatrix::testPixel( int x, int y ) const
  40. {
  41. const int idx = index( x, y );
  42. return ( idx >= 0 ) ? testBit( idx ) : true;
  43. }
  44. /*!
  45. \brief Set a pixel and test if a pixel has been set before
  46. \param x X-coordinate
  47. \param y Y-coordinate
  48. \param on Set/Clear the pixel
  49. \return true, when pos is outside of rect(), or when the pixel
  50. was set before.
  51. */
  52. inline bool QwtPixelMatrix::testAndSetPixel( int x, int y, bool on )
  53. {
  54. const int idx = index( x, y );
  55. if ( idx < 0 )
  56. return true;
  57. const bool onBefore = testBit( idx );
  58. setBit( idx, on );
  59. return onBefore;
  60. }
  61. /*!
  62. \brief Calculate the index in the bit field corresponding to a position
  63. \param x X-coordinate
  64. \param y Y-coordinate
  65. \return Index, when rect() contains pos - otherwise -1.
  66. */
  67. inline int QwtPixelMatrix::index( int x, int y ) const
  68. {
  69. const int dx = x - d_rect.x();
  70. if ( dx < 0 || dx >= d_rect.width() )
  71. return -1;
  72. const int dy = y - d_rect.y();
  73. if ( dy < 0 || dy >= d_rect.height() )
  74. return -1;
  75. return dy * d_rect.width() + dx;
  76. }
  77. #endif