2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2014-01-10 20:11:39 +01:00
|
|
|
|
// Copyright © 2014 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
#if WINDOWS_RUNTIME
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
internal static partial class Extensions
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
public static Matrix Translate(this Matrix matrix, double offsetX, double offsetY)
|
|
|
|
|
|
{
|
|
|
|
|
|
matrix.OffsetX += offsetX;
|
|
|
|
|
|
matrix.OffsetY += offsetY;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
return matrix;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix Scale(this Matrix matrix, double scaleX, double scaleY)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Multiply(matrix, new Matrix(scaleX, 0d, 0d, scaleY, 0d, 0d));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix Rotate(this Matrix matrix, double angle)
|
|
|
|
|
|
{
|
|
|
|
|
|
angle = (angle % 360d) / 180d * Math.PI;
|
|
|
|
|
|
var cos = Math.Cos(angle);
|
|
|
|
|
|
var sin = Math.Sin(angle);
|
2014-07-01 18:57:44 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
return Multiply(matrix, new Matrix(cos, sin, -sin, cos, 0d, 0d));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix RotateAt(this Matrix matrix, double angle, double centerX, double centerY)
|
|
|
|
|
|
{
|
|
|
|
|
|
angle = (angle % 360d) / 180d * Math.PI;
|
|
|
|
|
|
var cos = Math.Cos(angle);
|
|
|
|
|
|
var sin = Math.Sin(angle);
|
|
|
|
|
|
var offsetX = centerX * (1d - cos) + centerY * sin;
|
|
|
|
|
|
var offsetY = centerY * (1d - cos) - centerX * sin;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
return Multiply(matrix, new Matrix(cos, sin, -sin, cos, offsetX, offsetY));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix Invert(this Matrix matrix)
|
|
|
|
|
|
{
|
|
|
|
|
|
var determinant = matrix.M11 * matrix.M22 - matrix.M12 * matrix.M21;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
return new Matrix(
|
|
|
|
|
|
matrix.M22 / determinant, -matrix.M12 / determinant,
|
|
|
|
|
|
-matrix.M21 / determinant, matrix.M11 / determinant,
|
|
|
|
|
|
(matrix.M21 * matrix.OffsetY - matrix.M22 * matrix.OffsetX) / determinant,
|
|
|
|
|
|
(matrix.M12 * matrix.OffsetX - matrix.M11 * matrix.OffsetY) / determinant);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix Multiply(this Matrix matrix1, Matrix matrix2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Matrix(
|
|
|
|
|
|
matrix1.M11 * matrix2.M11 + matrix1.M12 * matrix2.M21,
|
|
|
|
|
|
matrix1.M11 * matrix2.M12 + matrix1.M12 * matrix2.M22,
|
|
|
|
|
|
matrix1.M21 * matrix2.M11 + matrix1.M22 * matrix2.M21,
|
|
|
|
|
|
matrix1.M21 * matrix2.M12 + matrix1.M22 * matrix2.M22,
|
|
|
|
|
|
(matrix2.M11 * matrix1.OffsetX + matrix2.M21 * matrix1.OffsetY) + matrix2.OffsetX,
|
|
|
|
|
|
(matrix2.M12 * matrix1.OffsetX + matrix2.M22 * matrix1.OffsetY) + matrix2.OffsetY);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|