Procedure:Pythagorus

Share your advanced PureBasic knowledge/code with the community.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Procedure:Pythagorus

Post by collectordave »

Just a quick and simple Triangle side length calculator based around pythagorus for a bit of fun.

You can use 3,4,5 to test.

Enter any two side lengths and the third is calculated and presented in the empty string gadget.

Code: Select all

Enumeration FormWindow
  #winMain
  #txtSideA
  #strSideA
  #txtSideB
  #strSideB
  #txtHypotenuse
  #strHypotenuse
  #btnCalculate
EndEnumeration

Procedure Calculate()
 
  Define A.i, B.i, H.i, x.i
    
  A = Val(GetGadgetText(#strSideA))
  B = Val(GetGadgetText(#strSideB))
  H = Val(GetGadgetText(#strHypotenuse))
    
  If A = 0
    x = Sqr((H * H) - (B * B))
    SetGadgetText(#strSideA,Str(x))      
  EndIf

  If B = 0
    x = Sqr((H * H) - (A * A))
    SetGadgetText(#strSideB,Str(x))       
  EndIf

  If H = 0
    x = Sqr((A * A) + (B * B))
    SetGadgetText(#strHypotenuse,Str(x))
  EndIf
  
EndProcedure

  OpenWindow(#winMain, 5, 5, 310, 110, "Pythagorus", #PB_Window_SystemMenu)
  TextGadget(#txtSideA, 10, 10, 90, 20, "Side A")
  StringGadget(#strSideA, 10, 40, 90, 20, "")
  TextGadget(#txtSideB, 110, 10, 90, 20, "Side B")
  StringGadget(#strSideB, 110, 40, 90, 20, "")
  TextGadget(#txtHypotenuse, 210, 10, 90, 20, "Hypotenuse")
  StringGadget(#strHypotenuse, 210, 40, 90, 20, "")
  ButtonGadget(#btnCalculate, 220, 70, 80, 30, "Calculate")

  Repeat
    
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End

      Case #PB_Event_Gadget
        Select EventGadget()
          
          Case #btnCalculate
            Calculate()
        EndSelect
      
    EndSelect
    
  ForEver
Enjoy

cd
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.