/// <summary> /// Concrete Intersection: Boundingsphere intersects another bounding sphere with 'intersection_type' /// </summary> /// <param name="bs"></param> /// <param name="intersection_type">Not used.</param> /// <returns>True if sphere intersects another sphere, else FALSE</returns> public bool Intersects(BoundingSphere bs, IntersectionTypes intersection_type) { Vector3 direction = Center - bs.Center; float distance = direction.LengthSquared; float radii = Radius + bs.Radius; float radii2 = (float)Math.Pow(radii, 2.0); if (distance > radii2) { return(false); } return(true); }
private void GradeLineIntersection() { if (dropDownMenu.value != (int)DropdownChoices.LINE) { failure("טעית בבחירה, הבחירה הנכונה היא: קו חיתוך"); return; } if (!CheckInputVector(inputX, inputY) || !CheckInputVector(inputX1, inputY1)) { return; } Vector2 center = GetVector2Input(inputX, inputY), direction = GetVector2Input(inputX1, inputY1); Line2 l = new Line2(center, direction); IntersectionTypes t = new IntersectionTypes(); bool doesAnswerIntersectLine0 = Intersection.TestLine2Line2(ref line0, ref l, out t); answerLine.transform.position = center; Vector3 newDirection = Vector3.RotateTowards(answerLine.transform.forward, direction, 1000, 0); answerLine.transform.rotation = Quaternion.LookRotation(newDirection); //answerLine.transform.forward = direction; answerLine.gameObject.SetActive(true); answerLine.vectorLine.SetWidth(4); //answerLine.UpdateLine2(); if (doesAnswerIntersectLine0 && t == IntersectionTypes.Line) { answerLine.SetLineColor(Color.green); answerLine.UpdateLine2(); success(); } else { answerLine.SetLineColor(Color.red); answerLine.UpdateLine2(); failure("הקו שהכנסת אינו נמצא על אחד הקווים"); } }
public void IntersectsTest() { BoundingSphere target = new BoundingSphere(new Vector3(0, 0, 0), 10.0f); BoundingBoxAxisAligned boundBox = new BoundingBoxAxisAligned(new Vector3(-5, -5, -5), new Vector3(5, 5, 5)); IntersectionTypes intersection_type = IntersectionTypes.SOLID; float slight_offset = 0.001f; bool expected = true; bool actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); { // Test1: Solid intersections (IE: Sphere and AABB can reside inside of each other and it counts as an intersection) intersection_type = IntersectionTypes.SOLID; target = new BoundingSphere(new Vector3(0, 0, 0), 1.0f); //Sphere inside AABB expected = true; actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); target = new BoundingSphere(new Vector3(0, 0, 0), 1000.0f); //AABB inside sphere expected = true; actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); target = new BoundingSphere(new Vector3(6, 6, 6), (boundBox.MaxCorner - new Vector3(6, 6, 6)).Length + slight_offset); //Sphere crosses max corner of AABB expected = true; actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); target = new BoundingSphere(new Vector3(6, 6, 6), (boundBox.MaxCorner - new Vector3(6, 6, 6)).Length - slight_offset); //Sphere does not cross max corner of AABB expected = false; actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); } { // Test2: Hollow intersections (IE: Sphere and AABB only intersect one is partially outside of the other. As in when just their surfaces cross.) intersection_type = IntersectionTypes.HOLLOW; target = new BoundingSphere(new Vector3(0, 0, 0), 1.0f); expected = false; actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); target = new BoundingSphere(new Vector3(0, 0, 0), 1000.0f); expected = false; actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); target = new BoundingSphere(new Vector3(6, 6, 6), (boundBox.MaxCorner - new Vector3(6, 6, 6)).Length + slight_offset); //Sphere crosses max corner of AABB expected = true; actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); target = new BoundingSphere(new Vector3(6, 6, 6), (boundBox.MaxCorner - new Vector3(6, 6, 6)).Length - slight_offset); //Sphere does not cross max corner of AABB expected = false; actual = target.Intersects(boundBox, intersection_type); Assert.AreEqual(expected, actual); } { //Test3: Intersects other bounding spheres BoundingSphere target2 = new BoundingSphere(new Vector3(0, 0, 0), 1.0f); target = new BoundingSphere(new Vector3(0, 0, 0), 1.0f); expected = true; actual = target.Intersects(target2, intersection_type); Assert.AreEqual(expected, actual); target2 = new BoundingSphere(new Vector3(10, 10, 10), 1.0f); target = new BoundingSphere(new Vector3(0, 0, 0), 1.0f); expected = false; actual = target.Intersects(target2, intersection_type); Assert.AreEqual(expected, actual); target2 = new BoundingSphere(new Vector3(10, 10, 10), 1000.0f); target = new BoundingSphere(new Vector3(0, 0, 0), 1.0f); expected = true; actual = target.Intersects(target2, intersection_type); Assert.AreEqual(expected, actual); expected = true; actual = target2.Intersects(target, intersection_type); Assert.AreEqual(expected, actual); } }
/// <summary> /// Does BoundingSphere intersect BoundingBoxAxisAligned /// </summary> /// <notes> /// Code modified from: http://tog.acm.org/resources/GraphicsGems/gems/BoxSphere.c /// Support for mixed hollow/solid intersections was dropped. Only hollow-hollow and solid-solid /// are supported /// </notes> /// <param name="intersection_type">Defines intersection method</param> /// <returns>True if sphere intersects bounding box, else FALSE</returns> public bool Intersects(BoundingBoxAxisAligned aabb, IntersectionTypes intersection_type) { float a, b; float r2 = Radius2; bool face; switch (intersection_type) { case IntersectionTypes.HOLLOW: // Hollow Box - Hollow Sphere { float dmin = 0; float dmax = 0; face = false; for (int i = 0; i < 3; i++) { a = (float)Math.Pow(Center[i] - aabb.MinCorner[i], 2.0); b = (float)Math.Pow(Center[i] - aabb.MaxCorner[i], 2.0); dmax += Math.Max(a, b); if (Center[i] < aabb.MinCorner[i]) { face = true; dmin += a; } else if (Center[i] > aabb.MaxCorner[i]) { face = true; dmin += b; } else if (Math.Min(a, b) <= r2) { face = true; } } if (face && (dmin <= r2) && (r2 <= dmax)) { return(true); } break; } case IntersectionTypes.SOLID: // Solid Box - Solid Sphere { float dmin = 0; for (int i = 0; i < 3; i++) { if (Center[i] < aabb.MinCorner[i]) { dmin += (float)Math.Pow(Center[i] - aabb.MinCorner[i], 2.0); } else if (Center[i] > aabb.MaxCorner[i]) { dmin += (float)Math.Pow(Center[i] - aabb.MaxCorner[i], 2.0); } } if (dmin <= r2) { return(true); } break; } } return(false); }
/// <summary> /// Computes the intersection of the lines p1-p2 and p3-p4. /// This function computes both the bool value of the hasIntersection test /// and the (approximate) value of the intersection point itself (if there is one). /// </summary> public virtual void ComputeIntersection(Coordinate p1, Coordinate p2, Coordinate p3, Coordinate p4) { _inputLines[0, 0] = new Coordinate(p1); _inputLines[0, 1] = new Coordinate(p2); _inputLines[1, 0] = new Coordinate(p3); _inputLines[1, 1] = new Coordinate(p4); _result = ComputeIntersect(p1, p2, p3, p4); }
/// <summary> /// /// </summary> public LineIntersector() { _intPt[0] = new Coordinate(); _intPt[1] = new Coordinate(); _result = 0; }
public bool Intersects(BoundingBoxAxisAligned aabb, IntersectionTypes type) { return(Intersects(aabb)); }