home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 9.1 KB | 328 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: PRPoly.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef PRPOLY_H
- #include "PRPoly.h"
- #endif
-
- #ifndef FWODEXCE_H
- #include "FWODExce.h"
- #endif
-
- #ifndef FWPOINT_H
- #include "FWPoint.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef FWSTRMRW_H
- #include "FWStrmRW.h"
- #endif
-
- #ifndef FWEXCEPT_H
- #include "FWExcept.h"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphics_Polygon
- #endif
-
- FW_DEFINE_AUTO(FW_CPrivPolygonRep)
-
- //========================================================================================
- // class FW_CPrivPolygonRep
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::FW_CPrivPolygonRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivPolygonRep::FW_CPrivPolygonRep(long count, const FW_SPoint* points) :
- fCount(count),
- fPoints(NULL),
- fValidBounds(FALSE)
- {
- FW_ASSERT(fCount != 0);
- FW_ASSERT(points != NULL);
-
- long memorySize = count * sizeof(FW_SPoint);
- fPoints = (FW_SPoint*)FW_CMemoryManager::AllocateBlock(memorySize);
-
- FW_CMemoryManager::CopyMemory(points, fPoints, memorySize);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::FW_CPrivPolygonRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivPolygonRep::FW_CPrivPolygonRep(FW_CReadableStream& stream) :
- fCount(0),
- fPoints(NULL),
- fValidBounds(FALSE)
- {
- stream >> fCount;
-
- long memorySize = fCount * sizeof(FW_SPoint);
- fPoints = (FW_SPoint*) FW_CMemoryManager::AllocateBlock(memorySize);
-
- for (long i = 0; i < fCount; i++)
- stream >> fPoints[i];
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::FW_CPrivPolygonRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivPolygonRep::FW_CPrivPolygonRep(const FW_CPrivPolygonRep& poly) :
- fCount(poly.GetCount()),
- fPoints(NULL),
- fValidBounds(FALSE)
- {
- long memorySize = fCount * sizeof(FW_SPoint);
- fPoints = (FW_SPoint*) FW_CMemoryManager::AllocateBlock(memorySize);
-
- FW_CMemoryManager::CopyMemory(poly.GetPoints(), fPoints, memorySize);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::~FW_CPrivPolygonRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivPolygonRep::~FW_CPrivPolygonRep()
- {
- FW_START_DESTRUCTOR
- FW_CMemoryManager::FreeBlock(fPoints); // FreeBlock tests for NULL
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::Transform
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::Transform(Environment *ev, ODTransform* transform)
- {
- FW_TransformPoints(ev, transform, fPoints, fCount);
- fValidBounds = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::InverseTransform
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::InverseTransform(Environment *ev, ODTransform* transform)
- {
- FW_InverseTransformPoints(ev, transform, fPoints, fCount);
- fValidBounds = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::Move
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::Move(FW_Fixed deltaX, FW_Fixed deltaY)
- {
- for(long i = 0; i < fCount; i++)
- {
- fPoints[i].x += deltaX;
- fPoints[i].y += deltaY;
- }
- fValidBounds = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::MoveTo
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::MoveTo(FW_Fixed x, FW_Fixed y)
- {
- FW_SRect bounds;
- GetBounds(bounds);
-
- Move(x - bounds.left, y - bounds.top);
-
- fValidBounds = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::Inset
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::Inset(FW_Fixed x, FW_Fixed y)
- {
- // Find the geometric center of the polygon
- FW_Wide xCenterWide = FW_IntToWide(0);
- FW_Wide yCenterWide = xCenterWide;
-
- long i;
- for(i = 0; i < fCount; i ++)
- {
- xCenterWide += FW_FixedToWide(fPoints[i].x);
- yCenterWide += FW_FixedToWide(fPoints[i].y);
- }
-
- FW_Fixed xCenter = xCenterWide / FW_IntToFixed(fCount);
- FW_Fixed yCenter = yCenterWide / FW_IntToFixed(fCount);
-
- // Now offset all points
- for(i = 0; i < fCount; i ++)
- {
- if(fPoints[i].x < xCenter)
- {
- if((fPoints[i].x += x) > xCenter)
- fPoints[i].x = xCenter;
- }
- else if(fPoints[i].x > xCenter)
- {
- if((fPoints[i].x -= x) < xCenter)
- fPoints[i].x = xCenter;
- }
-
- if(fPoints[i].y < yCenter)
- {
- if((fPoints[i].y += y) > yCenter)
- fPoints[i].y = yCenter;
- }
- else if(fPoints[i].y > yCenter)
- {
- if((fPoints[i].y -= y) < yCenter)
- fPoints[i].y = yCenter;
- }
- }
- fValidBounds = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::GetBounds
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::GetBounds(FW_SRect& rect) const
- {
- if (fValidBounds)
- {
- rect = fBounds;
- }
- else
- {
- rect.left =
- rect.right = fPoints[0].x;
- rect.top =
- rect.bottom = fPoints[0].y;
-
- for(long i = 1; i < fCount; i ++)
- {
- if(fPoints[i].x < rect.left)
- rect.left = fPoints[i].x;
- else if(fPoints[i].x > rect.right)
- rect.right = fPoints[i].x;
-
- if(fPoints[i].y < rect.top)
- rect.top = fPoints[i].y;
- else if(fPoints[i].y > rect.bottom)
- rect.bottom = fPoints[i].y;
- }
-
- ((FW_CPrivPolygonRep*)this)->fBounds = rect;
- ((FW_CPrivPolygonRep*)this)->fValidBounds = TRUE;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::SetPoints
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::SetPoints(const FW_SPoint* points,
- long startIndex,
- long pointCount)
- {
- FW_ASSERT(startIndex + pointCount <= fCount);
- FW_CMemoryManager::CopyMemory(points, fPoints + startIndex, pointCount * sizeof(FW_CPoint));
- fValidBounds = FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::GetPoints
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::GetPoints(FW_SPoint* points,
- long startIndex,
- long pointCount) const
- {
- FW_ASSERT(startIndex + pointCount <= fCount);
- FW_CMemoryManager::CopyMemory(fPoints + startIndex, points, pointCount * sizeof(FW_CPoint));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::GetCount
- //----------------------------------------------------------------------------------------
-
- long FW_CPrivPolygonRep::GetCount() const
- {
- return fCount;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::GetPoints
- //----------------------------------------------------------------------------------------
-
- FW_SPoint* FW_CPrivPolygonRep::GetPoints() const
- {
- ((FW_CPrivPolygonRep*)this)->fValidBounds = FALSE; // I don't what what they are going to do with those points
- return fPoints;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivPolygonRep::Write(FW_CWritableStream& stream) const
- {
- stream << fCount;
- for (long i = 0; i< fCount; i++)
- stream << fPoints[i];
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivPolygonRep::IsEqual
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivPolygonRep::IsEqual(const FW_CPrivPolygonRep* other) const
- {
- if (other == this)
- return TRUE;
-
- if(fCount == other->fCount)
- {
- for (long i = 0; i< fCount; i++)
- {
- if (other->fPoints[i] != fPoints[i])
- return FALSE;
- }
- return TRUE;
- }
-
- return FALSE;
- }
-