Ejemplo n.º 1
0
        private void Draw(Bitmap bitmap, GeometryShader2D shader, Matrix4x4 transform)
        {
            var geometries = new List <IMarkGeometry>();

            var border = (MarkGeometryRectangle)Boundary.Clone();

            border.Stroke = Color.White;

            border.Transform(transform);
            geometries.Add(border);

            // apply transformation to geometries
            foreach (var line in Segments)
            {
                var geometry = ((IMarkGeometry)line.Clone());
                geometry.Transform(transform);
                geometries.Add(geometry);
            }

            // draw used cells in red
            shader.Draw(
                bitmap,
                geometries
                );

            if (ChildrenExists)
            {
                NorthWest.Draw(bitmap, shader, transform);
                NorthEast.Draw(bitmap, shader, transform);
                SouthWest.Draw(bitmap, shader, transform);
                SouthEast.Draw(bitmap, shader, transform);
            }
        }
Ejemplo n.º 2
0
        public List <NamedPoint> GetPointsInArea(Rectangle area)
        {
            List <NamedPoint> allPoints = new List <NamedPoint>();

            if (this.Bounds.IntersectsWith(area) == false)
            {
                return(allPoints);
            }

            if (Point != null)
            {
                bool pointInArea = Program.PointWithinSquare(area, Point);
                if (pointInArea == true)
                {
                    allPoints.Add(Point);
                }
            }

            if (NorthWest == null)
            {
                return(allPoints);
            }

            allPoints.AddRange(NorthWest.GetPointsInArea(area));
            allPoints.AddRange(NorthEast.GetPointsInArea(area));
            allPoints.AddRange(SouthWest.GetPointsInArea(area));
            allPoints.AddRange(SouthEast.GetPointsInArea(area));

            return(allPoints);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get all objects within the provided range.
        /// </summary>
        /// <param name="range">The area to get objects from.</param>
        /// <returns>Returns all objects that are within the provided range.</returns>
        public List <T> Query(RectangleF range)
        {
            List <T> found = new List <T>();

            if (Boundary.Intersects(range))
            {
                foreach (Tuple <Vector2, T> point in points)
                {
                    if (range.Contains(point.Item1))
                    {
                        found.Add(point.Item2);
                    }
                }
            }
            else
            {
                return(found);
            }

            if (divided)
            {
                found = found.Concat(NorthWest.Query(range)).ToList();
                found = found.Concat(NorthEast.Query(range)).ToList();
                found = found.Concat(SouthWest.Query(range)).ToList();
                found = found.Concat(SouthEast.Query(range)).ToList();
            }

            return(found);
        }
Ejemplo n.º 4
0
        public void Draw(Graphics graphics)
        {
            Pen pen = Pens.Black;

            graphics.DrawRectangle(pen, Bounds);

            if (Point != null)
            {
                Point.Draw(graphics);
            }

            if (NorthWest != null)
            {
                NorthWest.Draw(graphics);
            }
            if (NorthEast != null)
            {
                NorthEast.Draw(graphics);
            }
            if (SouthWest != null)
            {
                SouthWest.Draw(graphics);
            }
            if (SouthEast != null)
            {
                SouthEast.Draw(graphics);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Checks to see if the actual point is in the quad tree as opposed to being bounded by it
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public bool Contains(Point2Int point)
        {
            if (!BoundingBox.ContainsPoint(point))
            {
                return(false);
            }

            if (Points != null)
            {
                return(Points.Contains(point));
            }

            var center = BoundingBox.CenterPoint();

            if (point.X < center.X)
            {
                if (point.Y < center.Y)
                {
                    // bottom left
                    return(SouthWest.Contains(point));
                }

                //top left
                return(NorthWest.Contains(point));
            }

            if (point.Y < center.Y)
            {
                // bottom right
                return(SouthEast.Contains(point));
            }
            //top right
            return(NorthEast.Contains(point));
        }
Ejemplo n.º 6
0
    public void Subdivide()
    {
        if (isDivided)
        {
            return;
        }

        NorthEast = Create(transform, gameObject.name + " - North East", this);
        NorthWest = Create(transform, gameObject.name + " - North West", this);
        SouthEast = Create(transform, gameObject.name + " - South East", this);
        SouthWest = Create(transform, gameObject.name + " - South West", this);

        Vector3 axisA = CurrentFace.axisA / 2;
        Vector3 axisB = CurrentFace.axisB / 2;

        NorthEast.Initialize(
            shapeGenerator,
            colorSettings,
            CurrentFace.LocalUp + axisA - axisB,
            axisA,
            axisB,
            CurrentFace.Resolution
            );

        NorthWest.Initialize(
            shapeGenerator,
            colorSettings,
            CurrentFace.LocalUp - axisA - axisB,
            axisA,
            axisB,
            CurrentFace.Resolution
            );

        SouthEast.Initialize(
            shapeGenerator,
            colorSettings,
            CurrentFace.LocalUp + axisA + axisB,
            axisA,
            axisB,
            CurrentFace.Resolution
            );

        SouthWest.Initialize(
            shapeGenerator,
            colorSettings,
            CurrentFace.LocalUp - axisA + axisB,
            axisA,
            axisB,
            CurrentFace.Resolution
            );

        NorthEast.CurrentLevelOfDetail = CurrentLevelOfDetail + 1;
        NorthWest.CurrentLevelOfDetail = CurrentLevelOfDetail + 1;
        SouthEast.CurrentLevelOfDetail = CurrentLevelOfDetail + 1;
        SouthWest.CurrentLevelOfDetail = CurrentLevelOfDetail + 1;

        isDivided            = true;
        MeshRenderer.enabled = false;
    }
Ejemplo n.º 7
0
        public bool Insert(MarkGeometryLine line)
        {
            if (
                !(
                    GeometricArithmeticModule.IsWithin2D(line.StartPoint, Boundary.Extents) &&
                    GeometricArithmeticModule.IsWithin2D(line.EndPoint, Boundary.Extents)
                    )
                )
            {
                return(false);
            }

            // ensure quads exist
            if (!ChildrenExists)
            {
                var radius = 0.5 * SubSize;

                NorthWest = new ContourQuadTree(
                    Boundary.Extents.MinX + radius, // west
                    Boundary.Extents.MaxY - radius, // north
                    SubSize
                    );
                NorthEast = new ContourQuadTree(
                    Boundary.Extents.MaxX - radius, // east
                    Boundary.Extents.MaxY - radius, // north
                    SubSize
                    );
                SouthWest = new ContourQuadTree(
                    Boundary.Extents.MinX + radius, // west
                    Boundary.Extents.MinY + radius, // south
                    SubSize
                    );
                SouthEast = new ContourQuadTree(
                    Boundary.Extents.MaxX - radius, // east
                    Boundary.Extents.MinY + radius, // south
                    SubSize
                    );

                ChildrenExists = true;
            }

            if (
                (line.Length <= MinSize) ||
                !(
                    NorthWest.Insert(line) ||
                    NorthEast.Insert(line) ||
                    SouthWest.Insert(line) ||
                    SouthEast.Insert(line)
                    )
                )
            {
                Segments.Add(line);
            }

            return(true);
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Calculate a hashcode for this GeoArea.
        /// </summary>
        /// <returns>the hash code</returns>
        public override int GetHashCode()
        {
            unchecked
            {
                var hash = 17;
                hash = hash * 23 + NorthWest.GetHashCode();
                hash = hash * 23 + Size.GetHashCode();

                return(hash);
            }
        }
Ejemplo n.º 9
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = NorthWest.GetHashCode();
         hashCode = (hashCode * 397) ^ NorthEast.GetHashCode();
         hashCode = (hashCode * 397) ^ SouthEast.GetHashCode();
         hashCode = (hashCode * 397) ^ SouthWest.GetHashCode();
         return(hashCode);
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Compute matrix from quad tree
        /// </summary>
        public void computeMatrix()
        {
            if (NorthWest != null)
            {
                if (NorthWest.RootColor == Colors.Gray)
                {
                    NorthWest.computeMatrix();
                }
                else
                {
                    NorthWest.setMatrix(NorthWest.RootColor);
                }
            }

            if (SouthWest != null)
            {
                if (SouthWest.RootColor == Colors.Gray)
                {
                    SouthWest.computeMatrix();
                }
                else
                {
                    SouthWest.setMatrix(SouthWest.RootColor);
                }
            }

            if (SouthEast != null)
            {
                if (SouthEast.RootColor == Colors.Gray)
                {
                    SouthEast.computeMatrix();
                }
                else
                {
                    SouthEast.setMatrix(SouthEast.RootColor);
                }
            }

            if (NorthEast != null)
            {
                if (NorthEast.RootColor == Colors.Gray)
                {
                    NorthEast.computeMatrix();
                }
                else
                {
                    NorthEast.setMatrix(NorthEast.RootColor);
                }
            }

            mergeMatrices();
        }
Ejemplo n.º 11
0
    public void UpdateColors(ColorGenerator colorGenerator)
    {
        if (isDivided)
        {
            NorthEast.UpdateColors(colorGenerator);
            NorthWest.UpdateColors(colorGenerator);
            SouthEast.UpdateColors(colorGenerator);
            SouthWest.UpdateColors(colorGenerator);
            return;
        }

        CurrentFace.UpdateUVs(colorGenerator);
    }
Ejemplo n.º 12
0
 public void ConstructMesh()
 {
     if (isDivided)
     {
         NorthEast.ConstructMesh();
         NorthWest.ConstructMesh();
         SouthEast.ConstructMesh();
         SouthWest.ConstructMesh();
     }
     else
     {
         CurrentFace.ConstructMesh();
     }
 }
Ejemplo n.º 13
0
        private IntersectionsBinaryTree Intersect(MarkGeometryLine line, LineEquation equation)
        {
            if (!equation.PassesThroughRect(Boundary))
            {
                return(null);
            }

            // using binary tree to sort points relative to the line's starting point
            var intersections = new IntersectionsBinaryTree(line.StartPoint);

            if (ChildrenExists)
            {
                IntersectionsBinaryTree childIntersections;

                if ((childIntersections = NorthWest.Intersect(line)) != null)
                {
                    intersections.InsertRange(childIntersections);
                }
                if ((childIntersections = NorthEast.Intersect(line)) != null)
                {
                    intersections.InsertRange(childIntersections);
                }
                if ((childIntersections = SouthWest.Intersect(line)) != null)
                {
                    intersections.InsertRange(childIntersections);
                }
                if ((childIntersections = SouthEast.Intersect(line)) != null)
                {
                    intersections.InsertRange(childIntersections);
                }
            }

            MarkGeometryPoint intersection;

            for (int i = 0; i < Segments.Count; i++)
            {
                if ((
                        intersection = GeometricArithmeticModule.CalculateIntersection2D(
                            line,
                            Segments[i]
                            )) != null
                    )
                {
                    intersections.Insert(intersection);
                }
            }

            return(intersections);
        }
Ejemplo n.º 14
0
        public void GetPointsInArea(Bounding2DBox area, ref List <Point2Int> list)
        {
            if (!BoundingBox.DoesBoundaryBoxIntersect(area))
            {
                return;
            }

            if (Points != null)
            {
                GetLocalPointsInArea(area, ref list);
            }
            else
            {
                NorthEast.GetPointsInArea(area, ref list);
                NorthWest.GetPointsInArea(area, ref list);
                SouthWest.GetPointsInArea(area, ref list);
                SouthEast.GetPointsInArea(area, ref list);
            }
        }
Ejemplo n.º 15
0
        private void AddPointToChildren(Point2Int point)
        {
            var center = BoundingBox.CenterPoint();

            if (point.X < center.X)
            {
                if (point.Y < center.Y)
                {
                    SouthWest.Add(point);
                    return;
                }
                NorthWest.Add(point);
                return;
            }

            if (point.Y < center.Y)
            {
                SouthEast.Add(point);
                return;
            }
            NorthEast.Add(point);
        }
Ejemplo n.º 16
0
        public void Insert(Point pt)
        {
            if (!_rectangle.Contains(pt))
            {
                return;
            }
            if (ptList.Count < CAPACITY)
            {
                ptList.Add(pt);
            }
            else
            {
                if (!divided)
                {
                    SubDivide();
                }

                NorthEast.Insert(pt);
                SouthEast.Insert(pt);
                SouthWest.Insert(pt);
                NorthWest.Insert(pt);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Insert a point into the QuadTree.
        /// </summary>
        /// <param name="point">Position where to insert the point.</param>
        /// <param name="obj">The object that is attached to the point.</param>
        /// <returns>Returns true if the point was successfully inserted into this QuadTree, otherwise false.</returns>
        public bool Insert(Vector2 point, T obj)
        {
            if (!Boundary.Contains(point))
            {
                return(false);
            }

            if (points.Count < Capacity)
            {
                // Inserted a point successfully.
                points.Add(new Tuple <Vector2, T>(point, obj));
                return(true);
            }
            else if (!divided)
            {
                Subdivide();
            }

            if (NorthWest.Insert(point, obj))
            {
                return(true);
            }
            if (NorthEast.Insert(point, obj))
            {
                return(true);
            }
            if (SouthWest.Insert(point, obj))
            {
                return(true);
            }
            if (SouthEast.Insert(point, obj))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 18
0
        public void DrawPoints()
        {
            int x = _rectangle._x;
            int y = _rectangle._y;
            int w = _rectangle._w;
            int h = _rectangle._h;

            SwinGame.DrawRectangle(SwinGame.ColorBlack(), x - w, y - h, w * 2, h * 2);
            // SwinGame.DrawRectangle(SwinGame.ColorBlack(), _rectangle._x - 1 - _rectangle._w , _rectangle._y - 1 - _rectangle._h, 2 + (_rectangle._w *2),2+( _rectangle._h *2));
            //SwinGame.DrawRectangle(SwinGame.ColorWhite(), _rectangle._x - _rectangle._w, _rectangle._y - _rectangle._h, _rectangle._w *2, _rectangle._h *2);

            foreach (Point pt in ptList)
            {
                SwinGame.DrawCircle(SwinGame.ColorBlack(), pt._x, pt._y, 1);
            }

            if (divided)
            {
                NorthWest.DrawPoints();
                NorthEast.DrawPoints();
                SouthWest.DrawPoints();
                SouthEast.DrawPoints();
            }
        }
Ejemplo n.º 19
0
    public QuadTreeTerrainFace FindClosestFace(Vector3 point)
    {
        if (isDivided)
        {
            var neClosestFace = NorthEast.FindClosestFace(point);
            var nwClosestFace = NorthWest.FindClosestFace(point);
            var seClosestFace = SouthEast.FindClosestFace(point);
            var swClosestFace = SouthWest.FindClosestFace(point);

            float neDistance = neClosestFace.DistanceTo(point);
            float nwDistance = nwClosestFace.DistanceTo(point);
            float seDistance = seClosestFace.DistanceTo(point);
            float swDistance = swClosestFace.DistanceTo(point);

            float minDistance = Mathf.Min(neDistance, nwDistance, seDistance, swDistance);

            if (neDistance == minDistance)
            {
                return(neClosestFace);
            }
            else if (nwDistance == minDistance)
            {
                return(nwClosestFace);
            }
            else if (seDistance == minDistance)
            {
                return(seClosestFace);
            }
            else if (swDistance == minDistance)
            {
                return(swClosestFace);
            }
        }

        return(this);
    }
Ejemplo n.º 20
0
 public override string ToString()
 {
     return(string.Format("geography::Parse('POLYGON(({0}, {1}, {2}, {3}, {0}))')", NorthWest.AsText(), SouthWest.AsText(), SouthEast.AsText(), NorthEast.AsText()));
 }