Share via


Small Basic: How to Draw a Triangle

If you know how to draw rectangle, how can you draw a triangle on it?
See following picture.  You already drew a square as GraphicsWindow.DrawRectangle(100, 200, 200, 200).  So, the triangle you should draw has three points (x1 = 100, y1 = 200), (x2 = 300, y2 = 200), (x3 = 200, y3 = 50).  There are 6 coordinate for the triangle.

So the answer is GraphicsWindow.DrawTriangle(100, 200, 300, 200, 200, 50).

Following code shows picture above.

gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
GraphicsWindow.Title = "How to Draw Triangle"
DrawGrid()
x = 100
y = 200
width = 200
height = 200
GraphicsWindow.PenWidth = 4
GraphicsWindow.PenColor = "Green"
GraphicsWindow.DrawRectangle(x, y, width, height)
x1 = 100
y1 = 200
x2 = 300
y2 = 200
x3 = 200
y3 = 50
GraphicsWindow.PenColor = "DimGray"
GraphicsWindow.DrawTriangle(x1, y1,  x2, y2, x3,  y3)
DrawPoints()
Sub DrawPoints
  ' param x1, y1, x2, y2, x3, y3
  size = 8
  r = size  / 2
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.FillEllipse(x1 - r, y1 -  r, size, size)
  GraphicsWindow.DrawText(x1 + 4, y1 +  4, "P1=(" + x1  + "," + y1 +  ")")
  GraphicsWindow.FillEllipse(x2 - r, y2 -  r, size, size)
  GraphicsWindow.DrawText(x2 + 4, y2 +  4, "P2=(" + x2  + "," + y2 +  ")")
  GraphicsWindow.FillEllipse(x3 - r, y3 -  r, size, size)
  GraphicsWindow.DrawText(x3 + 4, y3 +  4, "P3=(" + x3  + "," + y3 +  ")")
EndSub
Sub DrawGrid
  GraphicsWindow.PenColor = "MediumSeaGreen"
  GraphicsWindow.BrushColor = "MediumSeaGreen"
  For _x =  0 To gw Step 50
    GraphicsWindow.DrawLine(_x, 0, _x, gh)
    If gw -  50 < _x Then
      GraphicsWindow.DrawText(_x + 4, 4, "x")
    Else
      GraphicsWindow.DrawText(_x + 4, 4, _x)
    EndIf
  EndFor
  For _y =  0 To gh Step 50
    GraphicsWindow.DrawLine(0, _y,  gw, _y)
    If gh -  50 < _y Then
      GraphicsWindow.DrawText(4, _y +  4, "y")
    Else
      GraphicsWindow.DrawText(4, _y +  4, _y)
    EndIf
  EndFor
EndSub

And you can see another sample with GraphicsWindow.FillTriangle()  here

Other Languages