1 /*
2     Scan Tailor - Interactive post-processing tool for scanned pages.
3     Copyright (C)  Joseph Artsimovich <joseph.artsimovich@gmail.com>
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "GridLineTraverser.h"
20 #include "LineIntersectionScalar.h"
21 
GridLineTraverser(const QLineF & line)22 GridLineTraverser::GridLineTraverser(const QLineF& line) {
23   const QPoint p1(line.p1().toPoint());
24   const QPoint p2(line.p2().toPoint());
25   int h_spans, v_spans, num_spans;
26   double s1 = 0.0, s2 = 0.0;
27   if ((h_spans = std::abs(p1.x() - p2.x())) > (v_spans = std::abs(p1.y() - p2.y()))) {
28     // Major direction: horizontal.
29     num_spans = h_spans;
30     lineIntersectionScalar(line, QLineF(p1, QPoint(p1.x(), p1.y() + 1)), s1);
31     lineIntersectionScalar(line, QLineF(p2, QPoint(p2.x(), p2.y() + 1)), s2);
32   } else {
33     // Major direction: vertical.
34     num_spans = v_spans;
35     lineIntersectionScalar(line, QLineF(p1, QPoint(p1.x() + 1, p1.y())), s1);
36     lineIntersectionScalar(line, QLineF(p2, QPoint(p2.x() + 1, p2.y())), s2);
37   }
38 
39   m_dt = num_spans == 0 ? 0 : 1.0 / num_spans;
40   m_line.setP1(line.pointAt(s1));
41   m_line.setP2(line.pointAt(s2));
42   m_totalStops = num_spans + 1;
43   m_stopsDone = 0;
44 }
45 
next()46 QPoint GridLineTraverser::next() {
47   const QPointF pt(m_line.pointAt(m_stopsDone * m_dt));
48   ++m_stopsDone;
49 
50   return pt.toPoint();
51 }
52