qwt_clipper.cpp 12.5 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_clipper.h"
Bryant's avatar
Bryant committed
11 12 13 14
#include "qwt_point_polar.h"
#include <qrect.h>
#include <string.h>
#include <stdlib.h>
pixhawk's avatar
pixhawk committed
15

Bryant's avatar
Bryant committed
16 17
#if QT_VERSION < 0x040601
#define qAtan(x) ::atan(x)
pixhawk's avatar
pixhawk committed
18 19
#endif

Bryant's avatar
Bryant committed
20 21 22 23 24 25 26
namespace QwtClip
{
    // some templates used for inlining
    template <class Point, typename T> class LeftEdge;
    template <class Point, typename T> class RightEdge;
    template <class Point, typename T> class TopEdge;
    template <class Point, typename T> class BottomEdge;
pixhawk's avatar
pixhawk committed
27

Bryant's avatar
Bryant committed
28 29 30 31 32
    template <class Point> class PointBuffer;
}

template <class Point, typename Value>
class QwtClip::LeftEdge
pixhawk's avatar
pixhawk committed
33 34
{
public:
Bryant's avatar
Bryant committed
35 36 37 38
    inline LeftEdge( Value x1, Value, Value, Value ):
        d_x1( x1 )
    {
    }
pixhawk's avatar
pixhawk committed
39

Bryant's avatar
Bryant committed
40 41 42 43
    inline bool isInside( const Point &p  ) const
    {
        return p.x() >= d_x1;
    }
pixhawk's avatar
pixhawk committed
44

Bryant's avatar
Bryant committed
45 46 47 48 49
    inline Point intersection( const Point &p1, const Point &p2 ) const
    {
        double dy = ( p1.y() - p2.y() ) / double( p1.x() - p2.x() );
        return Point( d_x1, static_cast< Value >( p2.y() + ( d_x1 - p2.x() ) * dy ) );
    }
pixhawk's avatar
pixhawk committed
50
private:
Bryant's avatar
Bryant committed
51
    const Value d_x1;
pixhawk's avatar
pixhawk committed
52 53
};

Bryant's avatar
Bryant committed
54 55
template <class Point, typename Value>
class QwtClip::RightEdge
pixhawk's avatar
pixhawk committed
56 57
{
public:
Bryant's avatar
Bryant committed
58 59 60 61
    inline RightEdge( Value, Value x2, Value, Value ):
        d_x2( x2 )
    {
    }
pixhawk's avatar
pixhawk committed
62

Bryant's avatar
Bryant committed
63 64 65 66 67 68 69 70 71 72
    inline bool isInside( const Point &p  ) const
    {
        return p.x() <= d_x2;
    }

    inline Point intersection( const Point &p1, const Point &p2 ) const
    {
        double dy = ( p1.y() - p2.y() ) / double( p1.x() - p2.x() );
        return Point( d_x2, static_cast<Value>( p2.y() + ( d_x2 - p2.x() ) * dy ) );
    }
pixhawk's avatar
pixhawk committed
73

Bryant's avatar
Bryant committed
74 75
private:
    const Value d_x2;
pixhawk's avatar
pixhawk committed
76 77
};

Bryant's avatar
Bryant committed
78 79
template <class Point, typename Value>
class QwtClip::TopEdge
pixhawk's avatar
pixhawk committed
80 81
{
public:
Bryant's avatar
Bryant committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    inline TopEdge( Value, Value, Value y1, Value ):
        d_y1( y1 )
    {
    }

    inline bool isInside( const Point &p  ) const
    {
        return p.y() >= d_y1;
    }

    inline Point intersection( const Point &p1, const Point &p2 ) const
    {
        double dx = ( p1.x() - p2.x() ) / double( p1.y() - p2.y() );
        return Point( static_cast<Value>( p2.x() + ( d_y1 - p2.y() ) * dx ), d_y1 );
    }
pixhawk's avatar
pixhawk committed
97 98

private:
Bryant's avatar
Bryant committed
99
    const Value d_y1;
pixhawk's avatar
pixhawk committed
100 101
};

Bryant's avatar
Bryant committed
102 103
template <class Point, typename Value>
class QwtClip::BottomEdge
pixhawk's avatar
pixhawk committed
104
{
Bryant's avatar
Bryant committed
105 106 107 108 109
public:
    inline BottomEdge( Value, Value, Value, Value y2 ):
        d_y2( y2 )
    {
    }
pixhawk's avatar
pixhawk committed
110