Advertisement
James1337

Convex Polygon Area

Jul 4th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 0.99 KB | None | 0 0
  1. #NoTrayIcon
  2.  
  3. ; Convex Polygon Area
  4. ; http://redd.it/29umz8
  5.  
  6. Global $Form, $idInput, $idOutput, $idButton
  7. $Form = GUICreate("Convex Polygon Area", 640, 360)
  8. GUISetFont(10, 1000, 0, "Courier New", $Form)
  9. $idInput = GUICtrlCreateEdit("Input", 20, 20, 300, 300)
  10. $idOutput = GUICtrlCreateEdit("", 320, 20, 300, 300)
  11. $idButton = GUICtrlCreateButton("Calculate", 200, 320, 120, 20)
  12. GUISetState(@SW_SHOW, $Form)
  13.  
  14. Do
  15.     Switch GUIGetMsg()
  16.         Case -3
  17.             ExitLoop
  18.         Case $idButton
  19.             GUICtrlSetData($idOutput, Abs(ConvexPolygonArea(GUICtrlRead($idInput))))
  20.     EndSwitch
  21. Until False
  22. Exit
  23.  
  24. Func ConvexPolygonArea($input)
  25.     Local $area = 0, $lines, $n, $i, $a, $b
  26.     $input = StringRegExpReplace($input, "^.+\r\n", "") ; remove first line
  27.     $lines = StringSplit($input, @CRLF, 1)
  28.     $n = $lines[0]
  29.     For $i = 1 To $n
  30.         $lines[$i] = StringSplit($lines[$i], ",", 2)
  31.     Next
  32.     For $i = 1 To $n
  33.         $a = $lines[$i]
  34.         $b = $lines[Mod($i, $n)+1]
  35.         $area += $a[0]*$b[1] - $b[0]*$a[1]
  36.     Next
  37.     Return $area / 2
  38. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement