Advertisement
VladimirKostovsky

JS_Oct

Dec 25th, 2023
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Point {
  2.     constructor(x, y) {
  3.         this.coordinates = [x, y];
  4.     }
  5. }
  6.  
  7. class Rectangle {
  8.     constructor(point1, point2) {
  9.         this.point1 = point1;
  10.         this.point2 = point2;
  11.     }
  12. }
  13.  
  14. class DecomposedField {
  15.     constructor(field, cellWidth, cellHeight, useAllAlgorithm) {
  16.         // Реализация DecomposedField
  17.     }
  18. }
  19.  
  20. class Field {
  21.     constructor(height, width) {
  22.         this.changeSize(height, width);
  23.         this.clear();
  24.     }
  25.  
  26.     changeSize(height, width) {
  27.         this.height = height;
  28.         this.width = width;
  29.         this.decomposedField = null;
  30.     }
  31.  
  32.     clear() {
  33.         this.robot = null;
  34.         this.places = [];
  35.         this.barriers = [];
  36.         this.decomposedField = null;
  37.     }
  38.  
  39.     isDecomposed() {
  40.         return this.decomposedField != null;
  41.     }
  42.  
  43.     placeRobot(x, y) {
  44.         let point = new Point(x, y);
  45.         if (this.pointIsFree(point)) {
  46.             this.robot = new Point(x, y);
  47.             return true;
  48.         }
  49.         return false;
  50.     }
  51.  
  52.     addBarrier(point1, point2) {
  53.         let rectangle = new Rectangle(point1, point2);
  54.         this.barriers.push(rectangle);
  55.         this.decomposedField = null;
  56.     }
  57.  
  58.     addPlace(x, y) {
  59.         let newPlace = new Point(x, y);
  60.         if (this.pointIsFree(newPlace)) {
  61.             this.places.push(newPlace);
  62.             return true;
  63.         }
  64.         return false;
  65.     }
  66.  
  67.     isValidPoint(point) {
  68.         return point.x >= 0 && point.x < this.width && point.y >= 0 && point.y < this.height;
  69.     }
  70.  
  71.     pointIsFree(point) {
  72.         if (!this.isValidPoint(point)) {
  73.             return false;
  74.         }
  75.  
  76.         for (let item of this.barriers) {
  77.             if (this.isPointInsideRectangle(point, item) || this.isPointOnRectangle(point, item)) {
  78.                 return false;
  79.             }
  80.         }
  81.         return true;
  82.     }
  83.  
  84.     isPointInsideRectangle(point, rectangle) {
  85.         const [x, y] = point.coordinates;
  86.         const [x1, y1] = rectangle.point1.coordinates;
  87.         const [x2, y2] = rectangle.point2.coordinates;
  88.  
  89.         return x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && y >= Math.min(y1, y2) && y <= Math.max(y1, y2);
  90.     }
  91.  
  92.     isPointOnRectangle(point, rectangle) {
  93.         let pointOnLine1 = this.isPointOnLine(point, rectangle.point1, rectangle.point2);
  94.         let pointOnLine2 = this.isPointOnLine(point, rectangle.point2, rectangle.point1);
  95.         return pointOnLine1 || pointOnLine2;
  96.     }
  97.  
  98.     isPointOnLine(point, linePoint1, linePoint2) {
  99.         const [x, y] = point.coordinates;
  100.         const [x1, y1] = linePoint1.coordinates;
  101.         const [x2, y2] = linePoint2.coordinates;
  102.  
  103.         return (
  104.             (x - x1) * (y2 - y1) === (x2 - x1) * (y - y1) &&
  105.             x >= Math.min(x1, x2) &&
  106.             x <= Math.max(x1, x2) &&
  107.             y >= Math.min(y1, y2) &&
  108.             y <= Math.max(y1, y2)
  109.         );
  110.     }
  111.  
  112.     decompose(cellWidth, cellHeight, useAllAlgorithm = false) {
  113.         let decomposedField = new DecomposedField(this, cellWidth, cellHeight, useAllAlgorithm);
  114.  
  115.         if (decomposedField.composingErrorMessage) {
  116.             return decomposedField.composingErrorMessage;
  117.         }
  118.  
  119.         this.decomposedField = decomposedField;
  120.         return null;
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement