00001
00007 #include <string>
00008 #include <iostream>
00009 #include <fstream>
00010 #include "boundingBox.h"
00011
00012 BoundingBox::BoundingBox()
00013 {
00014 m_x1 = 0;
00015 m_y1 = 0;
00016 m_x2 = 0;
00017 m_y2 = 0;
00018 }
00019
00020 BoundingBox::BoundingBox(int g, int h, int lg, int ht)
00021 {
00022 SetBoundingBox(g, h, lg, ht);
00023 }
00024
00025 BoundingBox::~BoundingBox()
00026 {
00027 }
00028
00029 void BoundingBox::Affiche() const
00030 {
00031 std::cout << "Sommet 1 : " << m_x1 << " " << m_y1 << "\t";
00032 std::cout << "Sommet 2 : " << m_x2 << " " << m_y2 << std::endl;
00033 }
00034
00035 bool BoundingBox::IsVide() const
00036 {
00037 return ((m_x2 == m_x1) || (m_y2 == m_y1));
00038 }
00039
00040 void BoundingBox::SetBoundingBox(int g, int h, int lg, int ht)
00041 {
00042 m_x1 = g;
00043 m_y1 = h;
00044 m_x2 = g + lg - 1;
00045 m_y2 = h + ht - 1;
00046 }
00047
00048 int BoundingBox::GetLargeur() const
00049 {
00050 if ((m_x2 == 0) && (m_x1 == 0)) {
00051 return 0;
00052 } else {
00053 return m_x2 - m_x1 + 1;
00054 }
00055 }
00056
00057 int BoundingBox::GetHauteur() const
00058 {
00059 if ((m_y2 == 0) && (m_y1 == 0)) {
00060 return 0;
00061 } else {
00062 return m_y2 - m_y1 + 1;
00063 }
00064 }
00065
00066 void BoundingBox::Valider()
00067 {
00068
00069 if (m_x1 > m_x2)
00070 {
00071 int x = m_x1;
00072 m_x1 = m_x2;
00073 m_x2 = x;
00074 }
00075
00076 if (m_y1 > m_y2)
00077 {
00078 int y = m_y1;
00079 m_y1 = m_y2;
00080 m_y2 = y;
00081 }
00082 }
00083
00084 void BoundingBox::SetCoord(int x1, int y1, int x2, int y2)
00085 {
00086 m_x1 = x1;
00087 m_y1 = y1;
00088 m_x2 = x2;
00089 m_y2 = y2;
00090 }
00091
00092 void BoundingBox::RendCarre()
00093 {
00094
00095 if (GetLargeur() < GetHauteur()) {
00096 if (m_y1 < m_y2) {
00097 m_y2 = m_y1 + abs(m_x2 - m_x1);
00098 } else {
00099 m_y2 = m_y1 - abs(m_x2 - m_x1);
00100 }
00101 } else {
00102 if (m_x1 < m_x2) {
00103 m_x2 = m_x1 + abs(m_y2 - m_y1);
00104 } else {
00105 m_x2 = m_x1 - abs(m_y2 - m_y1);
00106 }
00107 }
00108 }
00109
00110 void BoundingBox::Deplace(int dx, int dy)
00111 {
00112 m_x1 += dx;
00113 m_y1 += dy;
00114 m_x2 += dx;
00115 m_y2 += dy;
00116 }
00117
00118 bool BoundingBox::Contient(int x, int y, bool border ) const
00119 {
00120 if (border
00121 && (x >= m_x1)
00122 && (x <= m_x2)
00123 && (y >= m_y1)
00124 && (y <= m_y2)) {
00125 return true;
00126 } else if (!border
00127 && (x > m_x1)
00128 && (x < m_x2)
00129 && (y > m_y1)
00130 && (y < m_y2)) {
00131 return true;
00132 }
00133 return false;
00134 }
00135
00136 bool BoundingBox::Contient(const BoundingBox& bb) const
00137 {
00138 return ((bb.m_x1 > m_x1)
00139 && (bb.m_x2 < m_x2)
00140 && (bb.m_y1 > m_y1)
00141 && (bb.m_y2 < m_y2));
00142 }
00143
00144 BoundingBox BoundingBox::operator|(const BoundingBox &bb) const
00145 {
00146 BoundingBox tmp;
00147 if (m_x1 < bb.m_x1) {
00148 tmp.m_x1 = m_x1;
00149 } else {
00150 tmp.m_x1 = bb.m_x1;
00151 }
00152
00153 if (m_x2 > bb.m_x2) {
00154 tmp.m_x2 = m_x2;
00155 } else {
00156 tmp.m_x2 = bb.m_x2;
00157 }
00158
00159 if (m_y1 < bb.m_y1) {
00160 tmp.m_y1 = m_y1;
00161 } else {
00162 tmp.m_y1 = bb.m_y1;
00163 }
00164
00165 if (m_y2 > bb.m_y2) {
00166 tmp.m_y2 = m_y2;
00167 } else {
00168 tmp.m_y2 = bb.m_y2;
00169 }
00170 return tmp;
00171 }
00172
00173 BoundingBox& BoundingBox::operator|=(const BoundingBox &bb)
00174 {
00175 *this = *this | bb;
00176 return *this;
00177 }
00178
00179 bool BoundingBox::operator==(const BoundingBox& bb)
00180 {
00181 return ((m_x1 == bb.m_x1)
00182 && (m_y1 == bb.m_y1)
00183 && (m_y1 == bb.m_y1)
00184 && (m_y2 == bb.m_y2));
00185 }
00186
00187 BoundingBox& BoundingBox::operator=(const BoundingBox& bb)
00188 {
00189 m_x1 = bb.m_x1;
00190 m_x2 = bb.m_x2;
00191 m_y1 = bb.m_y1;
00192 m_y2 = bb.m_y2;
00193 return *this;
00194 }
00195
00196 int BoundingBox::GetOrigineX() const {
00197 return m_x1;
00198 }
00199
00200 int BoundingBox::GetOrigineY() const {
00201 return m_y1;
00202 }
00203
00204 int BoundingBox::GetFinX() const {
00205 return m_x2;
00206 }
00207
00208 int BoundingBox::GetFinY() const {
00209 return m_y2;
00210 }
00211
00212 void BoundingBox::SetOrigineX(int pos) {
00213 m_x1 = pos;
00214 }
00215
00216 void BoundingBox::SetOrigineY(int pos) {
00217 m_y1 = pos;
00218 }
00219
00220 void BoundingBox::SetFinX(int pos) {
00221 m_x2 = pos;
00222 }
00223
00224 void BoundingBox::SetFinY(int pos) {
00225 m_y2 = pos;
00226 }