1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2002, OpenBeOS 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: Polygon.h 23 // Author: Marc Flerackers (mflerackers@androme.be) 24 // Description: BPolygon represents a n-sided area. 25 //------------------------------------------------------------------------------ 26 27 // Standard Includes ----------------------------------------------------------- 28 29 // System Includes ------------------------------------------------------------- 30 #include <Polygon.h> 31 32 // Project Includes ------------------------------------------------------------ 33 34 // Local Includes -------------------------------------------------------------- 35 36 // Local Defines --------------------------------------------------------------- 37 38 // Globals --------------------------------------------------------------------- 39 40 //------------------------------------------------------------------------------ 41 BPolygon::BPolygon(const BPoint *ptArray, int32 numPoints) 42 { 43 fCount = numPoints; 44 fPts = new BPoint[numPoints]; 45 memcpy(fPts, ptArray, numPoints * sizeof(BPoint)); 46 47 compute_bounds(); 48 } 49 //------------------------------------------------------------------------------ 50 BPolygon::BPolygon(const BPolygon *poly) 51 { 52 *this = *poly; 53 } 54 //------------------------------------------------------------------------------ 55 BPolygon::BPolygon () 56 : fCount(0), 57 fPts(NULL) 58 { 59 } 60 //------------------------------------------------------------------------------ 61 BPolygon::~BPolygon () 62 { 63 if (fPts) 64 delete[] fPts; 65 } 66 //------------------------------------------------------------------------------ 67 BPolygon &BPolygon::operator=(const BPolygon &from) 68 { 69 fBounds = from.fBounds; 70 fCount = from.fCount; 71 fPts = new BPoint[fCount]; 72 memcpy(fPts, from.fPts, fCount * sizeof(BPoint)); 73 74 return *this; 75 } 76 //------------------------------------------------------------------------------ 77 BRect BPolygon::Frame() const 78 { 79 return fBounds; 80 } 81 //------------------------------------------------------------------------------ 82 void BPolygon::AddPoints(const BPoint *ptArray, int32 numPoints) 83 { 84 BPoint *newPts = new BPoint[fCount + numPoints]; 85 86 if (fPts) 87 { 88 memcpy(newPts, fPts, fCount); 89 delete fPts; 90 } 91 92 memcpy(newPts + fCount, ptArray, numPoints); 93 94 fPts = newPts; 95 fCount += numPoints; 96 97 compute_bounds(); 98 } 99 //------------------------------------------------------------------------------ 100 int32 BPolygon::CountPoints() const 101 { 102 return fCount; 103 } 104 //------------------------------------------------------------------------------ 105 void BPolygon::MapTo(BRect srcRect, BRect dstRect) 106 { 107 for (int32 i = 0; i < fCount; i++) 108 map_pt(fPts + i, srcRect, dstRect); 109 map_rect(&fBounds, srcRect, dstRect); 110 } 111 //------------------------------------------------------------------------------ 112 void BPolygon::PrintToStream () const 113 { 114 for (int32 i = 0; i < fCount; i++) 115 fPts[i].PrintToStream(); 116 } 117 //------------------------------------------------------------------------------ 118 void BPolygon::compute_bounds() 119 { 120 if (fCount == 0) 121 return; 122 123 fBounds = BRect(fPts[0], fPts[0]); 124 125 for (int32 i = 1; i < fCount; i++) 126 { 127 if (fPts[i].x < fBounds.left) 128 fBounds.left = fPts[i].x; 129 if (fPts[i].y < fBounds.top) 130 fBounds.top = fPts[i].y; 131 if (fPts[i].x > fBounds.right) 132 fBounds.right = fPts[i].x; 133 if (fPts[i].y > fBounds.bottom) 134 fBounds.bottom = fPts[i].y; 135 } 136 } 137 //------------------------------------------------------------------------------ 138 void BPolygon::map_pt(BPoint *point, BRect srcRect, BRect dstRect) 139 { 140 point->x = (point->x - srcRect.left) * dstRect.Width() / srcRect.Width() 141 + dstRect.left; 142 point->y = (point->y - srcRect.top) * dstRect.Height() / srcRect.Height() 143 + dstRect.top; 144 } 145 //------------------------------------------------------------------------------ 146 void BPolygon::map_rect(BRect *rect, BRect srcRect, BRect dstRect) 147 { 148 BPoint leftTop = rect->LeftTop(); 149 BPoint bottomRight = rect->RightBottom(); 150 151 map_pt(&leftTop, srcRect, dstRect); 152 map_pt(&bottomRight, srcRect, dstRect); 153 154 *rect = BRect(leftTop, bottomRight); 155 } 156 //------------------------------------------------------------------------------ 157 158 /* 159 * $Log $ 160 * 161 * $Id $ 162 * 163 */ 164 165