GR32_ArrowHeads.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. unit GR32_ArrowHeads;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Vectorial Polygon Rasterizer for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Angus Johnson < http://www.angusj.com >
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2012
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. uses
  35. SysUtils, GR32, GR32_Polygons, GR32_VectorUtils, GR32_Geometry;
  36. type
  37. TArrowHeadAbstract = class
  38. private
  39. FSize: TFloat;
  40. FTipPoint: TFloatPoint;
  41. FBasePoint: TFloatPoint;
  42. protected
  43. function GetPointsInternal: TArrayOfFloatPoint; virtual; abstract;
  44. public
  45. constructor Create(size: TFloat); virtual;
  46. function GetPoints(const Line: TArrayOfFloatPoint; AtEnd: Boolean): TArrayOfFloatPoint;
  47. //Size: distance between arrow tip and arrow base
  48. property Size: TFloat read FSize write FSize;
  49. end;
  50. TArrowHeadSimple = class(TArrowHeadAbstract)
  51. protected
  52. function GetPointsInternal: TArrayOfFloatPoint; override;
  53. end;
  54. TArrowHeadFourPt = class(TArrowHeadAbstract)
  55. protected
  56. function GetPointsInternal: TArrayOfFloatPoint; override;
  57. end;
  58. TArrowHeadCircle = class(TArrowHeadAbstract)
  59. protected
  60. function GetPointsInternal: TArrayOfFloatPoint; override;
  61. end;
  62. TArrowHeadDiamond = class(TArrowHeadAbstract)
  63. protected
  64. function GetPointsInternal: TArrayOfFloatPoint; override;
  65. end;
  66. resourcestring
  67. RCStrInsufficientPointsInArray = 'Insufficient points in array';
  68. implementation
  69. constructor TArrowHeadAbstract.Create(Size: TFloat);
  70. begin
  71. FSize := Size;
  72. end;
  73. //------------------------------------------------------------------------------
  74. function TArrowHeadAbstract.GetPoints(const Line: TArrayOfFloatPoint;
  75. AtEnd: Boolean): TArrayOfFloatPoint;
  76. var
  77. HighI: Integer;
  78. UnitVec: TFloatPoint;
  79. begin
  80. HighI := high(Line);
  81. if HighI < 1 then
  82. raise exception.create(RCStrInsufficientPointsInArray);
  83. if AtEnd then
  84. begin
  85. FBasePoint := Line[HighI];
  86. UnitVec := GetUnitVector(Line[HighI -1], Line[HighI]);
  87. end else
  88. begin
  89. FBasePoint := Line[0];
  90. UnitVec := GetUnitVector(Line[1], Line[0]);
  91. end;
  92. FTipPoint := OffsetPoint(FBasePoint, UnitVec.X * FSize, UnitVec.Y * FSize);
  93. Result := GetPointsInternal;
  94. end;
  95. //------------------------------------------------------------------------------
  96. function TArrowHeadSimple.GetPointsInternal: TArrayOfFloatPoint;
  97. var
  98. UnitNorm: TFloatPoint;
  99. Sz: Single;
  100. begin
  101. SetLength(Result, 3);
  102. UnitNorm := GetUnitNormal(FTipPoint, FBasePoint);
  103. Sz := FSize * 0.5;
  104. Result[0] := FTipPoint;
  105. Result[1] := OffsetPoint(FBasePoint, UnitNorm.X *Sz, UnitNorm.Y *Sz);
  106. Result[2] := OffsetPoint(FBasePoint, -UnitNorm.X *Sz, -UnitNorm.Y *Sz);
  107. end;
  108. //------------------------------------------------------------------------------
  109. function TArrowHeadFourPt.GetPointsInternal: TArrayOfFloatPoint;
  110. var
  111. Angle: Double;
  112. begin
  113. SetLength(Result, 4);
  114. Result[0] := FTipPoint;
  115. Angle := GetAngleOfPt2FromPt1(FTipPoint, FBasePoint);
  116. Result[1] := GetPointAtAngleFromPoint(FBasePoint, FSize * 0.5, Angle + CRad60);
  117. Result[2] := FBasePoint;
  118. Result[3] := GetPointAtAngleFromPoint(FBasePoint, FSize * 0.5, Angle - CRad60);
  119. end;
  120. //------------------------------------------------------------------------------
  121. function TArrowHeadCircle.GetPointsInternal: TArrayOfFloatPoint;
  122. var
  123. MidPt: TFloatPoint;
  124. begin
  125. MidPt := Average(FTipPoint, FBasePoint);
  126. Result := Circle(MidPt.X, MidPt.Y, FSize * 0.5, Round(FSize));
  127. end;
  128. //------------------------------------------------------------------------------
  129. function TArrowHeadDiamond.GetPointsInternal: TArrayOfFloatPoint;
  130. var
  131. MidPt, UnitNorm: TFloatPoint;
  132. Sz: Single;
  133. begin
  134. MidPt := Average(FTipPoint, FBasePoint);
  135. UnitNorm := GetUnitNormal(FTipPoint, FBasePoint);
  136. Sz := FSize / 3;
  137. SetLength(Result, 4);
  138. Result[0] := FTipPoint;
  139. Result[1] := OffsetPoint(MidPt, UnitNorm.X * Sz, UnitNorm.Y * Sz);
  140. Result[2] := FBasePoint;
  141. Result[3] := OffsetPoint(MidPt, -UnitNorm.X * Sz, -UnitNorm.Y * Sz);
  142. end;
  143. //------------------------------------------------------------------------------
  144. end.