LineGeometry.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Perspex.Platform;
  4. namespace Perspex.Media
  5. {
  6. /// <summary>
  7. /// Represents the geometry of a line.
  8. /// </summary>
  9. public class LineGeometry : Geometry
  10. {
  11. private Point _startPoint;
  12. private Point _endPoint;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="LineGeometry"/> class.
  15. /// </summary>
  16. /// <param name="startPoint">The start point.</param>
  17. /// <param name="endPoint">The end point.</param>
  18. public LineGeometry(Point startPoint, Point endPoint)
  19. {
  20. _startPoint = startPoint;
  21. _endPoint = endPoint;
  22. IPlatformRenderInterface factory = PerspexLocator.Current.GetService<IPlatformRenderInterface>();
  23. IStreamGeometryImpl impl = factory.CreateStreamGeometry();
  24. using (IStreamGeometryContextImpl context = impl.Open())
  25. {
  26. context.BeginFigure(_startPoint, false);
  27. context.LineTo(_endPoint);
  28. context.EndFigure(false);
  29. }
  30. PlatformImpl = impl;
  31. }
  32. /// <inheritdoc/>
  33. public override Rect Bounds
  34. {
  35. get
  36. {
  37. double xMin, yMin, xMax, yMax;
  38. if (_startPoint.X <= _endPoint.X)
  39. {
  40. xMin = _startPoint.X;
  41. xMax = _endPoint.X;
  42. }
  43. else
  44. {
  45. xMin = _endPoint.X;
  46. xMax = _startPoint.X;
  47. }
  48. if (_startPoint.Y <= _endPoint.Y)
  49. {
  50. yMin = _startPoint.Y;
  51. yMax = _endPoint.Y;
  52. }
  53. else
  54. {
  55. yMin = _endPoint.Y;
  56. yMax = _startPoint.Y;
  57. }
  58. return new Rect(xMin, yMin, xMax - xMin, yMax - yMin);
  59. }
  60. }
  61. /// <inheritdoc/>
  62. public override Geometry Clone()
  63. {
  64. return new LineGeometry(_startPoint, _endPoint);
  65. }
  66. }
  67. }