1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2004, Haiku 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 // DEALINGS IN THE SOFTWARE. 21 // 22 // File Name: Point.cpp 23 // Author: Frans van Nispen 24 // Description: BPoint represents a single x,y coordinate. 25 //------------------------------------------------------------------------------ 26 27 // Standard Includes ----------------------------------------------------------- 28 #include <math.h> 29 #include <stdio.h> 30 31 // System Includes ------------------------------------------------------------- 32 #include <SupportDefs.h> 33 #include <Point.h> 34 #include <Rect.h> 35 36 37 const BPoint B_ORIGIN(0, 0); 38 39 40 void 41 BPoint::ConstrainTo(BRect r) 42 { 43 x = max_c(min_c(x, r.right), r.left); 44 y = max_c(min_c(y, r.bottom), r.top); 45 } 46 47 48 void 49 BPoint::PrintToStream() const 50 { 51 printf("BPoint(x:%.0f, y:%.0f)\n", x, y); 52 } 53 54 55 BPoint 56 BPoint::operator+(const BPoint& p) const 57 { 58 return BPoint(x + p.x, y + p.y); 59 } 60 61 62 BPoint 63 BPoint::operator-(const BPoint& p) const 64 { 65 return BPoint(x - p.x, y - p.y); 66 } 67 68 69 BPoint & 70 BPoint::operator+=(const BPoint& p) 71 { 72 x += p.x; 73 y += p.y; 74 75 return *this; 76 } 77 78 79 BPoint & 80 BPoint::operator-=(const BPoint& p) 81 { 82 x -= p.x; 83 y -= p.y; 84 85 return *this; 86 } 87 88 89 bool 90 BPoint::operator!=(const BPoint& p) const 91 { 92 return x != p.x || y != p.y; 93 } 94 95 96 bool 97 BPoint::operator==(const BPoint& p) const 98 { 99 return x == p.x && y == p.y; 100 } 101 102