Created
October 6, 2011 07:51
-
-
Save willbailey/1266786 to your computer and use it in GitHub Desktop.
WBVector
This file contains hidden or 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
@interface WBVector : NSObject <NSCopying> { | |
CGFloat _x; | |
CGFloat _y; | |
CGFloat _z; | |
} | |
@property (nonatomic) CGFloat x; | |
@property (nonatomic) CGFloat y; | |
@property (nonatomic) CGFloat z; | |
+ (WBVector *)vectorWithX:(CGFloat)x; | |
+ (WBVector *)vectorWithX:(CGFloat)x Y:(CGFloat)y; | |
+ (WBVector *)vectorWithX:(CGFloat)x Y:(CGFloat)y Z:(CGFloat)z; | |
+ (CGFloat)distanceFromVector:(WBVector *)v1 vector:(WBVector *)v2; | |
- (WBVector *)initWithX:(CGFloat)x; | |
- (WBVector *)initWithX:(CGFloat)x Y:(CGFloat)y; | |
- (WBVector *)initWithX:(CGFloat)x Y:(CGFloat)y Z:(CGFloat)z; | |
- (void)add:(WBVector *)v; | |
- (void)sub:(WBVector *)v; | |
- (void)mult:(id)v; | |
- (void)multByFloat:(CGFloat)f; | |
- (void)div:(id)v; | |
- (void)divByFloat:(CGFloat)f; | |
- (void)normalize; | |
- (void)limit:(CGFloat)max; | |
- (CGFloat)dist:(WBVector *)v; | |
- (CGFloat)mag; | |
@end |
This file contains hidden or 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
#import "WBVector.h" | |
@implementation WBVector | |
@synthesize x = _x, y = _y, z = _z; | |
+ (WBVector *)vectorWithX:(CGFloat)x { | |
return [[[WBVector alloc] initWithX:x Y:0 Z:0] autorelease]; | |
} | |
+ (WBVector *)vectorWithX:(CGFloat)x Y:(CGFloat)y { | |
return [[[WBVector alloc] initWithX:x Y:y Z:0] autorelease]; | |
} | |
+ (WBVector *)vectorWithX:(CGFloat)x Y:(CGFloat)y Z:(CGFloat)z { | |
return [[[WBVector alloc] initWithX:x Y:y Z:z] autorelease]; | |
} | |
+ (CGFloat)distanceFromVector:(WBVector *)v1 vector:(WBVector *)v2 { | |
return [v1 dist:v2]; | |
} | |
- (WBVector *)initWithX:(CGFloat)x Y:(CGFloat)y Z:(CGFloat)z { | |
_x = x; | |
_y = y; | |
_z = z; | |
return self; | |
} | |
- (WBVector *)initWithX:(CGFloat)x Y:(CGFloat)y { | |
return [self initWithX:x Y:y Z:0]; | |
} | |
- (WBVector *)initWithX:(CGFloat)x { | |
return [self initWithX:x Y:0 Z:0]; | |
} | |
- (id)copyWithZone:(NSZone *)zone { | |
return [[[WBVector alloc] initWithX:_x Y:_y Z:_z] autorelease]; | |
} | |
// add the passed in vector to the vector | |
- (void)add:(WBVector *)v { | |
_x += v.x; | |
_y += v.y; | |
_z += v.z; | |
}; | |
// subtract the passed in vector from the vector | |
- (void)sub:(WBVector *)v { | |
_x -= v.x; | |
_y -= v.y; | |
_z -= v.z; | |
}; | |
// multiply the vector by the passed in vector | |
- (void)mult:(WBVector *)v { | |
_x *= v.x; | |
_y *= v.y; | |
_z *= v.z; | |
}; | |
- (void)multByFloat:(CGFloat)f { | |
[self mult:[WBVector vectorWithX:f Y:f Z:f]]; | |
} | |
// divide the vector by the passed in vector | |
- (void)div:(WBVector *)v { | |
_x /= v.x; | |
_y /= v.y; | |
_z /= v.z; | |
}; | |
// divide the vector by the passed in float | |
- (void)divByFloat:(CGFloat)f { | |
[self div:[WBVector vectorWithX:f Y:f Z:f]]; | |
} | |
// calculate the distance between two vectors | |
- (CGFloat)dist:(WBVector *)v { | |
CGFloat dx = v.x - _x; | |
CGFloat dy = v.y - _y; | |
CGFloat dz = v.z - _z; | |
return sqrt(dx*dx + dy*dy + dz*dz); | |
}; | |
// limit the magnitude of the vector | |
- (void)limit:(CGFloat)max { | |
if ([self mag] > max) { | |
[self normalize]; | |
[self multByFloat:max]; | |
} | |
}; | |
// calculate the magnitude (distance from the origin) of the vector | |
- (CGFloat)mag { | |
return sqrt(_x*_x + _y*_y + _z*_z); | |
}; | |
// normalize the vector (convert to a unit vector) | |
- (void)normalize { | |
CGFloat mag = [self mag]; | |
[self divByFloat:mag]; | |
}; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment