MockStreamGeometryImpl.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, ITransformedGeometryImpl
  8. {
  9. private MockStreamGeometryContext _context;
  10. public MockStreamGeometryImpl()
  11. {
  12. Transform = Matrix.Identity;
  13. _context = new MockStreamGeometryContext();
  14. }
  15. public MockStreamGeometryImpl(Matrix transform)
  16. {
  17. Transform = transform;
  18. _context = new MockStreamGeometryContext();
  19. }
  20. private MockStreamGeometryImpl(Matrix transform, MockStreamGeometryContext context)
  21. {
  22. Transform = transform;
  23. _context = context;
  24. }
  25. public IGeometryImpl SourceGeometry { get; }
  26. public Rect Bounds => _context.CalculateBounds();
  27. public double ContourLength { get; }
  28. public Matrix Transform { get; }
  29. public IStreamGeometryImpl Clone()
  30. {
  31. return this;
  32. }
  33. public void Dispose()
  34. {
  35. }
  36. public bool FillContains(Point point)
  37. {
  38. return _context.FillContains(point);
  39. }
  40. public bool StrokeContains(IPen pen, Point point)
  41. {
  42. return false;
  43. }
  44. public Rect GetRenderBounds(IPen pen) => Bounds;
  45. public IGeometryImpl Intersect(IGeometryImpl geometry)
  46. {
  47. return new MockStreamGeometryImpl(Transform);
  48. }
  49. public IStreamGeometryContextImpl Open()
  50. {
  51. return _context;
  52. }
  53. public ITransformedGeometryImpl WithTransform(Matrix transform)
  54. {
  55. return new MockStreamGeometryImpl(transform, _context);
  56. }
  57. public bool TryGetPointAtDistance(double distance, out Point point)
  58. {
  59. point = new Point();
  60. return false;
  61. }
  62. public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent)
  63. {
  64. point = new Point();
  65. tangent = new Point();
  66. return false;
  67. }
  68. public bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry)
  69. {
  70. segmentGeometry = null;
  71. return false;
  72. }
  73. class MockStreamGeometryContext : IStreamGeometryContextImpl
  74. {
  75. private List<Point> points = new List<Point>();
  76. public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
  77. {
  78. }
  79. public void BeginFigure(Point startPoint, bool isFilled)
  80. {
  81. points.Add(startPoint);
  82. }
  83. public Rect CalculateBounds()
  84. {
  85. var left = double.MaxValue;
  86. var right = double.MinValue;
  87. var top = double.MaxValue;
  88. var bottom = double.MinValue;
  89. foreach (var p in points)
  90. {
  91. left = Math.Min(p.X, left);
  92. right = Math.Max(p.X, right);
  93. top = Math.Min(p.Y, top);
  94. bottom = Math.Max(p.Y, bottom);
  95. }
  96. return new Rect(new Point(left, top), new Point(right, bottom));
  97. }
  98. public void CubicBezierTo(Point point1, Point point2, Point point3)
  99. {
  100. }
  101. public void Dispose()
  102. {
  103. }
  104. public void EndFigure(bool isClosed)
  105. {
  106. }
  107. public void LineTo(Point point)
  108. {
  109. points.Add(point);
  110. }
  111. public void QuadraticBezierTo(Point control, Point endPoint)
  112. {
  113. throw new NotImplementedException();
  114. }
  115. public void SetFillRule(FillRule fillRule)
  116. {
  117. }
  118. public bool FillContains(Point point)
  119. {
  120. // Use the algorithm from https://www.blackpawn.com/texts/pointinpoly/default.html
  121. // to determine if the point is in the geometry (since it will always be convex in this situation)
  122. for (int i = 0; i < points.Count; i++)
  123. {
  124. var a = points[i];
  125. var b = points[(i + 1) % points.Count];
  126. var c = points[(i + 2) % points.Count];
  127. Vector v0 = c - a;
  128. Vector v1 = b - a;
  129. Vector v2 = point - a;
  130. var dot00 = v0 * v0;
  131. var dot01 = v0 * v1;
  132. var dot02 = v0 * v2;
  133. var dot11 = v1 * v1;
  134. var dot12 = v1 * v2;
  135. var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
  136. var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  137. var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  138. if ((u >= 0) && (v >= 0) && (u + v < 1)) return true;
  139. }
  140. return false;
  141. }
  142. }
  143. }
  144. }