Bonjour,

Je suis à la recherche d’un algorithme qui calcule une surface quelconque, j'utilise les méthodes de QPainter.

Ci-dessous l'exemple :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
 
 
class Drawing : public QMainWindow
{
    Q_OBJECT
 
public:
    Drawing(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~Drawing();
 
private:
    Ui::DrawingClass ui;
    QPoint dtPnt;
    bool blLftbtnPrsd ;
    QVector<QPoint> polyPoints;
    QRect echlRct;
    int extrmX;
    int extrmY;
 
protected:
    void mouseMoveEvent(QMouseEvent *e);
    void paintEvent(QPaintEvent *e);
    void mouseReleaseEvent ( QMouseEvent *e  );
    void mousePressEvent ( QMouseEvent *e ) ;
 
};
 
#endif // DRAWING_H
 
 
#include "drawing.h"
 
Drawing::Drawing(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    blLftbtnPrsd = false;
}
 
Drawing::~Drawing()
{
 
}
 
void Drawing::mousePressEvent ( QMouseEvent *e )
{
    if (e->button() == Qt::LeftButton)   
        {
        blLftbtnPrsd = true;
        polyPoints.clear();
 
        }
 
    if (e->button() == Qt::RightButton)
        {
        polyPoints.clear();
        extrmX = 0;
        extrmY = 0;
        update();
 
        }
 
 
}
 
void Drawing::mouseReleaseEvent ( QMouseEvent *e  )
{
    blLftbtnPrsd = false;  
    if (!polyPoints.isEmpty())
        {
            polyPoints << polyPoints.at(0);
            extrmX = 0 ;
            extrmY = 0;
 
             if (!polyPoints.isEmpty())
             {
 
                extrmX = this->width();
 
 
                foreach(QPoint pnt, polyPoints)
                {
                    if (pnt.x() < extrmX)
                        extrmX = pnt.x();
 
                    if (pnt.y() > extrmY)
                        extrmY = pnt.y();
 
                }        
            }
            update();
        }
}
 
void Drawing::mouseMoveEvent(QMouseEvent *e)
{
 
    if (blLftbtnPrsd && e->state() == Qt::LeftButton)
        {
            dtPnt.setX(e->x());
            dtPnt.setY(e->y());
            polyPoints << dtPnt ;
            update();
        }
 
}
 
void Drawing::paintEvent(QPaintEvent* e) 
{ 
    QPainter painter(this);
 
    // Grille
    int siUnte = 10;
 
    int siHght = this->height() - 80;
    int siWdth = this->width() - 80;
    int nbItrHght = (int)(siHght / siUnte);
    int nbItrWdth = (int)(siWdth / siUnte);
    QVector<QPoint> vectQdrlgHrz;
 
    painter.setPen(Qt::gray);
 
    for(int i = 1; i  <= nbItrHght; i++)
        painter.drawLine (40, ((i * siUnte)+40), siWdth+40, ((i * siUnte)+40));
 
    for(int i = 1; i  <= nbItrWdth; i++)
        painter.drawLine (((i * siUnte)+40), 40, ((i * siUnte)+40), siHght+40);
 
    // Courbe 
    painter.setPen(Qt::blue);
    painter.drawPolyline( polyPoints );
 
    // Axe 
    if (extrmX > 0 && extrmY > 0)
    {
        painter.setPen(Qt::red);
        painter.drawLine ( extrmX, 80, extrmX, extrmY + 10);
        painter.drawLine ( extrmX - 10, extrmY , this->width() - 80, extrmY);
 
    }
 
 
}
Comment pourrais je calculer la surface dessinée à partir de cet exemple (http://www.casimages.com/img.php?i=1...3810854071.png)?

Merci d'avance.