Search This Blog

Saturday, 22 March 2014

Visual Basic: Area de un triángulo (código)


areatri1


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
  1. Module areatriModule
  2. Dim base, height, area, cm, size, x, y, k, pxBase, pxHeight, x1, y1, x2, y2, x3, y3, triangle As Primitive
  3. Sub Main()
    1. ‘Area of a triangle
    2. TextWindow.Write(“enter the value of the base of the triangle: “)
    3. base = TextWindow.ReadNumber()
    4. TextWindow.Write(“enter the value of the height of the triangle: “)
    5. height = TextWindow.ReadNumber()
    6. area = (base * height) / 2
    7. TextWindow.WriteLine(“The area is ” + Area)
  4. cm = 24 ‘ [px/cm]
  5. size = 12 ‘ [cm]
  6. GraphicsWindow.Width = cm * size
  7. GraphicsWindow.Height = cm * size
  8. GraphicsWindow.Top = TextWindow.Top + 150
  9. GraphicsWindow.Left = TextWindow.Left + 50
  10. GraphicsWindow.BackgroundColor = “LightGray”
  11. DrawGrid()
  12. DrawTriangle()
  13. End Sub
  14. Sub DrawGrid()
    1. GraphicsWindow.PenColor = “DimGray”
    2. For x = 0 To cm * size Step cm
i.   GraphicsWindow.DrawLine(x, 0, x, cm * size)
  1. Next
  2. For y = 0 To cm * size Step cm
i.   GraphicsWindow.DrawLine(0, y, cm * size, y)
  1. Next
  2. End Sub
  3. Sub DrawTriangle()
    1. GraphicsWindow.PenColor = “Black”
    2. GraphicsWindow.BrushColor = “MediumSeaGreen”
    3. k = 0.3 ‘ 0 <= k <= 1
    4. pxBase = base * cm
    5. pxHeight = height * cm
    6. x1 = pxBase * k
    7. y1 = 0
    8. x2 = pxBase
    9. y2 = pxHeight
    10. x3 = 0
    11. y3 = y2
    12. triangle = Shapes.AddTriangle(x1, y1, x2, y2, x3, y3)
    13. Shapes.Move(triangle, cm, cm)
    14. End Sub
    15. End Module

No comments:

Post a Comment