Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Point {
- constructor(x, y) {
- this.coordinates = [x, y];
- }
- }
- class Rectangle {
- constructor(point1, point2) {
- this.point1 = point1;
- this.point2 = point2;
- }
- }
- class DecomposedField {
- constructor(field, cellWidth, cellHeight, useAllAlgorithm) {
- // Реализация DecomposedField
- }
- }
- class Field {
- constructor(height, width) {
- this.changeSize(height, width);
- this.clear();
- }
- changeSize(height, width) {
- this.height = height;
- this.width = width;
- this.decomposedField = null;
- }
- clear() {
- this.robot = null;
- this.places = [];
- this.barriers = [];
- this.decomposedField = null;
- }
- isDecomposed() {
- return this.decomposedField != null;
- }
- placeRobot(x, y) {
- let point = new Point(x, y);
- if (this.pointIsFree(point)) {
- this.robot = new Point(x, y);
- return true;
- }
- return false;
- }
- addBarrier(point1, point2) {
- let rectangle = new Rectangle(point1, point2);
- this.barriers.push(rectangle);
- this.decomposedField = null;
- }
- addPlace(x, y) {
- let newPlace = new Point(x, y);
- if (this.pointIsFree(newPlace)) {
- this.places.push(newPlace);
- return true;
- }
- return false;
- }
- isValidPoint(point) {
- return point.x >= 0 && point.x < this.width && point.y >= 0 && point.y < this.height;
- }
- pointIsFree(point) {
- if (!this.isValidPoint(point)) {
- return false;
- }
- for (let item of this.barriers) {
- if (this.isPointInsideRectangle(point, item) || this.isPointOnRectangle(point, item)) {
- return false;
- }
- }
- return true;
- }
- isPointInsideRectangle(point, rectangle) {
- const [x, y] = point.coordinates;
- const [x1, y1] = rectangle.point1.coordinates;
- const [x2, y2] = rectangle.point2.coordinates;
- return x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && y >= Math.min(y1, y2) && y <= Math.max(y1, y2);
- }
- isPointOnRectangle(point, rectangle) {
- let pointOnLine1 = this.isPointOnLine(point, rectangle.point1, rectangle.point2);
- let pointOnLine2 = this.isPointOnLine(point, rectangle.point2, rectangle.point1);
- return pointOnLine1 || pointOnLine2;
- }
- isPointOnLine(point, linePoint1, linePoint2) {
- const [x, y] = point.coordinates;
- const [x1, y1] = linePoint1.coordinates;
- const [x2, y2] = linePoint2.coordinates;
- return (
- (x - x1) * (y2 - y1) === (x2 - x1) * (y - y1) &&
- x >= Math.min(x1, x2) &&
- x <= Math.max(x1, x2) &&
- y >= Math.min(y1, y2) &&
- y <= Math.max(y1, y2)
- );
- }
- decompose(cellWidth, cellHeight, useAllAlgorithm = false) {
- let decomposedField = new DecomposedField(this, cellWidth, cellHeight, useAllAlgorithm);
- if (decomposedField.composingErrorMessage) {
- return decomposedField.composingErrorMessage;
- }
- this.decomposedField = decomposedField;
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement