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

areatri

Se pide ingresar la base y altura de un triángulo, y el programa calcula el área y lo grafica.  Edited by Nonki Takahashi
  1. ‘  Program Listing WGZ319
  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)
  1. cm = 24 ‘ [px/cm]
  2. size = 12 ‘ [cm]
  3. GraphicsWindow.Width = cm * size
  4. GraphicsWindow.Height = cm * size
  5. GraphicsWindow.Top = TextWindow.Top + 150
  6. GraphicsWindow.Left = TextWindow.Left + 50
  7. GraphicsWindow.BackgroundColor = “LightGray”
  8. DrawGrid()
  9. DrawTriangle()
  1. Sub DrawGrid
  2. GraphicsWindow.PenColor = “DimGray”
  3. For x = 0 To cm * size Step cm
  4. GraphicsWindow.DrawLine(x, 0, x, cm * size)
  5. EndFor
  6. For y = 0 To cm * size Step cm
  7. GraphicsWindow.DrawLine(0, y, cm * size, y)
  8. EndFor
  9. EndSub
  1. Sub DrawTriangle
  2. GraphicsWindow.PenColor = “Black”
  3. GraphicsWindow.BrushColor = “MediumSeaGreen”
  4. k = 0.3 ‘ 0 <= k <= 1
  5. pxBase = base * cm
  6. pxHeight = height * cm
  7. x1 = pxBase * k
  8. y1 = 0
  9. x2 = pxBase
  10. y2 = pxHeight
  11. x3 = 0
  12. y3 = y2
  13. triangle = Shapes.AddTriangle(x1, y1, x2, y2, x3, y3)
  14. Shapes.Move(triangle, cm, cm)
  15. EndSub