MockStreamGeometryImpl.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Media;
  4. using Avalonia.Platform;
  5. namespace Avalonia.UnitTests
  6. {
  7. public class MockStreamGeometryImpl : IStreamGeometryImpl
  8. {
  9. private MockStreamGeometryContext _impl = new MockStreamGeometryContext();
  10. public Rect Bounds => _impl.CalculateBounds();
  11. public Matrix Transform { get; set; }
  12. public IStreamGeometryImpl Clone()
  13. {
  14. return this;
  15. }
  16. public bool FillContains(Point point)
  17. {
  18. return _impl.FillContains(point);
  19. }
  20. public bool StrokeContains(Pen pen, Point point)
  21. {
  22. return false;
  23. }
  24. public Rect GetRenderBounds(double strokeThickness) => Bounds;
  25. public IStreamGeometryContextImpl Open()
  26. {
  27. return _impl;
  28. }
  29. public IGeometryImpl WithTransform(Matrix transform)
  30. {
  31. return this;
  32. }
  33. class MockStreamGeometryContext : IStreamGeometryContextImpl
  34. {
  35. private List<Point> points = new List<Point>();
  36. public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
  37. {
  38. throw new NotImplementedException();
  39. }
  40. public void BeginFigure(Point startPoint, bool isFilled)
  41. {
  42. points.Add(startPoint);
  43. }
  44. public Rect CalculateBounds()
  45. {
  46. var left = double.MaxValue;
  47. var right = double.MinValue;
  48. var top = double.MaxValue;
  49. var bottom = double.MinValue;
  50. foreach (var p in points)
  51. {
  52. left = Math.Min(p.X, left);
  53. right = Math.Max(p.X, right);
  54. top = Math.Min(p.Y, top);
  55. bottom = Math.Max(p.Y, bottom);
  56. }
  57. return new Rect(new Point(left, top), new Point(right, bottom));
  58. }
  59. public void CubicBezierTo(Point point1, Point point2, Point point3)
  60. {
  61. throw new NotImplementedException();
  62. }
  63. public void Dispose()
  64. {
  65. }
  66. public void EndFigure(bool isClosed)
  67. {
  68. }
  69. public void LineTo(Point point)
  70. {
  71. points.Add(point);
  72. }
  73. public void QuadraticBezierTo(Point control, Point endPoint)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. public void SetFillRule(FillRule fillRule)
  78. {
  79. }
  80. public bool FillContains(Point point)
  81. {
  82. // Use the algorithm from http://www.blackpawn.com/texts/pointinpoly/default.html
  83. // to determine if the point is in the geometry (since it will always be convex in this situation)
  84. for (int i = 0; i < points.Count; i++)
  85. {
  86. var a = points[i];
  87. var b = points[(i + 1) % points.Count];
  88. var c = points[(i + 2) % points.Count];
  89. Vector v0 = c - a;
  90. Vector v1 = b - a;
  91. Vector v2 = point - a;
  92. var dot00 = v0 * v0;
  93. var dot01 = v0 * v1;
  94. var dot02 = v0 * v2;
  95. var dot11 = v1 * v1;
  96. var dot12 = v1 * v2;
  97. var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
  98. var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  99. var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  100. if ((u >= 0) && (v >= 0) && (u + v < 1)) return true;
  101. }
  102. return false;
  103. }
  104. }
  105. }
  106. }