Skip to content

Instantly share code, notes, and snippets.

@boredzo
Created April 22, 2012 21:34
Show Gist options
  • Save boredzo/2467067 to your computer and use it in GitHub Desktop.
Save boredzo/2467067 to your computer and use it in GitHub Desktop.
Hypothetical 2-vector class
#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
# import <UIKit/UIKit.h>
#else
# import <AppKit/AppKit.h>
#endif
@interface PRH2Vector: NSObject
@property(readonly) CGPoint pointValue;
//Defaults to when the object was created.
@property(readonly, strong) NSDate *date;
+ (id) vectorWithPoint:(CGPoint)point;
+ (id) vectorWithPoint:(CGPoint)point date:(NSDate *)date;
- (id) initWithPoint:(CGPoint)point;
- (id) initWithPoint:(CGPoint)point date:(NSDate *)date;
//Sets pointValue to the size minus the origin of the rect. Mainly useful for easily computing a rect's diagonal length (hypotenuse) by getting the vector's magnitude.
+ (id) vectorWithSizeOfRect:(CGRect)rect;
//So named because if anyone ever creates a >2-vector class, they'll want to specify which plane they want an angle on, and it would be good for this class to be consistent with its hypothetical future siblings.
- (CGFloat) angleOnXYPlaneInRadians;
- (CGFloat) angleOnXYPlaneInDegrees;
- (CGFloat) angleOnXYPlaneInFractionOfACircle;
- (CGSize) distanceFromPoint:(CGPoint)otherPoint;
- (CGSize) distanceFromVector:(PRH2Vector *)otherVector;
- (CGFloat) magnitude;
//Compute what the size/pointValue of the vector would be if this were a normalized vector (magnitude = 1). Both do the same thing; it's all a matter of type-safety/caller self-documentation.
- (CGPoint) normalizedPointValue;
- (CGSize) normalizedSize;
- (NSTimeInterval) timeIntervalSinceDate:(NSDate *)otherDate;
- (NSTimeInterval) timeIntervalSinceVector:(PRH2Vector *)otherVector;
//Wondering why a vector has a date associated with it? Wonder no more!
#if TARGET_OS_IPHONE
+ (id) vectorWithEvent:(UIEvent *)event;
- (id) initWithEvent:(UIEvent *)event;
#else
//event should be a mouse, scroll, or tablet event.
+ (id) vectorWithEvent:(NSEvent *)event;
- (id) initWithEvent:(NSEvent *)event;
#endif
- (bool) isDragFromVector:(PRH2Vector *)otherVector;
//Still wondering? See <http://developer.apple.com/legacy/mac/library/documentation/Carbon/Reference/Carbon_Event_Manager_Ref/Reference/reference.html#//apple_ref/c/func/HIMouseTrackingGetParameters> .
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment