00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "LLVector.h""
00010
00011 @implementation LLVector
00012
00013 -(int) angleFromComponent:(int)x andComponent:(int) y {
00014 // Determine the angle to the given set of coords
00015 if (y > 0) {
00016 return atan(x / y);
00017 }
00018 if (y < 0) {
00019 return atan(x / y) + pi;
00020 }
00021 if ((y = 0) && (x < 0)) {
00022 return 3 * pi / 2;
00023 }
00024
00025 return pi / 2;
00026 }
00027
00028 -(LLVector*)add:(LLVector*)other {
00029
00030 LLVector *result = [[LLVector alloc] init];
00031
00032 // first add the position (may not be interesing)
00033 NSPoint resP;
00034 NSPoint otherP = [other origin];
00035 resP.x = origin.x + otherP.x;
00036 resP.y = origin.y + otherP.y;
00037 [result setOrigin:resP];
00038
00039 // Determine the X and Y components of the resultant vector
00040 int xv = [other size] * sin([other angle]) + size * sin(angle);
00041 int yv = [other size] * cos([other angle]) + size * cos(angle);
00042
00043 // Determine the resultant magnitude
00044 [result setSize: sqrt (xv*xv + yv*yv)];
00045
00046 // and the angle
00047 [result setAngle: [self angleFromComponent: xv andComponent: yv]];
00048
00049 return result;
00050 }
00051
00052 -(LLVector*)substract:(LLVector*)other {
00053
00054 LLVector *result = [[LLVector alloc] init];
00055
00056 // first substract the position (may not be interesing)
00057 NSPoint resP;
00058 NSPoint otherP = [other origin];
00059 resP.x = origin.x - otherP.x;
00060 resP.y = origin.y - otherP.y;
00061 [result setOrigin:resP];
00062
00063 // Determine the X and Y components of the resultant vector
00064 int xv = [other size] * sin([other angle]) + size * sin(angle + pi);
00065 int yv = [other size] * cos([other angle]) + size * cos(angle + pi);
00066
00067 // Determine the resultant magnitude
00068 [result setSize: sqrt (xv*xv + yv*yv)];
00069
00070 // and the angle
00071 [result setAngle: [self angleFromComponent: xv andComponent: yv]];
00072
00073 return result;
00074 }
00075
00076 -(NSPoint) origin {
00077 return origin;
00078 }
00079
00080 -(int) angle {
00081 return angle;
00082 }
00083
00084 -(int) size {
00085 return size;
00086 }
00087
00088 -(void) setOrigin:(NSPoint) pos {
00089 origin = pos;
00090 }
00091
00092 -(void) setAngle: (int) course {
00093 angle = course;
00094 }
00095
00096 -(void) setSize: (int) newSize {
00097 size = newSize;
00098 }
00099
00100 @end
00101