~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/qgsrect.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Halasz
  • Date: 2004-12-21 09:46:27 UTC
  • Revision ID: james.westby@ubuntu.com-20041221094627-r9lb6mlz2o3yp8gn
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          qgsrect.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Sat Jun 22 2002
 
5
    copyright            : (C) 2002 by Gary E.Sherman
 
6
    email                : sherman at mrcc.com
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 /* $Id: qgsrect.cpp,v 1.21 2004/10/21 17:27:38 mcoletti Exp $ */
 
18
#include <iostream>
 
19
#include <qstring.h>
 
20
#include "qgspoint.h"
 
21
#include "qgsrect.h"
 
22
 
 
23
QgsRect::QgsRect(QgsPoint const & p1, QgsPoint const & p2)
 
24
{
 
25
  xmin = p1.x();
 
26
  xmax = p2.x();
 
27
  ymin = p1.y();
 
28
  ymax = p2.y();
 
29
  normalize();
 
30
}
 
31
 
 
32
QgsRect::QgsRect(const QgsRect &r){
 
33
  xmin = r.xMin();
 
34
  ymin = r.yMin();
 
35
  xmax = r.xMax();
 
36
  ymax = r.yMax();
 
37
}
 
38
 
 
39
void QgsRect::normalize()
 
40
{
 
41
  double temp;
 
42
  if (xmin > xmax)
 
43
    {
 
44
      temp = xmin;
 
45
      xmin = xmax;
 
46
      xmax = temp;
 
47
    }
 
48
  if (ymin > ymax)
 
49
    {
 
50
      temp = ymin;
 
51
      ymin = ymax;
 
52
      ymax = temp;
 
53
    }
 
54
}
 
55
void QgsRect::scale(double scaleFactor, QgsPoint * cp)
 
56
{
 
57
  // scale from the center
 
58
  double centerX, centerY;
 
59
  if (cp)
 
60
    {
 
61
      centerX = cp->x();
 
62
      centerY = cp->y();
 
63
  } else
 
64
    {
 
65
      centerX = xmin + width() / 2;
 
66
      centerY = ymin + height() / 2;
 
67
    }
 
68
  double newWidth = width() * scaleFactor;
 
69
  double newHeight = height() * scaleFactor;
 
70
  xmin = centerX - newWidth / 2.0;
 
71
  xmax = centerX + newWidth / 2.0;
 
72
  ymin = centerY - newHeight / 2.0;
 
73
  ymax = centerY + newHeight / 2.0;
 
74
}
 
75
void QgsRect::expand(double scaleFactor, QgsPoint * cp)
 
76
{
 
77
  // scale from the center
 
78
  double centerX, centerY;
 
79
  if (cp)
 
80
    {
 
81
      centerX = cp->x();
 
82
      centerY = cp->y();
 
83
  } else
 
84
    {
 
85
      centerX = xmin + width() / 2;
 
86
      centerY = ymin + height() / 2;
 
87
    }
 
88
 
 
89
  double newWidth = width() * scaleFactor;
 
90
  double newHeight = height() * scaleFactor;
 
91
  xmin = centerX - newWidth;
 
92
  xmax = centerX + newWidth;
 
93
  ymin = centerY - newHeight;
 
94
  ymax = centerY + newHeight;
 
95
}
 
96
 
 
97
QgsRect QgsRect::intersect(QgsRect * rect)
 
98
{
 
99
  QgsRect intersection = QgsRect();
 
100
  
 
101
  intersection.setXmin(xmin > rect->xMin()?xmin:rect->xMin());
 
102
  intersection.setXmax(xmax < rect->xMax()?xmax:rect->xMax());
 
103
  intersection.setYmin(ymin > rect->yMin()?ymin:rect->yMin());
 
104
  intersection.setYmax(ymax < rect->yMax()?ymax:rect->yMax());
 
105
  return intersection;
 
106
}
 
107
bool QgsRect::isEmpty()
 
108
{
 
109
  if (xmax <= xmin || ymax <= ymin)
 
110
    {
 
111
      return TRUE;
 
112
  } else
 
113
    {
 
114
      return FALSE;
 
115
    }
 
116
}
 
117
// Return a string representation of the rectangle with high precision
 
118
QString QgsRect::stringRep() const
 
119
{
 
120
  QString tmp;
 
121
  QString rep = tmp.sprintf("%16f %16f,%16f %16f", xmin, ymin, xmax, ymax);
 
122
  return rep;
 
123
}
 
124
 
 
125
// overloaded version of above fn to allow precision to be set
 
126
// Return a string representation of the rectangle with high precision
 
127
QString QgsRect::stringRep(int thePrecision) const
 
128
{
 
129
  
 
130
  QString rep = QString::number(xmin,'f',thePrecision) + 
 
131
                QString(",") +
 
132
                QString::number(ymin,'f',thePrecision) +
 
133
                QString(" : ") +
 
134
                QString::number(xmax,'f',thePrecision) +
 
135
                QString(",") +
 
136
                QString::number(ymax,'f',thePrecision) ;
 
137
#ifdef QGISDEBUG
 
138
  std::cout << "Extents : " << rep << std::endl;
 
139
#endif    
 
140
  return rep;
 
141
}
 
142
// Return the rectangle as a set of polygon coordinates
 
143
QString QgsRect::asPolygon() const
 
144
{
 
145
  QString tmp;
 
146
  QString rep = tmp.sprintf("%16f %16f,%16f %16f,%16f %16f,%16f %16f,%16f %16f",
 
147
    xmin, ymin, xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin);
 
148
    return rep;
 
149
    
 
150
}
 
151
bool QgsRect::operator==(const QgsRect & r1)
 
152
{
 
153
  return (r1.xMax() == this->xMax() && r1.xMin() == this->xMin() && r1.yMax() == this->yMax() && r1.yMin() == this->yMin());
 
154
}
 
155
 
 
156
QgsRect & QgsRect::operator=(const QgsRect & r)
 
157
{
 
158
  if (&r != this)
 
159
    {
 
160
      xmax = r.xMax();
 
161
      xmin = r.xMin();
 
162
      ymax = r.yMax();
 
163
      ymin = r.yMin();
 
164
    }
 
165
  return *this;
 
166
 
 
167
 
 
168
}