Advertisement
VladimirKostovsky

JS_view

Dec 25th, 2023
1,149
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.  
  124. class FieldView {
  125.     constructor(canvasId, field) {
  126.         let canvas = document.getElementById(canvasId);
  127.         this.context = canvas.getContext('2d');
  128.         this.field = field;
  129.     }
  130.  
  131.     resizeCanvas() {
  132.         this.context.canvas.width = this.field.width;
  133.         this.context.canvas.height = this.field.height;
  134.     }
  135.  
  136.     drawBarriers() {
  137.         for (let item of this.field.barriers) {
  138.             this.drawRectangle(item);
  139.         }
  140.     }
  141.  
  142.     drawRobot() {
  143.         if (this.field.robot != null) {
  144.             this.drawPoint(this.field.robot, this.getPointRadius(), "red", "red");
  145.         }
  146.     }
  147.  
  148.     drawPlaces() {
  149.         for (let item of this.field.places) {
  150.             this.drawPlace(item, 1);
  151.         }
  152.     }
  153.  
  154.     drawNewPlaces() {
  155.         let decomposedField = this.field.decomposedField;
  156.  
  157.         if (!this.field.isDecomposed()) return;
  158.  
  159.         let cellWidth = this.field.decomposedField.cellWidth;
  160.         let cellHeight = this.field.decomposedField.cellHeight;
  161.  
  162.         let radius = Math.max(Math.min(cellHeight, cellWidth) / 2, this.getPointRadius());
  163.  
  164.         this.context.fillStyle = "rgb(243,10,10, 1)";
  165.         for (let place of decomposedField.places) {
  166.             let newPoint = new Point(place.x * cellWidth + (cellWidth / 2), place.y * cellHeight + (cellHeight / 2));
  167.             this.drawPoint(newPoint, radius, "", "yellow", 0);
  168.         }
  169.  
  170.         if (decomposedField.robot != null) {
  171.             const [x, y] = decomposedField.robot.coordinates;
  172.             let newPoint = new Point(x * cellWidth + (cellWidth / 2), y * cellHeight + (cellHeight / 2));
  173.             this.drawPoint(newPoint, radius, "", "red", 0);
  174.         }
  175.     }
  176.  
  177.     drawGridAndAreas(showGrid = true, showAreas = true) {
  178.         if (!showAreas && !showGrid) return;
  179.  
  180.         let decomposedField = this.field.decomposedField;
  181.  
  182.         let cellWidth = decomposedField.cellWidth;
  183.         let cellHeight = decomposedField.cellHeight;
  184.  
  185.         this.context.lineWidth = 2;
  186.         this.context.strokeStyle = "rgba(0,21,255,0.3)";
  187.  
  188.         for (let rowId = 0; rowId < decomposedField.rows; rowId++) {
  189.             for (let colId = 0; colId < decomposedField.cols; colId++) {
  190.                 this.context.beginPath();
  191.  
  192.                 if (showAreas) {
  193.                     if (decomposedField.field[rowId][colId]) {
  194.                         this.context.fillStyle = "rgba(255,0,35,0.2)";
  195.                     } else {
  196.                         this.context.fillStyle = "rgba(0,255,60,0.2)";
  197.                     }
  198.                 }
  199.  
  200.                 this.context.rect(colId * cellWidth, rowId * cellHeight, cellWidth, cellHeight);
  201.                 if (showGrid) this.context.stroke();
  202.                 if (showAreas) this.context.fill();
  203.  
  204.                 this.context.closePath();
  205.             }
  206.         }
  207.     }
  208.  
  209.     drawGrid(cellHeight, cellWidth) {
  210.         let numberOfCols = Math.floor(this.field.width / cellWidth);
  211.         let numberOfRows = Math.floor(this.field.height / cellHeight);
  212.  
  213.         this.context.lineWidth = 2;
  214.         this.context.strokeStyle = "rgba(0,21,255,0.3)";
  215.  
  216.         for (let rowId = 0; rowId < numberOfRows; rowId++) {
  217.             for (let colId = 0; colId < numberOfCols; colId++) {
  218.                 this.context.beginPath();
  219.                 this.context.rect(colId * cellWidth, rowId * cellHeight, cellWidth, cellHeight);
  220.                 this.context.stroke();
  221.                 this.context.closePath();
  222.             }
  223.         }
  224.     }
  225.  
  226.     drawPath(path) {
  227.         let cellWidth = this.field.decomposedField.cellWidth;
  228.         let cellHeight = this.field.decomposedField.cellHeight;
  229.  
  230.         this.context.beginPath();
  231.         this.context.strokeStyle = "rgb(243,10,10, 0.5)";
  232.  
  233.         for (let i = 0; i < path.length - 1; i++) {
  234.             this.context.moveTo(path[i].x * cellWidth + (cellWidth / 2), path[i].y * cellHeight + (cellHeight / 2));
  235.             this.context.lineTo(path[i + 1].x * cellWidth + (cellWidth / 2), path[i + 1].y * cellHeight + (cellHeight / 2));
  236.             this.context.stroke();
  237.         }
  238.         this.context.closePath();
  239.     }
  240.  
  241.     drawPlace(place, isLastPlace = false) {
  242.         let arcColor = "yellow";
  243.         if (isLastPlace) {
  244.             arcColor = "darkgoldenrod";
  245.         }
  246.         this.drawPoint(place, this.getPointRadius(), arcColor, "yellow");
  247.     }
  248.  
  249.     drawPoint(point, radius, arcColor, fillColor, lineWidth = 10) {
  250.         this.context.beginPath();
  251.         this.context.lineWidth = lineWidth;
  252.         if (radius > 0 && radius < 10) {
  253.             this.context.lineWidth = 1;
  254.         }
  255.         this.context.arc(point.x, point.y, radius, 0, 6.28);
  256.         if (lineWidth > 0) {
  257.             this.context.strokeStyle = arcColor;
  258.             this.context.stroke();
  259.         }
  260.         this.context.fillStyle = fillColor;
  261.         this.context.fill();
  262.         this.context.closePath();
  263.     }
  264.  
  265.     drawRectangle(rectangle) {
  266.         this.context.beginPath();
  267.         const [x1, y1] = rectangle.point1.coordinates;
  268.         const [x2, y2] = rectangle.point2.coordinates;
  269.         this.context.rect(x1, y1, x2 - x1, y2 - y1);
  270.         this.context.fillStyle = "black";
  271.         this.context.fill();
  272.         this.context.closePath();
  273.     }
  274.  
  275.     drawLine(startPoint, endPoint, lineWidth, color) {
  276.         this.context.beginPath();
  277.         this.context.lineWidth = lineWidth;
  278.         this.context.strokeStyle = color;
  279.         this.context.moveTo(startPoint.x, startPoint.y);
  280.         this.context.lineTo(endPoint.x, endPoint.y);
  281.         this.context.stroke();
  282.         this.context.closePath();
  283.     }
  284.  
  285.     getPointRadius(k = 1) {
  286.         return this.field.height * 0.005 * k;
  287.     }
  288. }
  289.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement