From dd70ed0422b0f49b644ecc1e1b2d607427432323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Grocholewski?= <m.grocholewski@atcomp.pl> Date: Mon, 4 Nov 2024 15:09:43 +0100 Subject: [PATCH] fix(vector-map): fix center point calculation --- .../utils/shapes/coords/getCenter.test.ts | 21 ++++++++++++++ .../utils/shapes/coords/getCenter.ts | 29 +++++++++++++++++++ .../utils/shapes/coords/getCentroid.test.ts | 21 -------------- .../utils/shapes/coords/getCentroid.ts | 27 ----------------- .../utils/shapes/elements/getShapePolygon.ts | 4 +-- 5 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter.test.ts create mode 100644 src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter.ts delete mode 100644 src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid.test.ts delete mode 100644 src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid.ts diff --git a/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter.test.ts b/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter.test.ts new file mode 100644 index 00000000..6373f63b --- /dev/null +++ b/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter.test.ts @@ -0,0 +1,21 @@ +/* eslint-disable no-magic-numbers */ +import { Coordinate } from 'ol/coordinate'; +import getCenter from '@/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter'; + +describe('getCenter', () => { + it('should return a center for coordinates', () => { + const coords: Array<Coordinate> = [ + [0, 0], + [20, 0], + [35, 10], + [20, 20], + [10, 30], + [0, 20], + ]; + + const result = getCenter(coords); + + expect(result[0]).toBeCloseTo(17.5); + expect(result[1]).toBeCloseTo(15); + }); +}); diff --git a/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter.ts b/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter.ts new file mode 100644 index 00000000..08296a5a --- /dev/null +++ b/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCenter.ts @@ -0,0 +1,29 @@ +/* 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]; +} diff --git a/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid.test.ts b/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid.test.ts deleted file mode 100644 index e6f1cc8a..00000000 --- a/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -/* eslint-disable no-magic-numbers */ -import { Coordinate } from 'ol/coordinate'; -import getCentroid from './getCentroid'; - -describe('getCentroid', () => { - it('should return a centroid for coordinates', () => { - const coords: Array<Coordinate> = [ - [0, 0], - [20, 0], - [35, 10], - [20, 20], - [10, 30], - [0, 20], - ]; - - const result = getCentroid(coords); - - expect(result[0]).toBeCloseTo(13.46); - expect(result[1]).toBeCloseTo(12.05); - }); -}); diff --git a/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid.ts b/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid.ts deleted file mode 100644 index 686fb618..00000000 --- a/src/components/Map/MapViewer/MapViewerVector/utils/shapes/coords/getCentroid.ts +++ /dev/null @@ -1,27 +0,0 @@ -/* 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]; -} diff --git a/src/components/Map/MapViewer/MapViewerVector/utils/shapes/elements/getShapePolygon.ts b/src/components/Map/MapViewer/MapViewerVector/utils/shapes/elements/getShapePolygon.ts index 3f2a5280..ef748554 100644 --- a/src/components/Map/MapViewer/MapViewerVector/utils/shapes/elements/getShapePolygon.ts +++ b/src/components/Map/MapViewer/MapViewerVector/utils/shapes/elements/getShapePolygon.ts @@ -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]; -- GitLab