Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Triangle
- Public A As PointF
- Public B As PointF
- Public C As PointF
- ''' <summary>
- '''
- ''' </summary>
- ''' <param name="sideA">The length of the known side BC</param>
- ''' <param name="sideB">The length of the known side AC</param>
- ''' <param name="sideC">The length of the known side AB</param>
- ''' <param name="angleA">The internal angle in Radians at vertex A</param>
- ''' <param name="angleB">The internal angle in Radians at vertex B</param>
- ''' <param name="angleC">The internal angle in Radians at vertex C</param>
- ''' <remarks></remarks>
- 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)
- Dim bX As Decimal = CDec(Math.Cos(angleA) * sideC)
- Dim bY As Decimal = CDec(Math.Sin(angleA) * sideC)
- Me.A = PointF.Empty
- Me.C = PointF.Add(A, New SizeF(sideB, 0))
- Me.B = New PointF(bX, -bY)
- If bX < 0 Then
- Me.A = PointF.Add(Me.A, New SizeF(-bX, 0))
- Me.B = PointF.Add(Me.B, New SizeF(-bX, 0))
- Me.C = PointF.Add(Me.C, New SizeF(-bX, 0))
- End If
- End Sub
- Public Sub ScaleToFit(ByVal maxWidthOrHeight As Decimal)
- Dim xCoords() As Single = {Me.A.X, Me.B.X, Me.C.X}
- Dim OverallWidth As Decimal = CDec(xCoords.Max - xCoords.Min)
- Dim OverallHeight As Decimal = CDec(Math.Abs(Me.B.Y)) 'B.Y is negative owing to GDI+ coordinates
- Dim scaleFactor As Decimal = If(OverallWidth > OverallHeight, _
- maxWidthOrHeight / OverallWidth, _
- maxWidthOrHeight / OverallHeight)
- Scale(scaleFactor)
- centreTriangle(25, 300)
- End Sub
- Private Sub Scale(ByVal scaleFactor As Decimal)
- Me.A = ScalePointF(Me.A, scaleFactor)
- Me.B = ScalePointF(Me.B, scaleFactor)
- Me.C = ScalePointF(Me.C, scaleFactor)
- End Sub
- Private Function ScalePointF(ByVal pf As PointF, ByVal factor As Decimal) As PointF
- Return New PointF(pf.X * factor, pf.Y * factor)
- End Function
- Private Sub centreTriangle(ByVal border As Integer, ByVal displaySize As Integer)
- If B.Y > A.Y Then B.Y -= ((B.Y - A.Y) * 2)
- Dim pts() As PointF = New PointF() {A, B, C}
- Dim offset_X As Integer = pts.Min(Function(p) CInt(p.X)) - border
- Dim offset_Y As Integer = pts.Max(Function(p) CInt(p.Y)) - (displaySize - border)
- A = New PointF(A.X - offset_X, A.Y - offset_Y)
- B = New PointF(B.X - offset_X, B.Y - offset_Y)
- C = New PointF(C.X - offset_X, C.Y - offset_Y)
- End Sub
- End Class
- public class Triangle
- {
- private Point A;
- private Point B;
- private Point C;
- /**
- * @return Point A
- */
- public Point getA()
- {
- return this.A;
- }
- /**
- * @return Point B
- */
- public Point getB()
- {
- return this.B;
- }
- /**
- * @return Point C
- */
- public Point getC()
- {
- return this.C;
- }
- /**
- @param sideA The length of the known side BC
- @param sideB The length of the known side AC
- @param sideC The length of the known side AB
- @param angleA The internal angle in Radians at vertex A
- @param angleB The internal angle in Radians at vertex B
- @param angleC The internal angle in Radians at vertex C
- */
- public Triangle(float sideA, float sideB, float sideC, float angleA, float angleB, float angleC)
- {
- float bX = (float)(Math.cos(angleA) * sideC);
- float bY = (float)(Math.sin(angleA) * sideC);
- this.A = new Point(0, 0);
- this.C = new Point((int)(this.A.getX() + sideB), (int)this.A.getY());
- this.B = new Point((int)bX, (int)-bY);
- if (bX < 0)
- {
- this.A = new Point((int)(this.A.getX() - bX), (int)this.A.getY());
- this.B = new Point((int)(this.B.getX() - bX), (int)this.B.getY());
- this.C = new Point((int)(this.C.getX() - bX), (int)this.C.getY());
- }
- }
- public final void ScaleToFit(float maxWidthOrHeight)
- {
- float[] xCoords = {(float)this.getA().getX(), (float)this.getB().getX(), (float)this.getC().getX()};
- float min = 10000;
- float max = -1;
- for(int x = 0; x < 3; x++) {
- if(xCoords[x] < min) {min = xCoords[x];}
- if(xCoords[x] > max) {max = xCoords[x];}
- }
- float OverallWidth = (float)(max - min);
- float OverallHeight = (float)Math.abs(this.getB().getY()); //B.Y is negative owing to Graphics coordinates
- float scaleFactor = (float)(OverallWidth > OverallHeight ? maxWidthOrHeight / OverallWidth : maxWidthOrHeight / OverallHeight);
- Scale(scaleFactor);
- centreTriangle(25, 300);
- }
- private void Scale(float scaleFactor)
- {
- this.A = ScalePoint(this.A, scaleFactor);
- this.B = ScalePoint(this.B, scaleFactor);
- this.C = ScalePoint(this.C, scaleFactor);
- }
- private Point ScalePoint(Point p, float factor)
- {
- return new Point((int)(p.getX() * factor), (int)(p.getY() * factor));
- }
- private void centreTriangle(int border, int displaySize)
- {
- int y1 = (int)this.A.getY();
- int y2 = (int)this.B.getY();
- if(y2 > y1) {y2 -= ((y2 - y1) * 2);}
- this.B = new Point((int)this.B.getX(), y2);
- Point[] pts = new Point[] {this.A, this.B, this.C};
- float min = 10000;
- float max = -1;
- for(int x = 0; x < 3; x++) {
- if(pts[x].getX() < min) {min = (float)pts[x].getX();}
- if(pts[x].getY() > max) {max = (float)pts[x].getY();}
- }
- int offset_X = (int)(min - border);
- int offset_Y = (int)(max - (displaySize - border));
- this.A = new Point((int)(this.A.getX() - offset_X), (int)(this.A.getY() - offset_Y));
- this.B = new Point((int)(this.B.getX() - offset_X), (int)(this.B.getY() - offset_Y));
- this.C = new Point((int)(this.C.getX() - offset_X), (int)(this.C.getY() - offset_Y));
- }
- }
Add Comment
Please, Sign In to add comment