示例#1
0
        /*
         * Returns true if this segment is inside or intersects the given
         * bounding box.
         *
         * @param r a bounding box.
         */

        public bool Intersects(Box2d r)
        {
            var b = a + ab;

            if (r.Contains(a) || r.Contains(b))
            {
                return(true);
            }

            var t = new Box2d(a, b);

            if (t.xmin > r.xmax || t.xmax < r.xmin || t.ymin > r.ymax || t.ymax < r.ymin)
            {
                return(false);
            }

            var p0 = Vector2d.Cross(ab, new Vector2d(r.xmin, r.ymin) - a);
            var p1 = Vector2d.Cross(ab, new Vector2d(r.xmax, r.ymin) - a);

            if (p1 * p0 <= 0)
            {
                return(true);
            }

            var p2 = Vector2d.Cross(ab, new Vector2d(r.xmin, r.ymax) - a);

            if (p2 * p0 <= 0)
            {
                return(true);
            }

            var p3 = Vector2d.Cross(ab, new Vector2d(r.xmax, r.ymax) - a);

            if (p3 * p0 <= 0)
            {
                return(true);
            }

            return(false);
        }
示例#2
0
    /**
     * Returns true if this segment is inside or intersects the given
     * bounding box.
     *
     * @param r a bounding box.
     */
    public bool Intersects(Box2d r)
    {
        Vector2d b = a + ab;
        if (r.Contains(a) || r.Contains(b)) {
            return true;
        }

        Box2d t = new Box2d(a, b);
        if (t.xmin > r.xmax || t.xmax < r.xmin || t.ymin > r.ymax || t.ymax < r.ymin) {
            return false;
        }

        double p0 = Vector2d.Cross(ab, new Vector2d(r.xmin, r.ymin) - a);
        double p1 = Vector2d.Cross(ab, new Vector2d(r.xmax, r.ymin) - a);
        if (p1 * p0 <= 0) {
            return true;
        }
        double p2 = Vector2d.Cross(ab, new Vector2d(r.xmin, r.ymax) - a);
        if (p2 * p0 <= 0) {
            return true;
        }
        double p3 = Vector2d.Cross(ab, new Vector2d(r.xmax, r.ymax) - a);
        if (p3 * p0 <= 0) {
            return true;
        }

        return false;
    }
示例#3
0
        public static void test_min_box_2()
        {
            Random r = new Random(31337);

            bool write_svg           = false;
            int  contained_circles_N = 100;
            int  test_iters          = 1000;

            //LocalProfiler p = new LocalProfiler();
            //p.Start("Hulls");

            QueryNumberType[] modes = new QueryNumberType[] { QueryNumberType.QT_DOUBLE, QueryNumberType.QT_INT64 };
            //QueryNumberType[] modes = new QueryNumberType[] { QueryNumberType.QT_DOUBLE };

            foreach (var queryMode in modes)
            {
                for (int k = 0; k < test_iters; ++k)
                {
                    int        N        = contained_circles_N;
                    double     scale    = (r.NextDouble() + 0.1) * 1024.0;
                    Interval1d radRange = new Interval1d(10, 100);

                    Vector2d[] pts    = TestUtil.RandomPoints2(N, r, Vector2d.Zero, scale);
                    double[]   radius = TestUtil.RandomScalars(N, r, new Interval1d(radRange));

                    double eps = MathUtil.Epsilonf;

                    SVGWriter svg = (write_svg) ? new SVGWriter() : null;

                    List <Vector2d> accumPts = new List <Vector2d>();
                    for (int i = 0; i < pts.Length; ++i)
                    {
                        Polygon2d circ = Polygon2d.MakeCircle(radius[i], 16, radius[i]);
                        circ.Translate(pts[i]);
                        accumPts.AddRange(circ.Vertices);

                        if (svg != null)
                        {
                            svg.AddPolygon(circ, SVGWriter.Style.Outline("black", 1.0f));
                        }
                    }

                    ContMinBox2 contbox = new ContMinBox2(accumPts, 0.001, queryMode, false);
                    Box2d       box     = contbox.MinBox;

                    if (svg != null)
                    {
                        svg.AddPolygon(new Polygon2d(box.ComputeVertices()), SVGWriter.Style.Outline("red", 2.0f));
                        svg.Write(TestUtil.GetTestOutputPath("contbox.svg"));
                    }

                    foreach (Vector2d v in accumPts)
                    {
                        if (box.Contains(v))
                        {
                            continue;
                        }
                        double d = box.DistanceSquared(v);
                        if (d < eps)
                        {
                            continue;
                        }
                        System.Console.WriteLine("test_min_box_2: Point {0} not contained!", v);
                    }
                }
            }

            //p.StopAll();
            //System.Console.WriteLine(p.AllTimes());
        }