Skip to content
Snippets Groups Projects
Commit dd70ed04 authored by Miłosz Grocholewski's avatar Miłosz Grocholewski
Browse files

fix(vector-map): fix center point calculation

parent 75202c31
No related branches found
No related tags found
1 merge request!292fix(vector-map): fix center point calculation
/* eslint-disable no-magic-numbers */
import { Coordinate } from 'ol/coordinate';
import getCentroid from './getCentroid';
import getCenter from '@/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter';
describe('getCentroid', () => {
it('should return a centroid for coordinates', () => {
describe('getCenter', () => {
it('should return a center for coordinates', () => {
const coords: Array<Coordinate> = [
[0, 0],
[20, 0],
......@@ -13,9 +13,9 @@ describe('getCentroid', () => {
[0, 20],
];
const result = getCentroid(coords);
const result = getCenter(coords);
expect(result[0]).toBeCloseTo(13.46);
expect(result[1]).toBeCloseTo(12.05);
expect(result[0]).toBeCloseTo(17.5);
expect(result[1]).toBeCloseTo(15);
});
});
/* eslint-disable no-magic-numbers */
import { Coordinate } from 'ol/coordinate';
export default function getCenter(coords: Array<Coordinate>): Coordinate {
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
coords.forEach(([x, y]) => {
if (x < minX) {
minX = x;
}
if (x > maxX) {
maxX = x;
}
if (y < minY) {
minY = y;
}
if (y > maxY) {
maxY = y;
}
});
const centerX = (minX + maxX) / 2;
const centerY = (minY + maxY) / 2;
return [centerX, centerY];
}
/* eslint-disable no-magic-numbers */
import { Coordinate } from 'ol/coordinate';
export default function getCentroid(coords: Array<Coordinate>): Coordinate {
let area = 0;
let centroidX = 0;
let centroidY = 0;
const numPoints = coords.length;
for (let i = 0; i < numPoints; i += 1) {
const x1 = coords[i][0];
const y1 = coords[i][1];
const x2 = coords[(i + 1) % numPoints][0];
const y2 = coords[(i + 1) % numPoints][1];
const crossProduct = x1 * y2 - x2 * y1;
area += crossProduct;
centroidX += (x1 + x2) * crossProduct;
centroidY += (y1 + y2) * crossProduct;
}
area /= 2;
centroidX /= 6 * area;
centroidY /= 6 * area;
return [centroidX, centroidY];
}
......@@ -3,9 +3,9 @@ import Polygon from 'ol/geom/Polygon';
import { Coordinate } from 'ol/coordinate';
import getPolygonCoords from '@/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getPolygonCoords';
import getEllipseCoords from '@/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getEllipseCoords';
import getCentroid from '@/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid';
import { Shape } from '@/types/models';
import { UsePointToProjectionResult } from '@/utils/map/usePointToProjection';
import getCenter from '@/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter';
export default function getShapePolygon({
shape,
......@@ -40,7 +40,7 @@ export default function getShapePolygon({
}
if (mirror) {
const centroid = getCentroid(coords);
const centroid = getCenter(coords);
coords = coords.map(coord => {
const mirroredX = 2 * centroid[0] - coord[0];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment