Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. // This file contains a class for you to extend with your solution.
  2.  
  3. function MeasurementAppController()
  4. {
  5. var self = this;
  6. var cameraVideoPageController = null;
  7. this.cameraVideoPageInitialised = function(controller)
  8.  
  9. {
  10. cameraVideoPageController = controller;
  11. var self=this
  12.  
  13. //BUTTONS
  14. var height =
  15. {title: "Set camera height", onClick: "measurementAppController.height()"}
  16. var topAngle =
  17. {title: "Angle to apex", onClick: "measurementAppController.topAngle()"}
  18. var bottomAngle =
  19. {title: "Angle to base", onClick: "measurementAppController.bottomAngle()"}
  20. var distanceAndHeight =
  21. {title: "Calculate distance + height", onClick: "measurementAppController.distanceAndHeight()"}
  22.  
  23. var buttons = [height, topAngle, bottomAngle, distanceAndHeight]
  24. cameraVideoPageController.addButtons(buttons)
  25. setInterval(function() {cameraVideoPageController.setHeadsUpDisplayHTML("angle: " + betaValue.toFixed(0) + '°')}, 0) //this function is called continously so the angle of the phone can always be seen
  26. }
  27.  
  28. //FUNCTIONS
  29. this.height = function ()
  30. {
  31. var self=this
  32. this.setHeight=prompt("Please enter your approximate height(m)")
  33. }
  34.  
  35. //SET THE TOP ANGLE
  36. this.topAngle = function() //set the top angle
  37. {
  38. var self=this
  39. this.setTopAngle = betaValue.toFixed(0)
  40. if (this.setTopAngle < 180 && this.setTopAngle > 0) // the top angle must be between these specifications
  41. {
  42. cameraVideoPageController.displayMessage("The angle to the object's APEX is set as " + this.setTopAngle + '°')//pops up little window notifying the user they have set the angle
  43. }
  44. else // picks up any errors
  45. {
  46. alert ("Please set an angle to the apex between 0° and 180°")
  47. }
  48. }
  49.  
  50.  
  51. //SET THE BOTTOM ANGLE
  52. this.bottomAngle = function() //set the bottom angle
  53. {
  54. var self=this
  55. this.setBottomAngle =betaValue.toFixed(0) // rounds to nearest degree so values aren't so jumpy
  56. if (this.setBottomAngle < 90 && this.setBottomAngle > 0) // the bottom angle must be below 90 degrees or else it is impossible to calculate
  57. {
  58. cameraVideoPageController.displayMessage("The angle to the object's BASE is set as " + this.setBottomAngle + '°') //pops up little window notifying the user they have set the angle
  59. }
  60. else // picks up any errors if angle is not <90 degrees
  61. {
  62. alert ("Please set an angle to the base between 0° and 90°")
  63. }
  64. }
  65.  
  66. //SET THE DISTANCE AND HEIGHT
  67. this.distanceAndHeight = function() //calculate distance and height
  68. {
  69. var self=this
  70.  
  71. if (this.setTopAngle > 90 && this.setTopAngle<180 && this.setBottomAngle <90 && this.setBottomAngle > 0) //forumla for calculating distance + height if apex is above camera height
  72. {
  73. this.distance = this.setHeight*Math.tan(this.convert(this.setBottomAngle))
  74. this.totalHeight = this.setHeight*(1 + (Math.tan(this.convert(this.setTopAngle-90))/Math.tan(this.convert(90-this.setBottomAngle)))) //formula for calculating height --> i did it all in one step as i thought it would get too messy and complicated if i created seperate functions
  75. alert("The object is " + this.distance.toFixed(2) + " metres away and it is " + this.totalHeight.toFixed(2) + "metres high") //popup window of the values
  76. }
  77. else if (this.setTopAngle > 0 && this.setTopAngle < 90 && this.setBottomAngle <90 && this.setBottomAngle > 0) //forumla for calculating distance + height if apex is below camera height
  78. { this.distance = this.setHeight*Math.tan(this.convert(this.setBottomAngle)) //forumla for calculating distance
  79. this.totalHeight = this.setHeight - (this.distance/Math.tan(this.convert(this.setTopAngle)))
  80. alert("The object is " + this.distance.toFixed(2) + " metres away and it is " + this.totalHeight.toFixed(2) + "metres high") //popup window of the value
  81. }
  82. else
  83. {
  84. alert ("Unable to calculate distance + height!") //picks up any errors
  85. }
  86. }
  87.  
  88. //CONVERT DEGREES TO RADIANS
  89. this.convert = function(x)
  90. {
  91. return Math.PI/180 * x
  92. }
  93. }
  94.  
  95. // OTHER METHODS
  96.  
  97. //get Beta Value
  98. function handleOrientation(event)
  99. {
  100. var self=this
  101. this.betaValue= event.beta;
  102. }
  103. window.addEventListener("deviceorientation", handleOrientation, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement