/// <summary> /// Checks if the grid intersects with the specified vector. /// </summary> /// <param name="vector">The vector.</param> /// <param name="feature">The feature.</param> /// <returns></returns> public bool Intersect(ref Vector2 vector, out Feature feature) { //TODO: Keep and eye out for floating point accuracy issues here. Possibly some //VERY intermittent errors exist? if (AABB.Contains(ref vector)) { int x = (int)Math.Floor((vector.X - AABB.Min.X) * GridCellSizeInv); int y = (int)Math.Floor((vector.Y - AABB.Min.Y) * GridCellSizeInv); float bottomLeft = Nodes[x, y]; float bottomRight = Nodes[x + 1, y]; float topLeft = Nodes[x, y + 1]; float topRight = Nodes[x + 1, y + 1]; if (bottomLeft <= 0 || bottomRight <= 0 || topLeft <= 0 || topRight <= 0) { float xPercent = (vector.X - (GridCellSize * x + AABB.Min.X)) * GridCellSizeInv; float yPercent = (vector.Y - (GridCellSize * y + AABB.Min.Y)) * GridCellSizeInv; float top = MathHelper.Lerp(topLeft, topRight, xPercent); float bottom = MathHelper.Lerp(bottomLeft, bottomRight, xPercent); float distance = MathHelper.Lerp(bottom, top, yPercent); if (distance <= 0) { float right = MathHelper.Lerp(bottomRight, topRight, yPercent); float left = MathHelper.Lerp(bottomLeft, topLeft, yPercent); Vector2 normal = Vector2.Zero; normal.X = right - left; normal.Y = top - bottom; //Uncommented by Daniel Pramel 08/17/08 //make sure the normal is not zero length. if (normal.X != 0 || normal.Y != 0) { Vector2.Normalize(ref normal, out normal); feature = new Feature(ref vector, ref normal, distance); return true; } } } } feature = new Feature(); return false; }
public bool Equals(ref Feature other) { return ((Normal == other.Normal) && (Position == other.Position) && (Distance == other.Distance)); }