|
Britbot
|
00001 #region #Usings 00002 00003 using System; 00004 using Pirates; 00005 00006 #endregion 00007 00008 namespace Britbot 00009 { 00014 public class HeadingVector 00015 { 00016 #region Fields & Properies 00017 00021 public double X { get; private set; } 00022 00026 public double Y { get; private set; } 00027 00028 #endregion 00029 00030 #region Constructors & Initializers 00031 00037 public HeadingVector(Direction d = Direction.NOTHING) 00038 { 00039 switch (d) 00040 { 00041 case Direction.NORTH: 00042 X = 0; 00043 Y = -1; 00044 break; 00045 case Direction.EAST: 00046 X = 1; 00047 Y = 0; 00048 break; 00049 case Direction.SOUTH: 00050 X = 0; 00051 Y = 1; 00052 break; 00053 case Direction.WEST: 00054 X = -1; 00055 Y = 0; 00056 break; 00057 default: 00058 X = 0; 00059 Y = 0; 00060 break; 00061 } 00062 } 00063 00068 public HeadingVector(HeadingVector toCopy) 00069 { 00070 this.X = toCopy.X; 00071 this.Y = toCopy.Y; 00072 } 00073 00079 public HeadingVector(double x, double y) 00080 { 00081 this.X = x; 00082 this.Y = y; 00083 } 00084 00085 #endregion 00086 00091 public override int GetHashCode() 00092 { 00093 unchecked 00094 { 00095 return (this.X.GetHashCode() * 397) ^ this.Y.GetHashCode(); 00096 } 00097 } 00098 00105 public void SetCoordinates(double x = 0, double y = 0) 00106 { 00107 this.X = x; 00108 this.Y = y; 00109 } 00110 00115 public void SetCoordinates(HeadingVector hv) 00116 { 00117 this.X = hv.X; 00118 this.Y = hv.Y; 00119 } 00120 00128 public static double operator *(HeadingVector hv1, HeadingVector hv2) 00129 { 00130 return hv1.X * hv2.X + hv1.Y * hv2.Y; 00131 } 00132 00142 public static HeadingVector adjustHeading(HeadingVector hv1, HeadingVector hv2) 00143 { 00144 //defining the result 00145 HeadingVector newHv = new HeadingVector(hv1); 00146 00147 00148 //if dot product is negative it means that the angle is bigger 00149 // then 90 so it implies change in direction: reset count 00150 //otherwise normal vector addition 00151 //WOW math is useful 00152 //I thought you HATED the applied math department 00153 if (hv1 * hv2 < 0) 00154 { 00155 newHv = hv2; 00156 } //also check that new direction isn't nothing 00157 else if (hv2.Norm() == 0) 00158 { 00159 newHv = hv2; 00160 } 00161 else 00162 { 00163 newHv.X += hv2.X; 00164 newHv.Y += hv2.Y; 00165 } 00166 //return the new heading vector 00167 return newHv; 00168 } 00169 00176 public static HeadingVector adjustHeading(HeadingVector hv1, Direction dir) 00177 { 00178 return HeadingVector.adjustHeading(hv1, new HeadingVector(dir)); 00179 } 00180 00186 public HeadingVector adjustHeading(HeadingVector Dir) 00187 { 00188 //just use the existing function 00189 this.SetCoordinates(HeadingVector.adjustHeading(this, Dir)); 00190 00191 //return this for a+b+c calculations 00192 return this; 00193 } 00194 00200 public HeadingVector adjustHeading(Direction Dir) 00201 { 00202 //just use the existing function 00203 this.SetCoordinates(HeadingVector.adjustHeading(this, new HeadingVector(Dir))); 00204 00205 //return this for a+b+c calculations 00206 return this; 00207 } 00208 00215 public static HeadingVector CalcDifference(Location source, Location target) 00216 { 00217 //assigning new variable 00218 return new HeadingVector(target.Col - source.Col, target.Row - source.Row); 00219 } 00220 00228 public static Location AddvanceByVector(Location loc, HeadingVector hv) 00229 { 00230 //calculate new col 00231 int col = loc.Col + (int) hv.X; 00232 int row = loc.Row + (int) hv.Y; 00233 00234 //check if out of boundries 00235 //first check negative valuse 00236 col = Math.Max(col, 0); 00237 row = Math.Max(row, 0); 00238 00239 //check to big boundries 00240 col = Math.Min(col, Bot.Game.GetCols() - 1); 00241 row = Math.Min(row, Bot.Game.GetRows() - 1); 00242 00243 //return new location 00244 return new Location(row, col); 00245 } 00246 00252 public HeadingVector Orthogonal() 00253 { 00254 //like multiplying by -1 00255 return new HeadingVector {X = this.Y, Y = -this.X}; 00256 } 00257 00258 //------------NORMS------------ 00259 00265 public double NormSquared() 00266 { 00267 return X * X + Y * Y; 00268 } 00269 00275 public double Norm() 00276 { 00277 return Math.Sqrt(NormSquared()); 00278 } 00279 00284 public HeadingVector Normalize() 00285 { 00286 return (1 / this.Norm()) * this; 00287 } 00288 00294 public double Norm1() 00295 { 00296 return Math.Abs(X) + Math.Abs(Y); 00297 } 00298 00303 public override string ToString() 00304 { 00305 return "(" + X + ", " + Y + ")"; 00306 } 00307 00318 protected bool Equals(HeadingVector other) 00319 { 00320 return X == other.X && Y == other.Y; 00321 } 00322 00328 public override bool Equals(object obj) 00329 { 00330 if (object.ReferenceEquals(null, obj)) 00331 return false; 00332 if (object.ReferenceEquals(this, obj)) 00333 return true; 00334 if (obj.GetType() != this.GetType()) 00335 return false; 00336 return Equals((HeadingVector) obj); 00337 } 00338 00350 public static bool operator ==(HeadingVector hv1, HeadingVector hv2) 00351 { 00352 return hv1.X == hv2.X && hv1.Y == hv2.Y; 00353 } 00354 00361 public static bool operator !=(HeadingVector hv1, HeadingVector hv2) 00362 { 00363 return !(hv1 == hv2); 00364 } 00365 00372 public static HeadingVector operator +(HeadingVector hv1, HeadingVector hv2) 00373 { 00374 //defining the result 00375 HeadingVector newHv = new HeadingVector(hv1); 00376 00377 //adding up 00378 newHv.X += hv2.X; 00379 newHv.Y += hv2.Y; 00380 00381 return newHv; 00382 } 00383 00390 public static HeadingVector operator -(HeadingVector hv1, HeadingVector hv2) 00391 { 00392 //defining the result 00393 HeadingVector newHv = new HeadingVector(hv1); 00394 00395 //Substraction 00396 newHv.X -= hv2.X; 00397 newHv.Y -= hv2.Y; 00398 00399 return newHv; 00400 } 00401 00408 public static HeadingVector operator *(double scalar, HeadingVector hv) 00409 { 00410 //defining the result 00411 HeadingVector newHv = new HeadingVector(scalar * hv.X, scalar * hv.Y); 00412 00413 return newHv; 00414 } 00415 } 00416 }
1.7.6.1