public override bool HitTest(Vector2 positionLngLat, int zoom)
 {
     if (points == null)
     {
         return(false);
     }
     return(OnlineMapsUtils.IsPointInPolygon(points, positionLngLat.x, positionLngLat.y));
 }
        public bool Contains(Vector3 point, Transform transform)
        {
            Vector3 p = Quaternion.Inverse(transform.rotation) * (point - transform.position);

            p.x /= transform.lossyScale.x;
            p.z /= transform.lossyScale.z;
            p   += transform.position;
            return(OnlineMapsUtils.IsPointInPolygon(poly, p.x, p.z));
        }
    protected void FillPoly(Color32[] buffer, Vector2 bufferPosition, int bufferWidth, int bufferHeight, float zoom, IEnumerable points, Color32 color, bool invertY)
    {
        if (color.a == 0)
        {
            return;
        }
        float alpha = color.a / 255f;

        double minX, maxX, minY, maxY;

        double[] bufferPoints = GetBufferPoints(bufferPosition, zoom, points, out minX, out maxX, out minY, out maxY);

        if (maxX < 0 || minX > bufferWidth || maxY < 0 || minY > bufferHeight)
        {
            return;
        }

        double stX = minX;

        if (stX < 0)
        {
            stX = 0;
        }
        else if (stX > bufferWidth)
        {
            stX = bufferWidth;
        }

        double stY = minY;

        if (stY < 0)
        {
            stY = 0;
        }
        else if (stY > bufferHeight)
        {
            stY = bufferHeight;
        }

        double endX = maxX;

        if (endX < 0)
        {
            stX = 0;
        }
        else if (endX > bufferWidth)
        {
            endX = bufferWidth;
        }

        double endY = maxY;

        if (endY < 0)
        {
            endY = 0;
        }
        else if (endY > bufferHeight)
        {
            endY = bufferHeight;
        }

        int lengthX = (int)Math.Round(endX - stX);
        int lengthY = (int)Math.Round(endY - stY);

        Color32 clr = new Color32(color.r, color.g, color.b, 255);

        const int blockSize   = 5;
        int       blockCountX = lengthX / blockSize + (lengthX % blockSize == 0 ? 0 : 1);
        int       blockCountY = lengthY / blockSize + (lengthY % blockSize == 0 ? 0 : 1);

        byte clrR = clr.r;
        byte clrG = clr.g;
        byte clrB = clr.b;

        int istx = (int)Math.Round(stX);
        int isty = (int)Math.Round(stY);

        for (int by = 0; by < blockCountY; by++)
        {
            int    byp     = by * blockSize;
            double bufferY = byp + stY;
            int    iby     = byp + isty;

            for (int bx = 0; bx < blockCountX; bx++)
            {
                int    bxp     = bx * blockSize;
                double bufferX = bxp + stX;
                int    ibx     = bxp + istx;

                bool p1 = OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX, bufferY);
                bool p2 = OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX + blockSize - 1, bufferY);
                bool p3 = OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX + blockSize - 1, bufferY + blockSize - 1);
                bool p4 = OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX, bufferY + blockSize - 1);

                if (p1 && p2 && p3 && p4)
                {
                    for (int y = 0; y < blockSize; y++)
                    {
                        if (byp + y >= lengthY)
                        {
                            break;
                        }
                        int cby = iby + y;
                        if (!invertY)
                        {
                            cby = bufferHeight - cby - 1;
                        }
                        int byi = cby * bufferWidth + ibx;

                        for (int x = 0; x < blockSize; x++)
                        {
                            if (bxp + x >= lengthX)
                            {
                                break;
                            }

                            int bufferIndex = byi + x;

                            Color32 a = buffer[bufferIndex];
                            a.r = (byte)(a.r + (clrR - a.r) * alpha);
                            a.g = (byte)(a.g + (clrG - a.g) * alpha);
                            a.b = (byte)(a.b + (clrB - a.b) * alpha);
                            a.a = (byte)(a.a + (255 - a.a) * alpha);
                            buffer[bufferIndex] = a;
                        }
                    }
                }
                else if (!p1 && !p2 && !p3 && !p4)
                {
                }
                else
                {
                    for (int y = 0; y < blockSize; y++)
                    {
                        if (byp + y >= lengthY)
                        {
                            break;
                        }
                        int cby = iby + y;
                        if (!invertY)
                        {
                            cby = bufferHeight - cby - 1;
                        }
                        int byi = cby * bufferWidth + ibx;

                        for (int x = 0; x < blockSize; x++)
                        {
                            if (bxp + x >= lengthX)
                            {
                                break;
                            }

                            if (OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX + x, bufferY + y))
                            {
                                int     bufferIndex = byi + x;
                                Color32 a           = buffer[bufferIndex];
                                a.r = (byte)(a.r + (clrR - a.r) * alpha);
                                a.g = (byte)(a.g + (clrG - a.g) * alpha);
                                a.b = (byte)(a.b + (clrB - a.b) * alpha);
                                a.a = (byte)(a.a + (255 - a.a) * alpha);
                                buffer[bufferIndex] = a;
                            }
                        }
                    }
                }
            }
        }
    }
    protected void FillPoly(Color[] buffer, OnlineMapsVector2i bufferPosition, int bufferWidth, int bufferHeight,
                            int zoom, List <Vector2> points, Color color)
    {
        if (color.a == 0)
        {
            return;
        }

        List <Vector2> bufferPoints = new List <Vector2>();

        float minX = float.MaxValue;
        float maxX = float.MinValue;
        float minY = float.MaxValue;
        float maxY = float.MinValue;

        foreach (Vector2 point in points)
        {
            Vector2 bufferPoint = (OnlineMapsUtils.LatLongToTilef(point, zoom) - bufferPosition) * OnlineMapsUtils.tileSize;

            if (bufferPoint.x < minX)
            {
                minX = bufferPoint.x;
            }
            if (bufferPoint.x > maxX)
            {
                maxX = bufferPoint.x;
            }
            if (bufferPoint.y < minY)
            {
                minY = bufferPoint.y;
            }
            if (bufferPoint.y > maxY)
            {
                maxY = bufferPoint.y;
            }

            bufferPoints.Add(bufferPoint);
        }

        float stY  = Mathf.Clamp(minY, 0, bufferHeight);
        float stX  = Mathf.Clamp(minX, 0, bufferWidth);
        float endY = Mathf.Clamp(maxY, 0, bufferHeight);
        float endX = Mathf.Clamp(maxX, 0, bufferWidth);

        int     lengthX = Mathf.RoundToInt(endX - stX);
        int     lengthY = Mathf.RoundToInt(endY - stY);
        Vector2 start   = new Vector2(stX, stY);

        Color clr = new Color(color.r, color.g, color.b, 1);

        for (int y = 0; y < lengthY; y++)
        {
            float bufferY = y + start.y;
            for (int x = 0; x < lengthX; x++)
            {
                float bufferX = x + start.x;
                if (OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX, bufferY))
                {
                    int bufferIndex = (int)bufferY * bufferWidth + (int)bufferX;
                    if (color.a == 1)
                    {
                        buffer[bufferIndex] = color;
                    }
                    else
                    {
                        buffer[bufferIndex] = Color.Lerp(buffer[bufferIndex], clr, color.a);
                    }
                }
            }
        }
    }
 public override bool HitTest(Vector2 positionLatLng, int zoom)
 {
     return(OnlineMapsUtils.IsPointInPolygon(points, positionLatLng.x, positionLatLng.y));
 }
Esempio n. 6
0
    protected void FillPoly(Color32[] buffer, OnlineMapsVector2i bufferPosition, int bufferWidth, int bufferHeight, int zoom, IEnumerable points, Color32 color, bool invertY)
    {
        float alpha = color.a / 255f;

        if (color.a == 0)
        {
            return;
        }

        int countPoints = points.Cast <object>().Count();

        double[] bufferPoints = null;

        double minX = double.MaxValue;
        double maxX = double.MinValue;
        double minY = double.MaxValue;
        double maxY = double.MinValue;

        int    valueType = -1; // 0 - Vector2, 1 - float, 2 - double
        object v1 = null, v2 = null;

        int i = 0;

        foreach (object p in points)
        {
            if (valueType == -1)
            {
                if (p is Vector2)
                {
                    valueType    = 0;
                    bufferPoints = new double[countPoints * 2];
                }
                else if (p is float)
                {
                    valueType    = 1;
                    bufferPoints = new double[countPoints];
                }
                else if (p is double)
                {
                    valueType    = 2;
                    bufferPoints = new double[countPoints];
                }
            }

            v2 = v1;
            v1 = p;

            if (valueType == 0)
            {
                Vector2 point = (Vector2)p;
                double  tx, ty;
                api.projection.CoordinatesToTile(point.x, point.y, zoom, out tx, out ty);
                tx = (tx - bufferPosition.x) * OnlineMapsUtils.tileSize;
                ty = (ty - bufferPosition.y) * OnlineMapsUtils.tileSize;

                if (tx < minX)
                {
                    minX = tx;
                }
                if (tx > maxX)
                {
                    maxX = tx;
                }
                if (ty < minY)
                {
                    minY = ty;
                }
                if (ty > maxY)
                {
                    maxY = ty;
                }

                bufferPoints[i * 2]     = tx;
                bufferPoints[i * 2 + 1] = ty;
            }
            else if (i % 2 == 1)
            {
                double tx = 0, ty = 0;
                if (valueType == 1)
                {
                    api.projection.CoordinatesToTile((float)v2, (float)v1, zoom, out tx, out ty);
                }
                else if (valueType == 2)
                {
                    api.projection.CoordinatesToTile((double)v2, (double)v1, zoom, out tx, out ty);
                }
                tx = (tx - bufferPosition.x) * OnlineMapsUtils.tileSize;
                ty = (ty - bufferPosition.y) * OnlineMapsUtils.tileSize;

                if (tx < minX)
                {
                    minX = tx;
                }
                if (tx > maxX)
                {
                    maxX = tx;
                }
                if (ty < minY)
                {
                    minY = ty;
                }
                if (ty > maxY)
                {
                    maxY = ty;
                }

                bufferPoints[i - 1] = tx;
                bufferPoints[i]     = ty;
            }

            i++;
        }

        if (maxX < 0 || minX > bufferWidth || maxY < 0 || minY > bufferHeight)
        {
            return;
        }

        double stX = minX;

        if (stX < 0)
        {
            stX = 0;
        }
        else if (stX > bufferWidth)
        {
            stX = bufferWidth;
        }

        double stY = minY;

        if (stY < 0)
        {
            stY = 0;
        }
        else if (stY > bufferHeight)
        {
            stY = bufferHeight;
        }

        double endX = maxX;

        if (endX < 0)
        {
            stX = 0;
        }
        else if (endX > bufferWidth)
        {
            endX = bufferWidth;
        }

        double endY = maxY;

        if (endY < 0)
        {
            endY = 0;
        }
        else if (endY > bufferHeight)
        {
            endY = bufferHeight;
        }

        int lengthX = (int)Math.Round(endX - stX);
        int lengthY = (int)Math.Round(endY - stY);

        Color32 clr = new Color32(color.r, color.g, color.b, 255);

        const int blockSize   = 11;
        int       blockCountX = lengthX / blockSize + (lengthX % blockSize == 0 ? 0 : 1);
        int       blockCountY = lengthY / blockSize + (lengthY % blockSize == 0 ? 0 : 1);

        byte clrR = clr.r;
        byte clrG = clr.g;
        byte clrB = clr.b;

        int istx = (int)Math.Round(stX);
        int isty = (int)Math.Round(stY);

        for (int by = 0; by < blockCountY; by++)
        {
            int    byp     = by * blockSize;
            double bufferY = byp + stY;
            int    iby     = byp + isty;

            for (int bx = 0; bx < blockCountX; bx++)
            {
                int    bxp     = bx * blockSize;
                double bufferX = bxp + stX;
                int    ibx     = bxp + istx;

                bool p1 = OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX, bufferY);
                bool p2 = OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX + blockSize - 1, bufferY);
                bool p3 = OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX + blockSize - 1, bufferY + blockSize - 1);
                bool p4 = OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX, bufferY + blockSize - 1);

                if (p1 && p2 && p3 && p4)
                {
                    for (int y = 0; y < blockSize; y++)
                    {
                        if (byp + y >= lengthY)
                        {
                            break;
                        }
                        int cby = iby + y;
                        if (invertY)
                        {
                            cby = bufferHeight - cby - 1;
                        }
                        int byi = cby * bufferWidth + ibx;

                        for (int x = 0; x < blockSize; x++)
                        {
                            if (bxp + x >= lengthX)
                            {
                                break;
                            }

                            int bufferIndex = byi + x;

                            Color32 a = buffer[bufferIndex];
                            a.r = (byte)(a.r + (clrR - a.r) * alpha);
                            a.g = (byte)(a.g + (clrG - a.g) * alpha);
                            a.b = (byte)(a.b + (clrB - a.b) * alpha);
                            a.a = (byte)(a.a + (255 - a.a) * alpha);
                            buffer[bufferIndex] = a;
                        }
                    }
                }
                else if (!p1 && !p2 && !p3 && !p4)
                {
                }
                else
                {
                    for (int y = 0; y < blockSize; y++)
                    {
                        if (byp + y >= lengthY)
                        {
                            break;
                        }
                        int cby = iby + y;
                        if (invertY)
                        {
                            cby = bufferHeight - cby - 1;
                        }
                        int byi = cby * bufferWidth + ibx;

                        for (int x = 0; x < blockSize; x++)
                        {
                            if (bxp + x >= lengthX)
                            {
                                break;
                            }

                            if (OnlineMapsUtils.IsPointInPolygon(bufferPoints, bufferX + x, bufferY + y))
                            {
                                int     bufferIndex = byi + x;
                                Color32 a           = buffer[bufferIndex];
                                a.r = (byte)(a.r + (clrR - a.r) * alpha);
                                a.g = (byte)(a.g + (clrG - a.g) * alpha);
                                a.b = (byte)(a.b + (clrB - a.b) * alpha);
                                a.a = (byte)(a.a + (255 - a.a) * alpha);
                                buffer[bufferIndex] = a;
                            }
                        }
                    }
                }
            }
        }
    }