Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.util.ArrayList;
  10. import java.util.Random;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. /**
  17. *
  18. * @author Maciek
  19. */
  20. public class Cone extends HttpServlet {
  21.  
  22. /**
  23. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  24. * methods.
  25. *
  26. * @param request servlet request
  27. * @param response servlet response
  28. * @throws ServletException if a servlet-specific error occurs
  29. * @throws IOException if an I/O error occurs
  30. */
  31. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  32. throws ServletException, IOException {
  33. response.setContentType("text/html;charset=UTF-8");
  34. try (PrintWriter out = response.getWriter()) {
  35. /* TODO output your page here. You may use following sample code. */
  36. out.println("<!DOCTYPE html>");
  37. out.println("<html>");
  38. out.println("<head>");
  39. out.println("<title>Servlet Cone</title>");
  40. out.println("</head>");
  41. out.println("<body>");
  42. out.println("<h1>Servlet Cone at " + request.getContextPath() + "</h1>");
  43. out.println("</body>");
  44. out.println("</html>");
  45. }
  46. }
  47.  
  48. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  49. /**
  50. * Handles the HTTP <code>GET</code> method.
  51. *
  52. * @param request servlet request
  53. * @param response servlet response
  54. * @throws ServletException if a servlet-specific error occurs
  55. * @throws IOException if an I/O error occurs
  56. */
  57. @Override
  58. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  59. throws ServletException, IOException {
  60. processRequest(request, response);
  61. ArrayList<Double> defects;
  62. if (request.getSession().getAttribute("defects") == null) {
  63. defects = new ArrayList<Double>();
  64. } else {
  65. defects = (ArrayList<Double>) request.getSession().getAttribute("defects");
  66. }
  67. if ( request.getParameter("x") != null
  68. && request.getParameter("y") != null
  69. && request.getParameter("z") != null
  70. && request.getParameter("r") != null){
  71. double x = Double.parseDouble(request.getParameter("x"));
  72. double y = Double.parseDouble(request.getParameter("y"));
  73. double z = Double.parseDouble(request.getParameter("z"));
  74. double r = Double.parseDouble(request.getParameter("r"));
  75.  
  76. defects.add(x);
  77. defects.add(y);
  78. defects.add(z);
  79. defects.add(r);
  80. request.getSession().setAttribute("defects", defects);
  81. }
  82. }
  83.  
  84. /**
  85. * Handles the HTTP <code>POST</code> method.
  86. *
  87. * @param request servlet request
  88. * @param response servlet response
  89. * @throws ServletException if a servlet-specific error occurs
  90. * @throws IOException if an I/O error occurs
  91. */
  92. @Override
  93. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  94. throws ServletException, IOException {
  95. processRequest(request, response);
  96. if ( request.getParameter("ra") != null
  97. && request.getParameter("rb") != null
  98. && request.getParameter("h") != null
  99. && request.getParameter("c") != null
  100. && request.getParameter("g") != null){
  101.  
  102. double ra = Double.parseDouble(request.getParameter("ra"));
  103. double h = Double.parseDouble(request.getParameter("h"));
  104. double rb = Double.parseDouble(request.getParameter("rb"));
  105. double coneDensity = Double.parseDouble(request.getParameter("c"));
  106. double globeDensity = Double.parseDouble(request.getParameter("g"));
  107. ArrayList<Double> defects = (ArrayList<Double>) request.getSession().getAttribute("defects");
  108. double H = (h * ra) / (ra - rb);
  109. double result = retMass(ra, rb, h, H, defects, coneDensity, globeDensity);
  110.  
  111.  
  112.  
  113. }
  114. }
  115.  
  116.  
  117. private boolean isInFigure(double ra, double rb, double h,
  118. double H, double x, double y, double z){
  119. if( (Math.pow(x, 2) + Math.pow(y, 2) <= Math.pow(((z*ra)/H), 2) && ( 0 <= z ) && (z <= H))
  120. && !(Math.pow(x, 2) + Math.pow(y, 2) <= Math.pow(((z*rb)/h), 2) && ( 0 <= z ) && (z <= h)))
  121. return true;
  122. return false;
  123. }
  124.  
  125.  
  126.  
  127. private int monteCarlo(int precision, double ra, double rb, double h, double H, ArrayList<Double> defects){
  128. double randomX = 0;
  129. double randomY = 0;
  130. double randomZ = 0;
  131. Random r = new Random();
  132. int defectsSize = defects.size();
  133. int defectHits = 0;
  134. for( int i = precision; i >=0; i--){
  135. do{
  136. randomX = -ra + ( ra + ra) * r.nextDouble();
  137. randomY = -ra + ( ra + ra) * r.nextDouble();
  138. randomZ = h * r.nextDouble();
  139. } while ( !isInFigure(ra, rb, h, H, randomX, randomY, randomZ));
  140. for(int j = 0; j < defectsSize; j+=4) {
  141. if ( (randomX - defects.get(j)) * (randomX - defects.get(j))
  142. + (randomY - defects.get(j+1)) * (randomY - defects.get(j+1))
  143. + (randomZ - defects.get(j+2)) * (randomZ - defects.get(j+2))
  144. <= defects.get(j+3) * defects.get(j+3)){
  145. defectHits++;
  146. }
  147. }
  148. }
  149. return defectHits;
  150. }
  151.  
  152. private double retMass(double ra, double rb, double h, double H, ArrayList<Double> defects, double coneDens, double globeDens) {
  153. int precision = 10000000;
  154. double defectHits = monteCarlo(precision, ra, rb, h, H, defects);
  155. double truncatedConeVolume = ( Math.PI * h * ((ra * ra) + (ra * rb) +(rb * rb))) / 3;
  156. double coneHits = precision - defectHits;
  157. double coneHitsVolume = truncatedConeVolume * coneHits/precision;
  158. double defectsVolume = truncatedConeVolume - coneHitsVolume;
  159. double properMass = coneHitsVolume * coneDens + defectsVolume * globeDens;
  160. return properMass;
  161. }
  162.  
  163. /**
  164. * Returns a short description of the servlet.
  165. *
  166. * @return a String containing servlet description
  167. */
  168. @Override
  169. public String getServletInfo() {
  170. return "Short description";
  171. }// </editor-fold>
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement