1

Topic: Rotate clockwise Rect and Poly (OnlineMapsDrawingRect/Poly)

Hi Alex,

great work on this Plugin!

Can you add a rotation function for both OnlineMapsDrawingElements please?

Many thanks!

Re: Rotate clockwise Rect and Poly (OnlineMapsDrawingRect/Poly)

Hi.

I don't plan to add rotation to these objects.
Below is an example of how you can rotate polygons.
You can rotate rectangles by making it a polygon.

using UnityEngine;

namespace InfinityCode.OnlineMapsSupport
{
    public class RotatePoly : MonoBehaviour
    {
        public OnlineMaps map;
        public float angle;

        private float _angle;
        private OnlineMapsVector2d[] originalPoints;
        private OnlineMapsDrawingPoly poly;

        private void Start()
        {
            if (map == null) map = OnlineMaps.instance;
        
            originalPoints = new[]
            {
                new OnlineMapsVector2d(0, 0),
                new OnlineMapsVector2d(10, 0),
                new OnlineMapsVector2d(10, 10),
                new OnlineMapsVector2d(0, 10)
            };

            OnlineMapsVector2d[] rotatedPoints = new OnlineMapsVector2d[originalPoints.Length];
            RotatedPoints(rotatedPoints);
        
            poly = new OnlineMapsDrawingPoly(rotatedPoints, Color.red, 2);
            map.drawingElementManager.Add(poly);
        }

        private void RotatedPoints(OnlineMapsVector2d[] rotatedPoints)
        {
            _angle = angle;
        
            float angleRad = Mathf.Repeat(angle, 360) * Mathf.Deg2Rad;
            OnlineMapsProjection projection = map.projection;
            double[] mercatorPoints = new double[originalPoints.Length * 2];

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

            for (int i = 0; i < originalPoints.Length; i++)
            {
                OnlineMapsVector2d p = originalPoints[i];
                double x, y;
                projection.CoordinatesToTile(p.x, p.y, 0, out x, out y);
            
                mercatorPoints[i * 2] = x;
                mercatorPoints[i * 2 + 1] = y;
            
                if (x < minX) minX = x;
                if (x > maxX) maxX = x;
                if (y < minY) minY = y;
                if (y > maxY) maxY = y;
            }
        
            double cx = (minX + maxX) / 2;
            double cy = (minY + maxY) / 2;
        
            OnlineMapsVector2d rp = new OnlineMapsVector2d();
        
            for (int i = 0; i < originalPoints.Length; i++)
            {
                double x = mercatorPoints[i * 2] - cx;
                double y = mercatorPoints[i * 2 + 1] - cy;
            
                double x2 = x * Mathf.Cos(angleRad) - y * Mathf.Sin(angleRad);
                double y2 = x * Mathf.Sin(angleRad) + y * Mathf.Cos(angleRad);
            
                x = x2 + cx;
                y = y2 + cy;
            
                projection.TileToCoordinates(x, y, 0, out rp.x, out rp.y);
                rotatedPoints[i] = rp;
            }
        }

        private void Update()
        {
            if (angle != _angle) UpdatePoints();
        }

        private void UpdatePoints()
        {
            OnlineMapsVector2d[] rotatedPoints = poly.points as OnlineMapsVector2d[];
            RotatedPoints(rotatedPoints);
            poly.points = rotatedPoints;
        }
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.