public bool IsPointWithinContour(Vector2 xzPoint, IHexCell cell, HexDirection direction)
        {
            var     contour  = GetContourForCellEdge(cell, direction);
            Vector2 midpoint = cell.AbsolutePositionXZ;

            for (int i = 1; i < contour.Count; i++)
            {
                if (Geometry2D.IsPointWithinTriangle(xzPoint, midpoint, contour[i - 1], contour[i]))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #2
0
        public bool TryGetSextantOfPointInCell(Vector2 xzPoint, IHexCell cell, out HexDirection sextant)
        {
            sextant = HexDirection.NE;

            foreach (var candidate in EnumUtil.GetValues <HexDirection>())
            {
                sextant = candidate;

                if (Geometry2D.IsPointWithinTriangle(
                        xzPoint, cell.AbsolutePositionXZ,
                        cell.AbsolutePositionXZ + RenderConfig.GetFirstCornerXZ(candidate),
                        cell.AbsolutePositionXZ + RenderConfig.GetSecondCornerXZ(candidate)
                        ))
                {
                    return(true);
                }
            }

            return(false);
        }
        public bool DoesXZFrustumOverlap(
            Vector2 bottomLeftFrustum, Vector2 topLeftFrustum, Vector2 topRightFrustum, Vector2 bottomRightFrustum
            )
        {
            Vector2 topMiddleFrustum = (topLeftFrustum + topRightFrustum) / 2f;

            Vector2 bottomLeftChunk  = transform.position.ToXZ();
            Vector2 topLeftChunk     = transform.position.ToXZ() + new Vector2(0f, Terrain.terrainData.size.z);
            Vector2 topRightChunk    = transform.position.ToXZ() + new Vector2(Terrain.terrainData.size.x, Terrain.terrainData.size.z);
            Vector2 bottomRightChunk = transform.position.ToXZ() + new Vector2(Terrain.terrainData.size.x, 0f);

            //The projected frustum is divided into three triangles, each of which are tested

            return((
                       //Are the points in the rightmost triangle?
                       Geometry2D.IsPointWithinTriangle(bottomLeftChunk, bottomLeftFrustum, topLeftFrustum, topMiddleFrustum) ||
                       Geometry2D.IsPointWithinTriangle(topLeftChunk, bottomLeftFrustum, topLeftFrustum, topMiddleFrustum) ||
                       Geometry2D.IsPointWithinTriangle(topRightChunk, bottomLeftFrustum, topLeftFrustum, topMiddleFrustum) ||
                       Geometry2D.IsPointWithinTriangle(bottomRightChunk, bottomLeftFrustum, topLeftFrustum, topMiddleFrustum)
                       ) || (
                       //Are the points in the leftmost triangle?
                       Geometry2D.IsPointWithinTriangle(bottomLeftChunk, bottomRightFrustum, topMiddleFrustum, topRightFrustum) ||
                       Geometry2D.IsPointWithinTriangle(topLeftChunk, bottomRightFrustum, topMiddleFrustum, topRightFrustum) ||
                       Geometry2D.IsPointWithinTriangle(topRightChunk, bottomRightFrustum, topMiddleFrustum, topRightFrustum) ||
                       Geometry2D.IsPointWithinTriangle(bottomRightChunk, bottomRightFrustum, topMiddleFrustum, topRightFrustum)
                       ) || (
                       //Are the points in the middle triangle?
                       Geometry2D.IsPointWithinTriangle(bottomLeftChunk, bottomLeftFrustum, topMiddleFrustum, bottomRightFrustum) ||
                       Geometry2D.IsPointWithinTriangle(topLeftChunk, bottomLeftFrustum, topMiddleFrustum, bottomRightFrustum) ||
                       Geometry2D.IsPointWithinTriangle(topRightChunk, bottomLeftFrustum, topMiddleFrustum, bottomRightFrustum) ||
                       Geometry2D.IsPointWithinTriangle(bottomRightChunk, bottomLeftFrustum, topMiddleFrustum, bottomRightFrustum)
                       ));
        }