[Lemon-commits] [lemon_svn] athos: r338 - hugo/trunk/src/work/athos/xy
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:38:51 CET 2006
Author: athos
Date: Tue Mar 23 18:28:47 2004
New Revision: 338
Added:
hugo/trunk/src/work/athos/xy/boundingbox.cc
hugo/trunk/src/work/athos/xy/boundingbox.h
Modified:
hugo/trunk/src/work/athos/xy/xy.cc
hugo/trunk/src/work/athos/xy/xy.h
Log:
Elkészült a boundingbox osztály (boundingbox.h) és hozzá a tesztprogi.
Added: hugo/trunk/src/work/athos/xy/boundingbox.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/xy/boundingbox.cc Tue Mar 23 18:28:47 2004
@@ -0,0 +1,61 @@
+#include <boundingbox.h>
+
+#include <iostream>
+using namespace std;
+using namespace hugo;
+int main()
+{
+ xy<int> z;
+ cout << "A teszt a következõ: elõször beolvasunk 2 síkvektort, elkészítjük hozzá a határoló dobozt. Azután további síkvektorokat olvasunk be, amiket egy másik bounding boxhoz adogatunk hozzá. Mindig kiírjuk, hogy a megadott új pont benne volt e, majd hozzáadás után a doboz aktuális állapotát. Ezt a második beolvasást CTRL-D-vel lehet megszakítani: ezután a két dobozt összeadjuk." << endl;
+
+ cout << "Kerek elõször 2 sikvektort (az elsõ dobozhoz)." << endl;
+ BoundingBox<int> doboz1;
+ cin >> z;
+ doboz1 += z;
+ cin >> z;
+ doboz1 += z;
+ cout << "Az elsõ határoló doboz aktualisan: " << endl;
+ cout << "Bal alsó sarok: " << doboz1.bottomLeft() << endl;
+ cout << "Jobb felsõ sarok: " << doboz1.topRight() << endl;
+
+
+
+ cout << "Kerek sok sikvektort." << endl;
+
+ BoundingBox<int> doboz;
+
+ vector< xy<int> > v;
+
+ while(cin >> z) {
+ v.push_back(z);
+ if (doboz.inside(z)){
+ cout << "Ez most belül van." << endl;
+ }
+ else{
+ cout << "Ez most kívül van." << endl;
+ }
+
+ doboz += z;
+ cout << "A második határoló doboz aktualisan: " << endl;
+ cout << "Bal alsó sarok: " << doboz.bottomLeft() << endl;
+ cout << "Jobb felsõ sarok: " << doboz.topRight() << endl;
+ }
+
+ doboz += doboz1;
+ cout << "A két doboz összege: " << endl;
+ cout << "Bal alsó sarok: " << doboz.bottomLeft() << endl;
+ cout << "Jobb felsõ sarok: " << doboz.topRight() << endl;
+
+ /*
+ cout << "A kovetkezo szamokat szoroztam ossze:" << endl;
+ for(unsigned int i=0; i<v.size(); ++i) {
+ cout << v[i] << ", A normanégyzete: " << v[i].normSquare() <<endl;
+ cout << v[i] << " " << s << " szorosa " << v[i]*s <<endl;
+ cout << v[i] << " " << s << " edrésze " << v[i]/s <<endl;
+ }
+ if (v.size()>1){
+ cout << "Az elsö kettö szorzata: " << v[0]*v[1] << endl;
+ }
+ */
+ cout << "Eleg nehez volt." << endl;
+}
Added: hugo/trunk/src/work/athos/xy/boundingbox.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/xy/boundingbox.h Tue Mar 23 18:28:47 2004
@@ -0,0 +1,92 @@
+// -*- c++ -*-
+/**
+Implementation of a bounding box of plainvectors.
+
+*/
+#ifndef HUGO_BOUNDINGBOX_H
+#define HUGO_BOUNDINGBOX_H
+
+
+#include <xy.h>
+
+namespace hugo {
+
+ template<typename T>
+ class BoundingBox {
+ xy<T> bottomleft, topright;
+ bool _empty;
+ public:
+
+ ///Default constructor: an empty bounding box
+ BoundingBox() { _empty = true; }
+
+ ///Constructing the instance from one point
+ BoundingBox(xy<T> a) { bottomleft=topright=a; _empty = false; }
+
+ ///Is there any point added
+ bool empty() const {
+ return _empty;
+ }
+
+ ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
+ xy<T> bottomLeft() const {
+ return bottomleft;
+ };
+
+ ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
+ xy<T> topRight() const {
+ return topright;
+ };
+
+ ///Checks whether a point is inside a bounding box
+ bool inside(const xy<T>& u){
+ if (_empty)
+ return false;
+ else{
+ return ((u.x-bottomleft.x)*(topright.x-u.x) >= 0 &&
+ (u.y-bottomleft.y)*(topright.y-u.y) >= 0 );
+ }
+ }
+
+ ///Increments a bounding box with a point
+ BoundingBox& operator +=(const xy<T>& u){
+ if (_empty){
+ bottomleft=topright=u;
+ _empty = false;
+ }
+ else{
+ if (bottomleft.x > u.x) bottomleft.x = u.x;
+ if (bottomleft.y > u.y) bottomleft.y = u.y;
+ if (topright.x < u.x) topright.x = u.x;
+ if (topright.y < u.y) topright.y = u.y;
+ }
+ return *this;
+ };
+
+ ///Sums a bounding box and a point
+ BoundingBox operator +(const xy<T>& u){
+ BoundingBox b = *this;
+ return b += u;
+ };
+
+ ///Increments a bounding box with an other bounding box
+ BoundingBox& operator +=(const BoundingBox &u){
+ if ( !u.empty() ){
+ *this += u.bottomLeft();
+ *this += u.topRight();
+ }
+ return *this;
+ };
+
+ ///Sums two bounding boxes
+ BoundingBox operator +(const BoundingBox& u){
+ BoundingBox b = *this;
+ return b += u;
+ };
+
+ };//class BoundingBox
+
+
+} //namespace hugo
+
+#endif //HUGO_BOUNDINGBOX_H
Modified: hugo/trunk/src/work/athos/xy/xy.cc
==============================================================================
--- hugo/trunk/src/work/athos/xy/xy.cc (original)
+++ hugo/trunk/src/work/athos/xy/xy.cc Tue Mar 23 18:28:47 2004
@@ -5,7 +5,7 @@
int main()
{
- cout << "Még egy skalárt is kérek (szépen)!" << endl;
+ cout << "Kérek szépen egy egész számot!" << endl;
int s;
cin >> s;
Modified: hugo/trunk/src/work/athos/xy/xy.h
==============================================================================
--- hugo/trunk/src/work/athos/xy/xy.h (original)
+++ hugo/trunk/src/work/athos/xy/xy.h Tue Mar 23 18:28:47 2004
@@ -12,62 +12,54 @@
template<typename T>
class xy {
- T _x,_y;
public:
+
+ T x,y;
///Default constructor: both coordinates become 0
- xy() : _x(0), _y(0 ){ /*_x=_y=0;*/ }
+ xy() : x(0), y(0) {}
- ///Constructing from coordinates
- xy(T a, T b) : _x(a), _x(b) { /*_x=a; _y=b;*/ }
+ ///Constructing the instance from coordinates
+ xy(T a, T b) : x(a), y(a) { }
- ///Gives back the x coordinate
- T x(){
- return _x;
- };
-
- ///Gives back the y coordinate
- T y(){
- return _y;
- };
///Gives back the square of the norm of the vector
T normSquare(){
- return _x*_x+_y*_y;
+ return x*x+y*y;
};
///Increments the left hand side by u
xy<T>& operator +=(const xy<T>& u){
- _x += u._x;
- _y += u._y;
+ x += u.x;
+ y += u.y;
return *this;
};
///Decrements the left hand side by u
xy<T>& operator -=(const xy<T>& u){
- _x -= u._x;
- _y -= u._y;
+ x -= u.x;
+ y -= u.y;
return *this;
};
///Multiplying the left hand side with a scalar
xy<T>& operator *=(const T &u){
- _x *= u;
- _y *= u;
+ x *= u;
+ y *= u;
return *this;
};
///Dividing the left hand side by a scalar
xy<T>& operator /=(const T &u){
- _x /= u;
- _y /= u;
+ x /= u;
+ y /= u;
return *this;
};
///Returns the scalar product of two vectors
T operator *(const xy<T>& u){
- return _x*u._x+_y*u._y;
+ return x*u.x+y*u.y;
};
///Returns the sum of two vectors
@@ -96,12 +88,12 @@
///Testing equality
bool operator==(const xy<T> &u){
- return (_x==u._x) && (_y==u._y);
+ return (x==u.x) && (y==u.y);
};
///Testing inequality
bool operator!=(xy u){
- return (_x!=u._x) || (_y!=u._y);
+ return (x!=u.x) || (y!=u.y);
};
};
@@ -111,11 +103,8 @@
inline
std::istream& operator>>(std::istream &is, xy<T> &z)
{
- ///This is not the best solution here: I didn't know how to solve this with friend functions
- T a,b;
- is >> a >> b;
- xy<T> buf(a,b);
- z=buf;
+
+ is >> z.x >> z.y;
return is;
}
@@ -124,7 +113,7 @@
inline
std::ostream& operator<<(std::ostream &os, xy<T> z)
{
- os << "(" << z.x() << ", " << z.y() << ")";
+ os << "(" << z.x << ", " << z.y << ")";
return os;
}
More information about the Lemon-commits
mailing list