public void Update() { double oldMercatorToViewScale = _mercatorToViewScale; Vector oldTranslation = _translation; _mercatorToViewScale = 256 * Math.Pow(2, ZoomLevel) / MercatorLocation.Width; var offset = new Vector { X = Width / _mercatorToViewScale / 2, Y = -Height / _mercatorToViewScale / 2 }; _translation = new Vector { X = -Position.X + offset.X, Y = -Position.Y + offset.Y }; _viewToMercatorScale = 1 / _mercatorToViewScale; _visibleMercatorRectangle = ToScene(VisibleViewRectangle); if (oldMercatorToViewScale != _mercatorToViewScale || oldTranslation != _translation) { EmitChanged(); } }
public Rect ToView(MercatorRectangle rect) { Point topLeft = ToView(new MercatorLocation(rect.Min.X, rect.Max.Y)); Point bottomRight = ToView(new MercatorLocation(rect.Max.X, rect.Min.Y)); double x = Math.Min(topLeft.X, bottomRight.X); double y = Math.Min(topLeft.Y, bottomRight.Y); double width = Math.Max(topLeft.X, bottomRight.X) - x; double height = Math.Max(topLeft.Y, bottomRight.Y) - y; return(new Rect { X = x, Y = y, Width = width, Height = height }); }
/// <summary> /// </summary> /// <param name="rectangle"></param> /// <param name="zoomLevel"></param> /// <returns></returns> public static TileRectangle CreateFrom(MercatorRectangle rectangle, int zoomLevel) { var zoomFactor = Math.Pow(x: 2, y: zoomLevel); var tileEdgeLength = MercatorRectangle.Earth.Width / zoomFactor; // Given zoom level = 1 // (X: 0, Y: 0) is top left // (X: 1, Y: 0) is top right // (X: 1, Y: 1) is bottom right // => We can interpolate var xDifference = rectangle.Min.X - MercatorRectangle.Earth.Min.X; var yDifference = rectangle.Min.Y - MercatorRectangle.Earth.Min.Y; var x = (int)(xDifference / tileEdgeLength); var y = (int)(yDifference / tileEdgeLength); var width = (int)Math.Ceiling(rectangle.Width / tileEdgeLength); var height = (int)Math.Ceiling(rectangle.Height / tileEdgeLength); return(new TileRectangle(x, y, zoomLevel, width, height)); }