Created
September 25, 2011 20:28
-
-
Save mikelikespie/1241114 to your computer and use it in GitHub Desktop.
Wrappers to overload operations with CGPoint, CGSize, and CGRect.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PGGeometry.hh | |
// PonyGrid | |
// | |
// Created by Mike Lewis on 9/23/11. | |
// Copyright (c) 2011 Square, Inc. All rights reserved. | |
// | |
#ifndef PonyGrid_PGGeometry_hh | |
#define PonyGrid_PGGeometry_hh | |
#import <math.h> | |
//#import <algorithm> | |
extern "C" { | |
#import <CoreGraphics/CGGeometry.h> | |
} | |
class PGSize; | |
class PGRect; | |
class PGPoint : public CGPoint { | |
public: | |
inline PGPoint(const CGPoint &pnt) { | |
x = pnt.x; | |
y = pnt.y; | |
} | |
inline PGPoint() : PGPoint(CGPointZero) { } | |
inline PGPoint(const CGFloat _x, const CGFloat _y) { | |
x = _x; | |
y = _y; | |
} | |
inline const PGPoint floor() const { | |
return PGPoint(::floorf(x), ::floorf(y)); | |
} | |
inline const PGPoint ceil() const { | |
return PGPoint(::ceilf(x), ::ceilf(y)); | |
} | |
inline const PGPoint round() const { | |
return PGPoint(::roundf(x), ::roundf(y)); | |
} | |
inline const PGPoint operator + (const CGSize &other) const { | |
return PGPoint(x + other.width, y + other.height); | |
} | |
inline const PGPoint operator - (const CGSize &other) const { | |
return PGPoint(x - other.width, y - other.height); | |
} | |
inline const PGSize operator - (const CGPoint &other) const; | |
inline const PGPoint operator * (const CGSize &other) const { | |
return PGPoint(x * other.width, y * other.height); | |
} | |
inline const PGPoint operator / (const CGSize &other) const { | |
return PGPoint(x / other.width, y / other.height); | |
} | |
inline const PGPoint operator * (const CGFloat &mult) const { | |
return PGPoint(x * mult, y * mult); | |
} | |
inline const PGPoint operator / (const CGFloat &denominator) const { | |
return PGPoint(x * denominator, y / denominator); | |
} | |
inline const PGPoint clamp(const PGRect &rect) const; | |
}; | |
class PGSize : public CGSize { | |
public: | |
inline PGSize(const CGSize &size) { | |
width = size.width; | |
height = size.height; | |
} | |
inline PGSize(const CGPoint &point) { | |
width = point.x; | |
height = point.y; | |
} | |
inline PGSize(const CGFloat _width, const CGFloat _height) { | |
width = _width; | |
height = _height; | |
} | |
inline PGSize() : PGSize(CGSizeZero) { } | |
inline const PGSize operator + (const CGSize &other) const { | |
return PGSize(width + other.width, height + other.height); | |
} | |
inline const PGSize operator - (const CGSize &other) const { | |
return PGSize(width - other.width, height - other.height); | |
} | |
inline const PGSize operator * (const CGFloat &mult) const { | |
return PGSize(width * mult, height * mult); | |
} | |
inline const PGSize operator / (const CGFloat &denom) const { | |
return PGSize(width / denom, height / denom); | |
} | |
inline const PGSize operator / (const CGSize &other) const { | |
return PGSize(width / other.width, height / other.height); | |
} | |
inline const PGPoint operator + (const CGPoint &other) const { | |
return PGPoint(width + other.x, height + other.y); | |
} | |
inline const PGPoint operator - (const CGPoint &other) const { | |
return PGPoint(width - other.x, height - other.y); | |
} | |
inline BOOL isZeroTol(const CGFloat &tol) const { | |
return fabsf(width) < tol && fabsf(height) < tol; | |
} | |
}; | |
class PGRect { | |
public: | |
inline PGRect () {} | |
inline PGRect(const CGSize &_size) { | |
size = _size; | |
} | |
inline PGRect(const CGPoint &_origin, const CGSize &_size) : origin(_origin), size(_size) { } | |
inline PGRect(const CGRect _other) : origin(_other.origin), size(_other.size) { } | |
inline operator CGRect () const { | |
return (CGRect){(CGPoint)origin, (CGSize)size}; | |
} | |
inline const PGPoint center() const { | |
return (origin + size) * 0.5f; | |
} | |
inline const PGPoint outerPoint() const { | |
return origin + size; | |
} | |
PGPoint origin; | |
PGSize size; | |
}; | |
const PGPoint PGPoint::clamp(const PGRect &rect) const { | |
return PGPoint(fminf(fmaxf(rect.origin.x , x), rect.outerPoint().x), | |
fminf(fmaxf(rect.origin.y , y), rect.outerPoint().y)); | |
} | |
const PGSize PGPoint::operator - (const CGPoint &other) const { | |
return PGSize(x - other.x, y - other.y); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment