Answer the question
In order to leave comments, you need to log in
How to divide a polygon on the map into equal parts or continuation: how to collect an array from the coordinates of the selected area with a given step?
Colleagues, good day.
So the task is to divide the polygon into equal sectors!
In continuation of the topic How to collect an array from the coordinates of the selected area ... there was one more question. Many thanks to comrade @serginhold for help and my understanding))))))))))
So! Building a map on a leaflet. We select an area on it, draw the simplest rectangle - a polygon, we have its corners in coordinates:
this.rectangle = L.rectangle(bounds, this.options).addTo(this.map); // строим прямоугольник - полигон
let coordsConners = this.rectangle.getLatLngs()[0]; // координаты углов
getLines(southWest, southEast, northEast) { // получаем длины вертикальной и горизонтальной линии
this.points = [
new L.LatLng(southWest[0], southWest[1]),
new L.LatLng(southEast[0], southEast[1]),
new L.LatLng(northEast[0], northEast[1])
];
this.rectangleData.horizont = +(this.points[0].distanceTo(this.points[1])).toFixed(0);
this.rectangleData.vertical = +(this.points[1].distanceTo(this.points[2])).toFixed(0);
}
let x_min = coordsConners[1].lat;
let x_max = coordsConners[3].lat;
let y_min = coordsConners[1].lng;
let y_max = coordsConners[3].lng;
let x = 0;
let y = 0;
let count = 100;
for (let i = 0; i < count; i++) {
x = Math.random() * (x_max - x_min) + x_min;
y = Math.random() * (y_max - y_min) + y_min;
new L.marker([x, y]).addTo(this.map);
}
Answer the question
In order to leave comments, you need to log in
Is it necessary?
const RECOMMEND_SIZE = 10;
const BIAS_TO_CENTER = 0.5;
let x_min = coordsConners[1].lat;
let x_max = coordsConners[3].lat;
let y_min = coordsConners[1].lng;
let y_max = coordsConners[3].lng;
let x_side_len = x_max - x_min;
let y_side_len = y_max - y_min;
let x_total_count = Math.round(x_len / RECOMMEND_SIZE);
let y_total_count = Math.round(y_len / RECOMMEND_SIZE);
let x_closest_size = x_side_len / x_total_count;
let y_closest_size = y_side_len / y_total_count;
for (let x = 0; x < x_total_count; x++) {
for (let y = 0; y < y_total_count; y++) {
let coord_x = x_min + x_closest_size * (x + BIAS_TO_CENTER);
let coord_y = y_min + y_closest_size * (y + BIAS_TO_CENTER);
new L.marker([coord_x, coord_y]).addTo(this.map);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question