/// <summary> /// Indica a que lado esta el rectangulo respecto de la linea: /// - Si == 0, toca la linea o la cruza. /// - Si > 0 esta debajo/derecha de la linea. /// - Si < 0 esta encima/izquierda de la linea. /// </summary> public LineSide WhichSide(BoundingBox2d r) { int[] sides = new int[3]; foreach (Point2d p in r.GetVertices()) { LineSide lado = this.WhichSide(p); sides[(int)lado]++; } if (sides[(int)LineSide.Middle] > 0) { // Toca la linea. return(LineSide.Middle); } if ((sides[(int)LineSide.Left] > 0) && (sides[(int)LineSide.Right] > 0)) { // Cruza la linea. return(LineSide.Middle); } if (sides[(int)LineSide.Left] > 0) { // totalmente a un lado return(LineSide.Left); } // if (sides[(int)Side.Right] > 0) // totalmente a un lado return(LineSide.Right); }
public BoundingBox2d GetEnvelope(double d, double estacion1, double estacion2) { double t0 = this.t0; double t1 = this.t1; Vector2d v = this.dirN.PerpRight.Mul(d); if (estacion1.EpsilonEquals(this.t0) && estacion2.EpsilonEquals(this.t1)) { return(BoundingBox2d.Union(this.p0.Add(v), this.p1.Add(v))); } if (this.t0.EpsilonEquals(this.t1)) { return(BoundingBox2d.Union(this.p0.Add(v), this.p0.Add(v))); } // Se normaliza la estacion. double t01_0 = (estacion1 - this.t0) / (this.t1 - this.t0); double t01_1 = (estacion2 - this.t0) / (this.t1 - this.t0); return(BoundingBox2d.Union( this.Evaluate01(t01_0).Add(v), this.Evaluate01(t01_1).Add(v))); }
public void TestWhichSideRectangle() { { Line2d line = Line2d.NewNormal(new Point2d(0, 5), new Vector2d(1, 1)); Assert.IsTrue(line.WhichSide(BoundingBox2d.FromExtents(0, 0, 1, 1)) == LineSide.Right); Assert.IsTrue(line.WhichSide(BoundingBox2d.FromExtents(0, 4, 2, 2)) == LineSide.Middle); Assert.IsTrue(line.WhichSide(BoundingBox2d.FromExtents(0, 6, 1, 1)) == LineSide.Middle); Assert.IsTrue(line.WhichSide(BoundingBox2d.FromExtents(0, 7, 1, 1)) == LineSide.Left); } }
public void TestInterseTouch() { { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = new BoundingBox2d(5, 15, 5, 15); Assert.IsFalse(bbox1.Touch(bbox2)); Assert.IsFalse(bbox2.Touch(bbox1)); } { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = new BoundingBox2d(5, 7, 5, 7); Assert.IsFalse(bbox1.Touch(bbox2)); Assert.IsFalse(bbox2.Touch(bbox1)); } { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = new BoundingBox2d(0, 10, 0, 10); Assert.IsTrue(bbox1.Touch(bbox2)); Assert.IsTrue(bbox2.Touch(bbox1)); } { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = new BoundingBox2d(10, 20, 10, 20); Assert.IsTrue(bbox1.Touch(bbox2)); Assert.IsTrue(bbox2.Touch(bbox1)); } { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = BoundingBox2d.Empty; Assert.IsFalse(bbox1.Touch(bbox2)); Assert.IsFalse(bbox2.Touch(bbox1)); } { BoundingBox2d bbox1 = BoundingBox2d.Empty; BoundingBox2d bbox2 = BoundingBox2d.Empty; Assert.IsFalse(bbox1.Touch(bbox2)); } { BoundingBox2d bbox1 = new BoundingBox2d(0, double.MaxValue, 0, double.MaxValue); BoundingBox2d bbox2 = new BoundingBox2d(-double.MaxValue, 10, -double.MaxValue, 10); Assert.IsFalse(bbox1.Touch(bbox2)); Assert.IsFalse(bbox2.Touch(bbox1)); } }
public BoundingBox2d GetEnvelope(double t0, double t1) { if (t0.EpsilonEquals(this.t0) && t1.EpsilonEquals(this.t1)) { return(BoundingBox2d.Union(this.p0, this.p1)); } if (this.t0.EpsilonEquals(this.t1)) { return(BoundingBox2d.Union(this.p0, this.p0)); } // Se normaliza la estacion. double t01_0 = (t0 - this.t0) / (this.t1 - this.t0); double t01_1 = (t1 - this.t0) / (this.t1 - this.t0); return(BoundingBox2d.Union( this.Evaluate01(t01_0), this.Evaluate01(t01_1))); }
public void TestUnion() { { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = new BoundingBox2d(5, 15, 5, 15); Assert.IsTrue(bbox1.Union(bbox2).EpsilonEquals(new BoundingBox2d(0, 15, 0, 15))); } { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = new BoundingBox2d(5, 7, 5, 7); Assert.IsTrue(bbox1.Union(bbox2).EpsilonEquals(new BoundingBox2d(0, 10, 0, 10))); } { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = new BoundingBox2d(0, 10, 0, 10); Assert.IsTrue(bbox1.Union(bbox2).EpsilonEquals(new BoundingBox2d(0, 10, 0, 10))); } { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = new BoundingBox2d(10, 20, 10, 20); Assert.IsTrue(bbox1.Union(bbox2).EpsilonEquals(new BoundingBox2d(0, 20, 0, 20))); } { BoundingBox2d bbox1 = new BoundingBox2d(0, 10, 0, 10); BoundingBox2d bbox2 = BoundingBox2d.Empty; Assert.IsTrue(bbox1.Union(bbox2).EpsilonEquals(new BoundingBox2d(0, 10, 0, 10))); } { BoundingBox2d bbox1 = BoundingBox2d.Empty; BoundingBox2d bbox2 = BoundingBox2d.Empty; Assert.IsTrue(bbox1.Union(bbox2).IsEmpty); } { BoundingBox2d bbox1 = new BoundingBox2d(0, double.MaxValue, 0, double.MaxValue); BoundingBox2d bbox2 = new BoundingBox2d(-double.MaxValue, 10, -double.MaxValue, 10); Assert.IsTrue(bbox1.Union(bbox2).EpsilonEquals(new BoundingBox2d(-double.MaxValue, double.MaxValue, -double.MaxValue, double.MaxValue))); } }
public static BoundingBox2d DoTransform(this ITransform2 transform, BoundingBox2d bbox) { return(BoundingBox2d.UnionOfPoints(bbox.GetVertices().Select(v => transform.DoTransform(v)))); }