'Author: Eric Heim 'Date Created: 3/15/2011 'Course: CS0004 'Description: This program allows the user to click three times on a label to draw a triangle. ' Then, it computes and displays the lengths of the sides and the perimeter and area. Option Strict On Option Explicit On Public Class frmMain 'counter for the number of clicks the user has performed for a triangle Dim counter As Integer = 0 Dim point1, point2, point3 As Point Private Sub lblDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblDraw.Click If counter = 0 Then CreateTrianglePoint1() ClearTriangleData() counter += 1 ElseIf counter = 1 Then CreateTrianglePoint2() counter += 1 Else Dim area, perimeter, side1, side2, side3 As Double CreateTrianglePoint3() GetTriangleStats(area, perimeter, side1, side2, side3) SetTriangleStats(area, perimeter, side1, side2, side3) counter = 0 End If End Sub 'Name: CreateTrianglePoint1 'Parameters: None 'Return Type: None 'Description Records the first point for the trangle Sub CreateTrianglePoint1() point1 = Cursor.Position End Sub 'Name: CreateTrianglePoint2 'Parameters: None 'Return Type: None 'Description Records the second point for the trangle and draws the first line Sub CreateTrianglePoint2() Dim offsetPoint As Point = New Point(lblDraw.Location.X + Me.Location.X + 5, lblDraw.Location.Y + Me.Location.Y + 30) Dim myPen As Pen = New Pen(Color.Blue, 3) point2 = Cursor.Position lblDraw.CreateGraphics.DrawLine(myPen, point1.X - offsetPoint.X, point1.Y - offsetPoint.Y, point2.X - offsetPoint.X, point2.Y - offsetPoint.Y) End Sub 'Name: CreateTrianglePoint3 'Parameters: None 'Return Type: None 'Description Records the third point for the trangle and draws the last two lines Sub CreateTrianglePoint3() Dim offsetPoint As Point = New Point(lblDraw.Location.X + Me.Location.X + 5, lblDraw.Location.Y + Me.Location.Y + 30) Dim myPen1 As Pen = New Pen(Color.Red, 3) Dim myPen2 As Pen = New Pen(Color.Green, 3) point3 = Cursor.Position lblDraw.CreateGraphics.DrawLine(myPen1, point2.X - offsetPoint.X, point2.Y - offsetPoint.Y, Cursor.Position.X - offsetPoint.X, Cursor.Position.Y - offsetPoint.Y) lblDraw.CreateGraphics.DrawLine(myPen2, Cursor.Position.X - offsetPoint.X, Cursor.Position.Y - offsetPoint.Y, point1.X - offsetPoint.X, point1.Y - offsetPoint.Y) End Sub 'Name: GetTriangleStats 'Parameters: inArea: the triangle's area, inPerimeter: the triangle's perimeter, ' inSide1, inSide2, and inSide3: the sides of the triangle 'Return Type: None 'Description Computes the area, perimeter and the length of the three sides of the triangle ' then stores them in the parameters passed to the subprocedure Sub GetTriangleStats(ByRef inArea As Double, ByRef inPerimeter As Double, ByRef inSide1 As Double, ByRef inSide2 As Double, ByRef inSide3 As Double) 'computes the length of each side of the triangle by the distance formula inSide1 = Math.Sqrt((point1.X - point2.X) ^ 2 + (point1.Y - point2.Y) ^ 2) inSide2 = Math.Sqrt((point2.X - point3.X) ^ 2 + (point2.Y - point3.Y) ^ 2) inSide3 = Math.Sqrt((point3.X - point1.X) ^ 2 + (point3.Y - point1.Y) ^ 2) 'Computes the perimeter and area of the triangle inPerimeter = GetTrianglePerimeter(inSide1, inSide2, inSide3) inArea = GetTriangleArea(inSide1, inSide End Sub 'Name: GetTrianglePerimeter 'Parameters: firstSide, secondSide, thirdSide: the three sides of the triangle 'Return Type: Double 'Description Computes the perimeter of the triangle Function GetTrianglePerimeter(ByVal firstSide As Double, ByVal secondSide As Double, ByVal thirdSide As Double) As Double Return firstSide + secondSide + thirdSide End Function 'Name: GetTriangleArea 'Parameters: a, b, c: the three sides of the triangle 'Return Type: Double 'Description Computes the area of the triangle Function GetTriangleArea(ByVal a As Double, ByVal b As Double, ByVal c As Double) As Double Dim s As Double = (a + b + c) / 2 Return Math.Sqrt(s * (s - a) * (s - b) * (s - c)) End Function 'Name: SetTriangleStats 'Parameters: inArea: the triangle's area, inPerimeter: the triangle's perimeter, ' inSide1, inSide2, and inSide3: the sides of the triangle 'Return Type: None 'Description Puts the triangle's stats from the parameters to the corresponding text boxes Sub SetTriangleStats(ByVal inArea As Double, ByVal inPerimeter As Double, ByVal inSide1 As Double, ByVal inSide2 As Double, ByVal inSide3 As Double) txtArea.Text = CStr(inArea) txtPerimeter.Text = CStr(inPerimeter) txtSide1.Text = CStr(inSide1) txtSide2.Text = CStr(inSide2) txtSide3.Text = CStr(inSide3) End Sub 'Name: ClearTriangleData 'Parameters: None 'Return Type: None 'Description Clears all of the text boxes and clears the drawing label Sub ClearTriangleData() txtArea.Text = "" txtPerimeter.Text = "" txtSide1.Text = "" txtSide2.Text = "" txtSide3.Text = "" lblDraw.CreateGraphics.Clear(Color.White) End Sub End Class