public bool ContainsPoint(Vector2 point, float accuracy, ContainmentMethod containmentMethod) { float treshold = accuracy / 2.0f; // Expanded bounds containment test. Rect expandedBounds = this.ExpandedBounds(accuracy); bool expandedBoundsContainment = expandedBounds.Contains(point); if (expandedBoundsContainment == false) { return(false); // Only if passed } // Line distance test. float distance = this.DistanceToPoint(point); bool lineDistanceTest = distance < treshold; if (lineDistanceTest == false) { return(false); // Only if passed } if (containmentMethod == ContainmentMethod.Precise) { // Perpendicular segment. Vector2 normalizedHalf = (this.b - this.a) / 2.0f; float halfLength = normalizedHalf.magnitude; Vector2 normalizedHalfPerpendicular = new Vector2(-normalizedHalf.y, normalizedHalf.x); Vector2 perpendicular_a = this.a + normalizedHalf; Vector2 perpendicular_b = this.a + normalizedHalf + normalizedHalfPerpendicular; // Perpendicular line distance test. float perpendicularDistance = Geometry.PointDistanceFromLine(point, perpendicular_a, perpendicular_b); bool perpendicularDistanceTest = perpendicularDistance < halfLength; // Endpoint distance test if previous failed. if (perpendicularDistanceTest == false) { float distanceFromEndPoints = Mathf.Min(Vector2.Distance(this.a, point), Vector2.Distance(this.b, point)); bool endpointDistanceTest = distanceFromEndPoints < treshold; if (endpointDistanceTest == false) { return(false); // Only if passed } } } // All passed. return(true); }
public bool IntersectionWithSegmentWithAccuracy(Segment segment, float accuracy, ContainmentMethod containmentMethod, out Vector2 intersectionPoint) { intersectionPoint = Vector2.zero; // Default // No intersecting if bounds don't even overlap. Rect expandedBounds = this.ExpandedBounds(accuracy); Rect otherExpandedBounds = segment.ExpandedBounds(accuracy); bool boundsOverlaps = expandedBounds.Overlaps(otherExpandedBounds); if (boundsOverlaps == false) { return(false); // No intersection } if (accuracy > 0.0f) // Only any accuracy is given { // Look up point containments. bool containsA = this.ContainsPoint(segment.a, accuracy, containmentMethod); if (containsA) { intersectionPoint = segment.a; return(true); // Intersecting } bool containsB = this.ContainsPoint(segment.b, accuracy, containmentMethod); if (containsB) { intersectionPoint = segment.b; return(true); // Intersecting } bool otherContainsA = segment.ContainsPoint(this.a, accuracy, containmentMethod); if (otherContainsA) { intersectionPoint = this.a; return(true); // Intersecting } bool otherContainsB = segment.ContainsPoint(this.b, accuracy, containmentMethod); if (otherContainsB) { intersectionPoint = this.b; return(true); // Intersecting } } // Do the Bryce Boe test. bool isIntersecting = Geometry.AreSegmentsIntersecting(this.a, this.b, segment.a, segment.b); if (isIntersecting == false) { return(false); // No intersection } // All fine, intersection point can be determined. intersectionPoint = Geometry.IntersectionPointOfLines(this.a, this.b, segment.a, segment.b); // Actually the intersection of lines defined by segments return(true); // Intersecting }
public bool IntersectionWithSegmentWithAccuracy(Segment segment, ContainmentMethod containmentMethod, out Vector2 intersectionPoint) { return(IntersectionWithSegmentWithAccuracy(segment, defaultAccuracy, containmentMethod, out intersectionPoint)); }