-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.m
More file actions
52 lines (45 loc) · 1.05 KB
/
Copy pathvector.m
File metadata and controls
52 lines (45 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#import "vector.h"
#include <math.h>
@implementation jsVector
@synthesize x=_x;
@synthesize y=_y;
- (id) initWithX: (float) x Y: (float) y{
self = [super init];
if(self){
self.x = x;
self.y = y;
}
return self;
}
+(id) vectorWithX: (float) x Y:(float) y{
return [[jsVector alloc] initWithX: x Y: y];
}
+(id) vectorWithMag: (float) mag angle:(float) ang{
return [[jsVector alloc] initWithX: mag*cos(ang) Y: mag*sin(ang)];
}
-(jsVector *) add: (jsVector*) b{
return [[jsVector alloc] initWithX: self.x+b.x Y: self.y+b.y];
}
-(jsVector *) sub: (jsVector*) b{
return [[jsVector alloc] initWithX: self.x-b.x Y: self.y-b.y];
}
-(float) length{
return sqrt(_x*_x+_y*_y);
}
-(float) angle{
return atan2(_y, _x);
}
-(jsVector *) unit{
float length = self.length;
return [[jsVector alloc] initWithX: _x/length Y: _y/length];
}
-(jsVector *) dotProduct: (jsVector *) b{
return self;
}
-(jsVector *) crossProduct: (jsVector *) b{
return self;
}
- (id) copyWithZone: (NSZone *) zone{
return [[[self class] allocWithZone: zone] initWithX: self.x Y: self.y];
}
@end