public List <EdgeIntersectionResult> CalculateMultipleIntersection(NavigationEdge edge, int maximum) { List <EdgeIntersectionResult> result = new List <EdgeIntersectionResult>(); foreach (var poly in this) { if (poly.GetBoundingIntersection(edge)) { foreach (var constrainedEdge in poly.ConstraintedEdges) { var r = edge.CalculateIntersection(constrainedEdge); if (r.SegmentsIntersect) { result.Add(r); } if (result.Count > maximum) { return(result); } } } } return(result); }
public NavigationEdge GetBounding() { if (cachedBoundingBox == null) { DeterministicVector2 min = new DeterministicVector2(0, 0); DeterministicVector2 max = new DeterministicVector2(0, 0); if (this.Count > 0) { var p = this[0]; min.X = p.X; max.X = p.X; min.Y = p.Y; max.Y = p.Y; } for (var i = 1; i < this.Count; i++) { var p = this[i]; min.X = DeterministicFloat.Min(p.X, min.X); min.Y = DeterministicFloat.Min(p.Y, min.Y); max.X = DeterministicFloat.Max(p.X, max.X); max.Y = DeterministicFloat.Max(p.Y, max.Y); } cachedBoundingBox = new NavigationEdge(min, max); } return(cachedBoundingBox); }
public override bool Equals(object obj) { NavigationEdge other = obj as NavigationEdge; if (other == null) { return(false); } return(this == other); }
public EdgeIntersectionResult CalculateIntersection(NavigationEdge other) { var result = new EdgeIntersectionResult(); var dx12 = this.B.X - this.A.X; var dy12 = this.B.Y - this.A.Y; var dx34 = other.B.X - other.A.X; var dy34 = other.B.Y - other.A.Y; var denominator = dy12 * dx34 - dx12 * dy34; if (denominator == 0) { return(result); } // normally t1 is 0 -> 1 (or no hit), but for us it's 0 -> 1 var t1 = ((this.A.X - other.A.X) * dy34 + (other.A.Y - this.A.Y) * dx34) / denominator; result.LinesIntersect = true; // normally t1 is 0 -> 1 (or no hit), but for us it's 0 -> 1 var t2 = ((other.A.X - this.A.X) * dy12 + (this.A.Y - other.A.Y) * dx12) / -denominator; // result.IntersectionPoint = new DeterministicVector2(this.A.X + dx12 * t1, this.A.Y + dy12 * t1); result.SegmentsIntersect = t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1; result.Deltas = new DeterministicVector2(t1, t2); if (t1 < 0) { t1 = new DeterministicFloat(0); } else if (t1 > 1) { t1 = new DeterministicFloat(1); } if (t2 < 0) { t1 = new DeterministicFloat(0); } else if (t2 > 1) { t2 = new DeterministicFloat(1); } // result.SegmentIntersection = new NavigationEdge(new DeterministicVector2(this.A.X + dx12 * t1, this.A.Y + dy12 * t1), new DeterministicVector2(other.A.X + dx34 * t2, other.A.Y + dy34 * t2)); return(result); }
public EdgeIntersectionResult CalculateAnyIntersection(NavigationEdge edge) { foreach (var poly in this) { foreach (var constrainedEdge in poly.ConstraintedEdges) { var result = edge.CalculateIntersection(constrainedEdge); if (result.SegmentsIntersect) { return(result); } } } return(new EdgeIntersectionResult()); }
public void Initialize(NavigationPolygon floor) { Bounding = floor.GetBounding(); Cells.Clear(); for (DeterministicFloat y = Bounding.A.Y; y < Bounding.B.Y; y++) { for (DeterministicFloat x = Bounding.A.X; x < Bounding.B.X; x++) { Cells.Add(new GridCell(x, y) { Type = GridCellType.Free }); } } }
public void CalculateConstraintedEdges() { ConstraintedEdges.Clear(); if (this.Count < 2) { return; } var p0 = this[this.Count - 1]; var p1 = this[0]; ConstraintedEdges.Add(new NavigationEdge(p0, p1)); for (int i = 1; i < this.Count; i++) { p0 = this[i - 1]; p1 = this[i]; ConstraintedEdges.Add(new NavigationEdge(p0, p1)); } cachedBoundingBox = null; }
public bool GetBoundingIntersection(NavigationEdge edge) { if (GetBoundingDistance(edge.A) == 0) { return(true); } if (GetBoundingDistance(edge.B) == 0) { return(true); } var bounding = this.GetBounding(); var otherBounding = new NavigationEdge(new DeterministicVector2(bounding.A.X, bounding.B.Y), new DeterministicVector2(bounding.B.X, bounding.A.Y)); if (edge.CalculateIntersection(new NavigationEdge(bounding.A, otherBounding.B)).SegmentsIntersect) { return(true); } if (edge.CalculateIntersection(new NavigationEdge(otherBounding.B, bounding.B)).SegmentsIntersect) { return(true); } if (edge.CalculateIntersection(new NavigationEdge(bounding.B, otherBounding.A)).SegmentsIntersect) { return(true); } if (edge.CalculateIntersection(new NavigationEdge(otherBounding.A, bounding.A)).SegmentsIntersect) { return(true); } return(false); }