Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 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. package ztp2;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Random;
  13. import javax.servlet.ServletException;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17.  
  18. /**
  19. *
  20. * @author Piotr Biskup
  21. */
  22. public class Ellipsoid extends HttpServlet {
  23.  
  24.  
  25. private class Deffect {
  26. private double x;
  27. private double y;
  28. private double z;
  29. private double r;
  30.  
  31. public Deffect(double x, double y, double z, double r) {
  32. this.x = x;
  33. this.y = y;
  34. this.z = z;
  35. this.r = r;
  36. }
  37.  
  38. public boolean pointInsideSphere(double xp, double yp, double zp){
  39.  
  40. if( ((xp-x)*(xp-x) + (yp-y)*(yp-y) + (zp-z)*(zp-z)) <= r*r ) return true;
  41.  
  42. return false;
  43. }
  44.  
  45. }
  46.  
  47.  
  48.  
  49. private List<Deffect> deffects = new ArrayList<>();
  50. private double a;
  51. private double b;
  52. private double c;
  53. private double v;
  54. private double g;
  55. private static final int points = 10_000_000;
  56. private int pointInsideDeffects;
  57. private double realMass;
  58.  
  59.  
  60. /**
  61. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  62. * methods.
  63. *
  64. * @param request servlet request
  65. * @param response servlet response
  66. * @throws ServletException if a servlet-specific error occurs
  67. * @throws IOException if an I/O error occurs
  68. */
  69. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  70. throws ServletException, IOException {
  71. try (PrintWriter out = response.getWriter()) {
  72. out.println(realMass);
  73. }
  74.  
  75. pointInsideDeffects = 0;
  76. deffects.clear();
  77. }
  78.  
  79. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  80. /**
  81. * Handles the HTTP <code>GET</code> method.
  82. *
  83. * @param request servlet request
  84. * @param response servlet response
  85. * @throws ServletException if a servlet-specific error occurs
  86. * @throws IOException if an I/O error occurs
  87. */
  88. @Override
  89. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  90. throws ServletException, IOException {
  91.  
  92. double x = Double.parseDouble(request.getParameter("x"));
  93. double y = Double.parseDouble(request.getParameter("y"));
  94. double z = Double.parseDouble(request.getParameter("z"));
  95. double r = Double.parseDouble(request.getParameter("r"));
  96.  
  97. Deffect d = new Deffect(x,y,z,r);
  98.  
  99. deffects.add(d);
  100. //processRequest(request, response);
  101. }
  102.  
  103. /**
  104. * Handles the HTTP <code>POST</code> method.
  105. *
  106. * @param request servlet request
  107. * @param response servlet response
  108. * @throws ServletException if a servlet-specific error occurs
  109. * @throws IOException if an I/O error occurs
  110. */
  111. @Override
  112. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  113. throws ServletException, IOException {
  114.  
  115. a = Double.parseDouble(request.getParameter("a"));
  116. b = Double.parseDouble(request.getParameter("b"));
  117. c = Double.parseDouble(request.getParameter("c"));
  118. v = Double.parseDouble(request.getParameter("v"));
  119. g = Double.parseDouble(request.getParameter("g"));
  120.  
  121.  
  122. pointInsideDeffects = monteCarlo(points);
  123.  
  124. double ellipsoideMass = v*(1.0 - (double)pointInsideDeffects/(double)points)*ellipsoideVolume();
  125. double deffectsMass = g*( (double)pointInsideDeffects/(double)points)*ellipsoideVolume();
  126.  
  127.  
  128. realMass = ellipsoideMass + deffectsMass;
  129.  
  130. processRequest(request, response);
  131. }
  132.  
  133.  
  134.  
  135.  
  136. private boolean pointInsideEllipsoide(double xp, double yp, double zp){
  137.  
  138. if( ((xp*xp)/(a*a) + (yp*yp)/(b*b) + (zp*zp)/(c*c)) <= 1.0 ) return true;
  139.  
  140. return false;
  141. }
  142.  
  143. private double generateVariable(double axis){
  144. Random random = new Random();
  145. int minus = random.nextInt(2);
  146. double a = random.nextDouble()*axis;
  147. if(minus == 1) a=a*(-1.0);
  148. return a;
  149. }
  150.  
  151. private int monteCarlo(int n){
  152.  
  153. int insidePoints = 0;
  154.  
  155. for(int i =0;i<n; i++){
  156.  
  157. double x = generateVariable(a);
  158. double y = generateVariable(b);
  159. double z = generateVariable(c);
  160.  
  161. if(!pointInsideEllipsoide(x, y, z))
  162. i--;
  163. else{
  164. for(Deffect d: deffects){
  165. if(d.pointInsideSphere(x, y, z))
  166. {
  167. insidePoints++;
  168. break;
  169. }
  170. }
  171. }
  172.  
  173.  
  174. }
  175.  
  176. return insidePoints;
  177. }
  178.  
  179. private double ellipsoideVolume(){
  180. return (4.0/3.0)*Math.PI*a*b*c;
  181. }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement