Ejemplo n.º 1
0
        /* Intersect the given trapezoidal line with
         * all other lines in this shape. */
        public float  intersect(TrapezoidLine t, float max)
        {
            float min = max;

            foreach (TDPoint pt in points)
            {
                if (points.IndexOf(pt) > 0)
                {
                    TDPoint prevpt = points[points.IndexOf(pt) - 1];
                    if (consider(t.getStart(), pt, prevpt))
                    {
                        min = Mathf.Min(min, (float )(pt.y - prevpt.y) / (pt.x - prevpt.x) * (t.getStart().x - prevpt.x) + prevpt.y);
                    }
                }
            }
            return(min);
        }
Ejemplo n.º 2
0
 public bool equals(TrapezoidLine t)
 {
     return(t.start.equals(start) &&
            t.length == length &&
            t.getUp() == up);
 }
Ejemplo n.º 3
0
        /*
         * Creates trapezoidal maps the "naive" way. More specifically, this
         * function iterates through each shape, and each point of each shape,
         * creating a TrapezoidLine for each point with an initially long length.
         * Then, this length is minimized over all the x intersections for above the
         * point. This is repeated for all the lines below the point. This is
         * primarily used for drawing the visual lines on the screen (as any time
         * savings are already negated when having to draw all objects anyway).
         */
        public List <TrapezoidLine> naiveMap(float width, float height)
        {
            //return all the vertical lines of trapezoid.
            //TODO:fix the boundary parameter;

            List <TrapezoidLine> trapezoidMap = new List <TrapezoidLine>();

            // Add the borders of the window as temporary shapes
            TDShape border  = new TDShape();
            TDShape border2 = new TDShape();

            border.getPoints().Add(new TDPoint(-1f * height, 0));
            border.getPoints().Add(new TDPoint(height, 0));

            border2.getPoints().Add(new TDPoint(0, -1f * width));
            border2.getPoints().Add(new TDPoint(0, width));

            shapes.Add(border);
            shapes.Add(border2);

            // Step through each shape
            for (int i = 0; i < shapes.Count; i++)
            {
                //TDShape s = shapes[i];
                //TDShape sh = shapes[i + 1];
                TDShape sh = shapes[i];

                // Ignore the border shapes so their points do not appear as part of
                // the trapezoidal map
                if (sh.equals(border) || sh.equals(border2))
                {
                    continue;
                }

                // Step through the points in the current shape

                for (int pi = 0; pi < sh.getPoints().Count; pi++)
                {
                    //TDPoint p = sh.getPoints()[pi];
                    TDPoint pt = sh.getPoints()[pi];

                    // Generate two trapezoidal lines for each point (up and down)
                    TrapezoidLine t  = new TrapezoidLine(pt, height + 1, true);
                    TrapezoidLine t2 = new TrapezoidLine(pt, height + 1, false);

                    bool tu  = false;
                    bool t2u = false;

                    // Iterate over all shapes, intersecting the trapezoidal lines
                    // with each of the shapes
                    for (int s2i = 0; s2i < shapes.Count; s2i++)
                    {
                        //TDShape s2 = shapes[s2i];
                        TDShape sh2 = shapes[s2i];


                        // if(!sh.equals(sh2)) {
                        // If the intersection yields a positive difference that is
                        // smaller than the previous length, update t
                        // down
                        if (t.getStart().y - sh2.intersect(t, height + 1) > 0)
                        {
                            t.setLength(Mathf.Min((t.getStart().y - sh2
                                                   .intersect(t, height + 1)), t.getLength()));
                            tu = true;
                        }
                        // if the intersection yields a negative difference that is
                        // absolutely smaller than the previous length, update t2
                        else if (t2.getStart().y - sh2.intersect(t2, height + 1) < 0)
                        {
                            t2.setLength(Mathf.Min(Mathf.Abs(t2.getStart().y
                                                             - sh2.intersect(t2, height + 1)), t2
                                                   .getLength()));
                            t2u = true;
                        }
                        // }
                    }
                    // If the lengths have been updated to a reasonable value, Add
                    // them
                    if (t.getLength() < height)
                    {
                        trapezoidMap.Add(t);
                    }
                    if (t2.getLength() < height)
                    {
                        trapezoidMap.Add(t2);
                    }
                }
            }



            // Remove the borders so they are not displayed
            shapes.Remove(border);
            shapes.Remove(border2);

            return(trapezoidMap);
        }