home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 11.5 KB | 397 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWPoly.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWPOLY_H
- #include "FWPoly.h"
- #endif
-
- #ifndef FWPOINT_H
- #include "FWPoint.h"
- #endif
-
- #ifndef FWRECT_H
- #include "FWRect.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- #ifndef FWMEMORY_H
- #include "FWMemory.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _TRANSFORM_
- #include <Trnsform.xh>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphics_Polygon
- #endif
-
- FW_DEFINE_CLASS_M1(FW_PPolygon, FW_CGraphicCountedPtr)
- FW_DEFINE_CLASS_M1(FW_CPolygonRep, FW_CGraphicCountedPtrRep)
-
- FW_REGISTER_ARCHIVABLE_CLASS(FW_LPolygonRep, FW_CPolygonRep, FW_CPolygonRep::Read, FW_CGraphicCountedPtrRep::Write)
-
- //========================================================================================
- // class FW_PPolygon
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_PPolygon::FW_PPolygon
- //----------------------------------------------------------------------------------------
-
- FW_PPolygon::FW_PPolygon() :
- FW_CGraphicCountedPtr()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPolygon::FW_PPolygon
- //----------------------------------------------------------------------------------------
-
- FW_PPolygon::FW_PPolygon(FW_CReadableStream& archive) :
- FW_CGraphicCountedPtr()
- {
- SetRep(new FW_CPolygonRep(archive));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPolygon::FW_PPolygon
- //----------------------------------------------------------------------------------------
-
- FW_PPolygon::FW_PPolygon(unsigned long count, const FW_CPoint* points) :
- FW_CGraphicCountedPtr()
- {
- SetRep(new FW_CPolygonRep(count, points));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPolygon::FW_PPolygon
- //----------------------------------------------------------------------------------------
-
- FW_PPolygon::FW_PPolygon(const FW_PPolygon& other) :
- FW_CGraphicCountedPtr(other)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPolygon::FW_PPolygon
- //----------------------------------------------------------------------------------------
-
- FW_PPolygon::~FW_PPolygon()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPolygon::operator=
- //----------------------------------------------------------------------------------------
-
- FW_PPolygon& FW_PPolygon::operator=(const FW_PPolygon& other)
- {
- // We don't need to test this == &other because SetRep will do it
- SetRep(other.GetRep());
- return *this;
- }
-
- //========================================================================================
- // class FW_CPolygonRep
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::FW_CPolygonRep
- //----------------------------------------------------------------------------------------
-
- FW_CPolygonRep::FW_CPolygonRep(unsigned long count, const FW_CPoint* points):
- fCount(count),
- fPoints(NULL)
- {
- FW_ASSERT(fCount != 0);
- FW_ASSERT(points != NULL);
-
- unsigned long memorySize = count * sizeof(FW_CPoint);
- fPoints = (FW_CPoint*)FW_CMemoryManager::AllocateBlock(memorySize);
-
- FW_CMemoryManager::CopyMemory(points, fPoints, memorySize);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::FW_CPolygonRep
- //----------------------------------------------------------------------------------------
-
- FW_CPolygonRep::FW_CPolygonRep(FW_CReadableStream& archive):
- fCount(0),
- fPoints(NULL)
- {
- archive >> fCount;
-
- unsigned long memorySize = fCount * sizeof(FW_CPoint);
- fPoints = (FW_CPoint*)FW_CMemoryManager::AllocateBlock(memorySize);
-
- for (unsigned long i = 0; i< fCount; i++)
- archive >> fPoints[i];
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::~FW_CPolygonRep
- //----------------------------------------------------------------------------------------
-
- FW_CPolygonRep::~FW_CPolygonRep()
- {
- FW_CMemoryManager::FreeBlock(fPoints); // FreeBlock tests for NULL
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::Transform
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::Transform(Environment *ev, ODTransform* transform)
- {
- for(unsigned long i = 0; i < fCount; i++)
- fPoints[i].Transform(ev, transform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::InverseTransform
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::InverseTransform(Environment *ev, ODTransform* transform)
- {
- for(unsigned long i = 0; i < fCount; i++)
- fPoints[i].InverseTransform(ev, transform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::Move
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::Move(FW_CFixed deltaX, FW_CFixed deltaY)
- {
- for(unsigned long i = 0; i < fCount; i++)
- {
- fPoints[i].x += deltaX;
- fPoints[i].y += deltaY;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::MoveTo
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::MoveTo(FW_CFixed x, FW_CFixed y)
- {
- FW_CRect bounds;
- GetBounds(bounds);
-
- FW_CFixed deltaX = x - bounds.left;
- FW_CFixed deltaY = y - bounds.top;
-
- Move(deltaX, deltaY);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::Inset
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::Inset(FW_CFixed x, FW_CFixed y)
- {
- // Find the geometric center of the polygon
- FW_CWide xCenterWide = FW_IntToWide(0);
- FW_CWide yCenterWide = xCenterWide;
-
- unsigned long i;
- for(i = 0; i < fCount; i ++)
- {
- xCenterWide += FW_CWide(fPoints[i].x);
- yCenterWide += FW_CWide(fPoints[i].y);
- }
-
- FW_CFixed xCenter = xCenterWide / FW_IntToFixed(fCount);
- FW_CFixed yCenter = yCenterWide / FW_IntToFixed(fCount);
-
- // Now offset all points
- for(i = 0; i < fCount; i ++)
- {
- if(fPoints[i].x < xCenter)
- {
- // Offset the coordinate
- fPoints[i].x += x;
-
- // Check for carryover
- if(fPoints[i].x > xCenter)
- fPoints[i].x = xCenter;
- }
- else if(fPoints[i].x > xCenter)
- {
- // Offset the coordinate
- fPoints[i].x -= x;
-
- // Check for carryover
- if(fPoints[i].x < xCenter)
- fPoints[i].x = xCenter;
- }
-
- if(fPoints[i].y < yCenter)
- {
- // Offset the coordinate
- fPoints[i].y += y;
-
- // Check for carryover
- if(fPoints[i].y > yCenter)
- fPoints[i].y = yCenter;
- }
- else if(fPoints[i].y > yCenter)
- {
- // Offset the coordinate
- fPoints[i].y -= y;
-
- // Check for carryover
- if(fPoints[i].y < yCenter)
- fPoints[i].y = yCenter;
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::GetBounds
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::GetBounds(FW_CRect& rect) const
- {
- rect.TopLeft() = fPoints[0];
- rect.BotRight() = fPoints[0];
-
- for(unsigned 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_CPolygonRep::SetPoints
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::SetPoints(const FW_CPoint* points,
- unsigned long startIndex,
- unsigned long pointCount)
- {
- FW_ASSERT(startIndex + pointCount <= fCount);
- FW_CMemoryManager::CopyMemory(points, fPoints + startIndex, pointCount * sizeof(FW_CPoint));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::GetPoints
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::GetPoints(FW_CPoint* points,
- unsigned long startIndex,
- unsigned long pointCount) const
- {
- FW_ASSERT(startIndex + pointCount <= fCount);
- FW_CMemoryManager::CopyMemory(fPoints + startIndex, points, pointCount * sizeof(FW_CPoint));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::GetCount
- //----------------------------------------------------------------------------------------
-
- unsigned long FW_CPolygonRep::GetCount() const
- {
- return fCount;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::GetPoints
- //----------------------------------------------------------------------------------------
-
- const FW_CPoint* FW_CPolygonRep::GetPoints() const
- {
- return fPoints;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CPolygonRep::Flatten(FW_CWritableStream& archive) const
- {
- archive << fCount;
- for (unsigned long i = 0; i< fCount; i++)
- archive << fPoints[i];
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::Copy
- //----------------------------------------------------------------------------------------
-
- FW_PPolygon FW_CPolygonRep::Copy() const
- {
- return FW_PPolygon(fCount, fPoints);;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::IsEqual
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPolygonRep::IsEqual(const FW_CGraphicCountedPtrRep* other) const
- {
- if (other == this)
- return TRUE;
-
- FW_CPolygonRep* polygonRep = FW_DYNAMIC_CAST(FW_CPolygonRep, other);
- if (polygonRep != NULL)
- {
- if(fCount == polygonRep->fCount)
- {
- for (unsigned long i = 0; i< fCount; i++)
- {
- if (polygonRep->fPoints[i] != fPoints[i])
- return FALSE;
- }
- return TRUE;
- }
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPolygonRep::Read
- //----------------------------------------------------------------------------------------
-
- void* FW_CPolygonRep::Read(FW_CReadableStream& archive)
- {
- FW_CPolygonRep* objectRep = new FW_CPolygonRep(archive);
- return objectRep;
- }
-
-