Capacitación básica en Computación
Visual Basic: Area de un triángulo (código)
Se pide ingresar la base y altura de un triángulo, y el programa calcula el área y lo grafica. Graduado (convertido) del programa en Small Basic WGZ319 Editado por Nonki Takahashi
- Module areatriModule
- Dim base, height, area, cm, size, x, y, k, pxBase, pxHeight, x1, y1, x2, y2, x3, y3, triangle As Primitive
- Sub Main()
- ‘Area of a triangle
- TextWindow.Write(“enter the value of the base of the triangle: “)
- base = TextWindow.ReadNumber()
- TextWindow.Write(“enter the value of the height of the triangle: “)
- height = TextWindow.ReadNumber()
- area = (base * height) / 2
- TextWindow.WriteLine(“The area is ” + Area)
- cm = 24 ‘ [px/cm]
- size = 12 ‘ [cm]
- GraphicsWindow.Width = cm * size
- GraphicsWindow.Height = cm * size
- GraphicsWindow.Top = TextWindow.Top + 150
- GraphicsWindow.Left = TextWindow.Left + 50
- GraphicsWindow.BackgroundColor = “LightGray”
- DrawGrid()
- DrawTriangle()
- End Sub
- Sub DrawGrid()
- GraphicsWindow.PenColor = “DimGray”
- For x = 0 To cm * size Step cm
- Next
- For y = 0 To cm * size Step cm
- Next
- End Sub
- Sub DrawTriangle()
- GraphicsWindow.PenColor = “Black”
- GraphicsWindow.BrushColor = “MediumSeaGreen”
- k = 0.3 ‘ 0 <= k <= 1
- pxBase = base * cm
- pxHeight = height * cm
- x1 = pxBase * k
- y1 = 0
- x2 = pxBase
- y2 = pxHeight
- x3 = 0
- y3 = y2
- triangle = Shapes.AddTriangle(x1, y1, x2, y2, x3, y3)
- Shapes.Move(triangle, cm, cm)
- End Sub
- End Module
No comments:
Post a Comment