📄 rectangl.imp
字号:
//// $Source: /home/gambit/CVS/gambit/sources/poly/rectangl.imp,v $// $Date: 2002/08/27 17:29:49 $// $Revision: 1.3 $//// DESCRIPTION:// Implementation of rectangle class//// This file is part of Gambit// Copyright (c) 2002, The Gambit Project//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//#include "base/base.h"#include "rectangl.h"//--------------------------------------------------------------------------// rectangle -- constructors and destructor//--------------------------------------------------------------------------template<class T> gRectangle<T>::gRectangle(const gRectangle<T>& given) : sides(given.sides){}template<class T> gRectangle<T>::gRectangle(const gList< gInterval<T> >& given): sides(given){}template<class T> gRectangle<T>::gRectangle(const gVector<T> lower_bd, const gVector<T> upper_bd) : sides(){ assert (lower_bd.Check(upper_bd)); for (int i = 1; i <= upper_bd.Length(); i++) { gInterval<T> side(lower_bd[i],upper_bd[i]); sides += side; }}template<class T> gRectangle<T>::~gRectangle() {}//--------------------------------------------------------------------------// rectangle -- operators//--------------------------------------------------------------------------template<class T> gRectangle<T>& gRectangle<T>::operator = (const gRectangle<T>& /* rhs */){ // gout << "For const'ness, operator = not allowed for gRectangles\n"; exit (0); return *this;}template<class T> bool gRectangle<T>::operator == (const gRectangle<T>& rhs) const{ for (int i = 1; i <= Dmnsn(); i++) if (sides[i] != rhs.sides[i]) return false; return true;}template<class T> bool gRectangle<T>::operator != (const gRectangle<T>& rhs) const{ return !(*this == rhs);}//--------------------------------------------------------------------------// interval -- information//--------------------------------------------------------------------------template<class T> const int gRectangle<T>::Dmnsn() const { return sides.Length();}template <class T> gVector<T> gRectangle<T>::LowerBound(void) const{ gVector<T> answer(Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) answer[i] = sides[i].LowerBound(); return answer;}template <class T> gVector<T> gRectangle<T>::UpperBound(void) const{ gVector<T> answer(Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) answer[i] = sides[i].UpperBound(); return answer;}template<class T> const T gRectangle<T>::LowerBoundOfCoord(const int& i) const { assert (1 <= i && i <= Dmnsn()); return sides[i].LowerBound();}template<class T> const T gRectangle<T>::UpperBoundOfCoord(const int& i) const { assert (1 <= i && i <= Dmnsn()); return sides[i].UpperBound();}template<class T> const T gRectangle<T>::HeightInCoord(const int& i) const { assert (1 <= i && i <= Dmnsn()); return sides[i].Length();}template<class T> const gInterval<T> gRectangle<T>::CartesianFactor(const int& i) const { return sides[i];}template<class T> const gRectangle<T> gRectangle<T>::SameCenterDoubleSideLengths() const { gList<gInterval<T> > new_sides; for (int i = 1; i <= Dmnsn(); i++) new_sides += CartesianFactor(i).SameCenterTwiceLength(); return gRectangle<T>(new_sides);}template<class T> const gRectangle<T> gRectangle<T>::CubeContainingCrcmscrbngSphere() const { T maxlength((T)0); T sumsquares((T)0); int i; for (i = 1; i <= Dmnsn(); i++) { sumsquares += sides[i].Length() * sides[i].Length(); if (sides[i].Length() > maxlength) maxlength = sides[i].Length(); } T diameter((T)0); while (diameter * diameter < sumsquares) diameter += maxlength; gList<gInterval<T> > new_sides; for (i = 1; i <= Dmnsn(); i++) new_sides += CartesianFactor(i).SameCenterWithNewLength(diameter); return gRectangle<T>(new_sides);}template<class T> const gRectangle<T> gRectangle<T>::Orthant(const gArray<int>& top_or_bot) const { gList<gInterval<T> > new_sides; for (int i = 1; i <= Dmnsn(); i++) if (top_or_bot[i] == 0) new_sides += CartesianFactor(i).LeftHalf(); else new_sides += CartesianFactor(i).RightHalf(); return gRectangle<T>(new_sides);}template<class T> const gVector<T> gRectangle<T>::SideLengths() const { gVector<T> answer(Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) answer[i] = sides[i].UpperBound() - sides[i].LowerBound(); return answer;}template<class T> const T gRectangle<T>::MaximalSideLength() const{ T answer((T)0); for (int i = 1; i <= Dmnsn(); i++) if (HeightInCoord(i) > answer) answer = HeightInCoord(i); return answer;}template<class T> bool gRectangle<T>::Contains(const gVector<T>& point) const { assert (point.Length() == Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) if (point[i] < sides[i].LowerBound() || sides[i].UpperBound() < point[i]) return false; return true;}template<class T> bool gRectangle<T>::Contains(const gRectangle<T>& R) const { assert (R.Dmnsn() == Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) if ( !sides[i].Contains(R.sides[i]) ) return false; return true;}template<class T> const T gRectangle<T>::Volume() const { T answer = (T)1; for (int i = 1; i <= Dmnsn(); i++) answer *= ( sides[i].UpperBound() - sides[i].LowerBound() ); return answer;}template<class T> const gVector<T> gRectangle<T>::Center() const { gVector<T> answer(Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) answer[i] = (sides[i].UpperBound() + sides[i].LowerBound()) / ((T)2); return answer;}template<class T> const gRectangle<T> gRectangle<T>::BoundingRectangle() const { return *this;}template<class T> const gList<gVector<T> > gRectangle<T>::VertexList() const { gList<gVector<T> > answer; gArray<int> ListOfTwos(Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) ListOfTwos[i] = 2; gIndexOdometer ListOfTopBottoms(ListOfTwos); while (ListOfTopBottoms.Turn()) { gVector<T> next(Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) if (ListOfTopBottoms[i] == 1) next[i] = LowerBoundOfCoord(i); else next[i] = UpperBoundOfCoord(i); answer += next; } return answer;}template<class T> const int gRectangle<T>::NumberOfCellsInSubdivision() const { int answer = 1; for (int i = 1; i <= Dmnsn(); i++) answer *= 2; return answer;}template<class T> const gRectangle<T> gRectangle<T>::SubdivisionCell(const int& index) const { int tmp = index; gArray<int> updowns(Dmnsn()); for (int i = 1; i <= Dmnsn(); i++) { if (tmp % 2 == 1) { updowns[i] = 1; tmp--; } else updowns[i] = 0; tmp /= 2; } return Orthant(updowns);}template<class T>const T gRectangle<T>::DiameterSquared() const{ T answer((T)0); for (int i = 1; i <= Dmnsn(); i++) answer += HeightInCoord(i) * HeightInCoord(i); return answer;}//----------------------------------// Conversion//----------------------------------template<class T> gRectangle<gDouble> TogDouble(const gRectangle<T>& given) { gList<gInterval<gDouble> > cartesian_factors; for (int i = 1; i <= given.Dmnsn(); i++) cartesian_factors += gInterval<gDouble>((gDouble)given.LowerBound()[i], (gDouble)given.UpperBound()[i]); return gRectangle<gDouble>(cartesian_factors);}//--------------------------------------------------------------------------// interval -- printing//--------------------------------------------------------------------------template <class T> void gRectangle<T>::Output(gOutput &output) const{ for (int i = 1; i <= Dmnsn() - 1; i++) output << sides[i] << "x"; if (Dmnsn() > 0) output << sides[Dmnsn()];}template <class T>gOutput &operator<<(gOutput &p_file, const gRectangle<T> &p_rect){ p_rect.Output(p_file); return p_file;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -