Microsoft Small Basic

Program Listing:
Embed this in your website
' SmallBasic Version 1.0
' Funktion: Regular Triangle
' Autor: Pappa Lapub
' Herkunft: http://social.msdn.microsoft.com/Forums/en-US/80e1f722-5266-4c1d-bb68-1b4e614afeaa/challenge-of-the-month-july-2014
' ImportURL: http://smallbasic.com/program/? (v1 NDK700, v2 LXK869)
' Extension: (LitDev)
' Kommentar: Community Suggestions (by Nonki), 3. Draw a picture of a regular triangle
' H = a * cos(30) = a * sin(60) = a/2 * tan(60) = a/2 * sqr(3)
' Variablen: triSide .. Initial Side length for triangle
'
' --------------------------------------------------------------------------------
triSide = 100
TBLeft = 10
TBTop = 10
TBWidth = 80  ' 3 digits at FontSize = 24
TBHeight = 44

GraphicsWindow.Title = "Select side length for regular triangle"
GraphicsWindow.BrushColor = "Black"
Draw = Controls.AddButton("Draw",TBLeft + TBWidth + 10,TBTop)
Controls.SetSize(Draw,TBHeight,TBHeight)
AddSpinControl()

GraphicsWindow.PenWidth = 0
'GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()

lastSide = triSide
factH = 0.5 * Math.SquareRoot(3)
Tri = Shapes.AddTriangle(0,0, triSide,0, triSide/2,triSide*factH)
Shapes.Rotate(Tri,180)
Shapes.Move(Tri,(GraphicsWindow.Width-triSide)/2, (GraphicsWindow.Height-triSide*factH)/2)


Controls.ButtonClicked = OnButtonClick
Controls.TextTyped = OnTextBoxType

' ///// SUBs \\\\\
Sub AddSpinControl
  TBRight = TBLeft + TBWidth
  btnWidth = 20                 ' FontSize = 9
  btnHeight = (TBHeight-2)/2    ' FontSize = 9
  Delta = 2                     ' Delta Up/Down

  GraphicsWindow.FontSize = 24
  TB = Controls.AddTextBox(TBLeft,TBTop)
  Controls.SetSize(TB,TBWidth,TBHeight)
  Controls.SetTextBoxText(TB,triSide)

  GraphicsWindow.FontSize = 9
  SpinUp = Controls.AddButton("▲",TBRight-btnWidth,TBTop+1)
  Controls.SetSize(SpinUp,btnWidth,btnHeight)
  ' LDShapes.SetImage(SpinUp, Program.Directory + "\Buttons\SpinUp.png") ' for lower button height (Img: 13x7 Pxl)
  SpinDown = Controls.AddButton("▼",TBRight-btnWidth,TBTop+btnHeight+1)
  Controls.SetSize(SpinDown,btnWidth,btnHeight)
  ' LDShapes.SetImage(SpinDown, Program.Directory + "\Buttons\SpinDown.png") ' for lower button height (Img: 13x7 Pxl)
EndSub

Sub DrawTriangle
  Rel = triSide/lastSide
  'LDShapes.BrushColour(Tri, GraphicsWindow.GetRandomColor()) ' for different colors
  Shapes.Zoom(Tri,Rel,Rel)
EndSub


' ////////// EVENTs \\\\\\\\\\
Sub OnButtonClick
  If (Controls.LastClickedButton = SpinUp) And (triSide <= GraphicsWindow.Height/factH) Then
    triSide = triSide + Delta
    Controls.SetTextBoxText(TB,triSide)
  ElseIf (Controls.LastClickedButton = SpinDown) And (triSide > Delta) Then
    triSide = Math.Abs(triSide - Delta)
    Controls.SetTextBoxText(TB,triSide)
  ElseIf Controls.LastClickedButton = Draw Then
    'DrawTriangle()
  EndIf
  DrawTriangle()
EndSub

Sub OnTextBoxType
  If Controls.LastTypedTextBox = TB Then

    'Controls.SetTextBoxText(TB, triSide) ' for ReadOnly TextBox
    If Controls.GetTextBoxText(TB) + 0 <> Controls.GetTextBoxText(TB) Or Controls.GetTextBoxText(TB) >= GraphicsWindow.Width Then
      Controls.SetTextBoxText(TB, triSide)
    Else
      triSide = Controls.GetTextBoxText(TB)
    EndIf

  EndIf
EndSub

Copyright (c) Microsoft Corporation. All rights reserved.