Guest User

Untitled

a guest
Oct 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. Public Class Triangle
  2.  
  3. Public A As PointF
  4. Public B As PointF
  5. Public C As PointF
  6.  
  7. ''' <summary>
  8. '''
  9. ''' </summary>
  10. ''' <param name="sideA">The length of the known side BC</param>
  11. ''' <param name="sideB">The length of the known side AC</param>
  12. ''' <param name="sideC">The length of the known side AB</param>
  13. ''' <param name="angleA">The internal angle in Radians at vertex A</param>
  14. ''' <param name="angleB">The internal angle in Radians at vertex B</param>
  15. ''' <param name="angleC">The internal angle in Radians at vertex C</param>
  16. ''' <remarks></remarks>
  17. Public Sub New(ByVal sideA As Decimal, ByVal sideB As Decimal, ByVal sideC As Decimal, ByVal angleA As Decimal, ByVal angleB As Decimal, ByVal angleC As Decimal)
  18.  
  19. Dim bX As Decimal = CDec(Math.Cos(angleA) * sideC)
  20. Dim bY As Decimal = CDec(Math.Sin(angleA) * sideC)
  21.  
  22. Me.A = PointF.Empty
  23. Me.C = PointF.Add(A, New SizeF(sideB, 0))
  24. Me.B = New PointF(bX, -bY)
  25.  
  26. If bX < 0 Then
  27. Me.A = PointF.Add(Me.A, New SizeF(-bX, 0))
  28. Me.B = PointF.Add(Me.B, New SizeF(-bX, 0))
  29. Me.C = PointF.Add(Me.C, New SizeF(-bX, 0))
  30. End If
  31.  
  32. End Sub
  33.  
  34. Public Sub ScaleToFit(ByVal maxWidthOrHeight As Decimal)
  35. Dim xCoords() As Single = {Me.A.X, Me.B.X, Me.C.X}
  36.  
  37. Dim OverallWidth As Decimal = CDec(xCoords.Max - xCoords.Min)
  38. Dim OverallHeight As Decimal = CDec(Math.Abs(Me.B.Y)) 'B.Y is negative owing to GDI+ coordinates
  39.  
  40. Dim scaleFactor As Decimal = If(OverallWidth > OverallHeight, _
  41. maxWidthOrHeight / OverallWidth, _
  42. maxWidthOrHeight / OverallHeight)
  43. Scale(scaleFactor)
  44. centreTriangle(25, 300)
  45. End Sub
  46.  
  47. Private Sub Scale(ByVal scaleFactor As Decimal)
  48. Me.A = ScalePointF(Me.A, scaleFactor)
  49. Me.B = ScalePointF(Me.B, scaleFactor)
  50. Me.C = ScalePointF(Me.C, scaleFactor)
  51. End Sub
  52.  
  53. Private Function ScalePointF(ByVal pf As PointF, ByVal factor As Decimal) As PointF
  54. Return New PointF(pf.X * factor, pf.Y * factor)
  55. End Function
  56.  
  57. Private Sub centreTriangle(ByVal border As Integer, ByVal displaySize As Integer)
  58. If B.Y > A.Y Then B.Y -= ((B.Y - A.Y) * 2)
  59. Dim pts() As PointF = New PointF() {A, B, C}
  60. Dim offset_X As Integer = pts.Min(Function(p) CInt(p.X)) - border
  61. Dim offset_Y As Integer = pts.Max(Function(p) CInt(p.Y)) - (displaySize - border)
  62. A = New PointF(A.X - offset_X, A.Y - offset_Y)
  63. B = New PointF(B.X - offset_X, B.Y - offset_Y)
  64. C = New PointF(C.X - offset_X, C.Y - offset_Y)
  65. End Sub
  66.  
  67. End Class
  68.  
  69. public class Triangle
  70. {
  71.  
  72. private Point A;
  73. private Point B;
  74. private Point C;
  75.  
  76. /**
  77. * @return Point A
  78. */
  79. public Point getA()
  80. {
  81. return this.A;
  82. }
  83.  
  84. /**
  85. * @return Point B
  86. */
  87. public Point getB()
  88. {
  89. return this.B;
  90. }
  91.  
  92. /**
  93. * @return Point C
  94. */
  95. public Point getC()
  96. {
  97. return this.C;
  98. }
  99.  
  100. /**
  101.  
  102.  
  103. @param sideA The length of the known side BC
  104. @param sideB The length of the known side AC
  105. @param sideC The length of the known side AB
  106. @param angleA The internal angle in Radians at vertex A
  107. @param angleB The internal angle in Radians at vertex B
  108. @param angleC The internal angle in Radians at vertex C
  109.  
  110. */
  111. public Triangle(float sideA, float sideB, float sideC, float angleA, float angleB, float angleC)
  112. {
  113.  
  114. float bX = (float)(Math.cos(angleA) * sideC);
  115. float bY = (float)(Math.sin(angleA) * sideC);
  116.  
  117. this.A = new Point(0, 0);
  118. this.C = new Point((int)(this.A.getX() + sideB), (int)this.A.getY());
  119. this.B = new Point((int)bX, (int)-bY);
  120.  
  121. if (bX < 0)
  122. {
  123. this.A = new Point((int)(this.A.getX() - bX), (int)this.A.getY());
  124. this.B = new Point((int)(this.B.getX() - bX), (int)this.B.getY());
  125. this.C = new Point((int)(this.C.getX() - bX), (int)this.C.getY());
  126. }
  127.  
  128. }
  129.  
  130. public final void ScaleToFit(float maxWidthOrHeight)
  131. {
  132. float[] xCoords = {(float)this.getA().getX(), (float)this.getB().getX(), (float)this.getC().getX()};
  133. float min = 10000;
  134. float max = -1;
  135.  
  136. for(int x = 0; x < 3; x++) {
  137. if(xCoords[x] < min) {min = xCoords[x];}
  138. if(xCoords[x] > max) {max = xCoords[x];}
  139. }
  140.  
  141. float OverallWidth = (float)(max - min);
  142. float OverallHeight = (float)Math.abs(this.getB().getY()); //B.Y is negative owing to Graphics coordinates
  143.  
  144. float scaleFactor = (float)(OverallWidth > OverallHeight ? maxWidthOrHeight / OverallWidth : maxWidthOrHeight / OverallHeight);
  145. Scale(scaleFactor);
  146. centreTriangle(25, 300);
  147. }
  148.  
  149. private void Scale(float scaleFactor)
  150. {
  151. this.A = ScalePoint(this.A, scaleFactor);
  152. this.B = ScalePoint(this.B, scaleFactor);
  153. this.C = ScalePoint(this.C, scaleFactor);
  154. }
  155.  
  156. private Point ScalePoint(Point p, float factor)
  157. {
  158. return new Point((int)(p.getX() * factor), (int)(p.getY() * factor));
  159. }
  160.  
  161. private void centreTriangle(int border, int displaySize)
  162. {
  163. int y1 = (int)this.A.getY();
  164. int y2 = (int)this.B.getY();
  165. if(y2 > y1) {y2 -= ((y2 - y1) * 2);}
  166. this.B = new Point((int)this.B.getX(), y2);
  167.  
  168. Point[] pts = new Point[] {this.A, this.B, this.C};
  169. float min = 10000;
  170. float max = -1;
  171.  
  172. for(int x = 0; x < 3; x++) {
  173. if(pts[x].getX() < min) {min = (float)pts[x].getX();}
  174. if(pts[x].getY() > max) {max = (float)pts[x].getY();}
  175. }
  176.  
  177. int offset_X = (int)(min - border);
  178. int offset_Y = (int)(max - (displaySize - border));
  179. this.A = new Point((int)(this.A.getX() - offset_X), (int)(this.A.getY() - offset_Y));
  180. this.B = new Point((int)(this.B.getX() - offset_X), (int)(this.B.getY() - offset_Y));
  181. this.C = new Point((int)(this.C.getX() - offset_X), (int)(this.C.getY() - offset_Y));
  182. }
  183.  
  184. }
Add Comment
Please, Sign In to add comment