Extensions.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993] for details.
  4. // All other rights reserved.
  5. using System;
  6. using System.Windows;
  7. using System.Windows.Media;
  8. namespace System.Windows.Controls
  9. {
  10. /// <summary>
  11. /// This set of internal extension methods provide general solutions and
  12. /// utilities in a small enough number to not warrant a dedicated extension
  13. /// methods class.
  14. /// </summary>
  15. internal static partial class Extensions
  16. {
  17. /// <summary>
  18. /// Inverts a Matrix. The Invert functionality on the Matrix type is
  19. /// internal to the framework only. Since Matrix is a struct, an out
  20. /// parameter must be presented.
  21. /// </summary>
  22. /// <param name="m">The Matrix object.</param>
  23. /// <param name="outputMatrix">The matrix to return by an output
  24. /// parameter.</param>
  25. /// <returns>Returns a value indicating whether the type was
  26. /// successfully inverted. If the determinant is 0.0, then it cannot
  27. /// be inverted and the original instance will remain untouched.</returns>
  28. public static bool Invert(this Matrix m, out Matrix outputMatrix)
  29. {
  30. double determinant = m.M11 * m.M22 - m.M12 * m.M21;
  31. if (determinant == 0.0)
  32. {
  33. outputMatrix = m;
  34. return false;
  35. }
  36. Matrix matCopy = m;
  37. m.M11 = matCopy.M22 / determinant;
  38. m.M12 = -1 * matCopy.M12 / determinant;
  39. m.M21 = -1 * matCopy.M21 / determinant;
  40. m.M22 = matCopy.M11 / determinant;
  41. m.OffsetX = (matCopy.OffsetY * matCopy.M21 - matCopy.OffsetX * matCopy.M22) / determinant;
  42. m.OffsetY = (matCopy.OffsetX * matCopy.M12 - matCopy.OffsetY * matCopy.M11) / determinant;
  43. outputMatrix = m;
  44. return true;
  45. }
  46. /// <summary>
  47. /// An implementation of the Contains member of string that takes in a
  48. /// string comparison. The traditional .NET string Contains member uses
  49. /// StringComparison.Ordinal.
  50. /// </summary>
  51. /// <param name="s">The string.</param>
  52. /// <param name="value">The string value to search for.</param>
  53. /// <param name="comparison">The string comparison type.</param>
  54. /// <returns>Returns true when the substring is found.</returns>
  55. public static bool Contains(this string s, string value, StringComparison comparison)
  56. {
  57. return s.IndexOf(value, comparison) >= 0;
  58. }
  59. }
  60. }