From 4c301604fc5cd07c987dbbe0c1c009ce73959f84 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Fri, 20 Nov 2020 12:00:59 +1100 Subject: [PATCH] Added alternating diagonals map measurement --- src/components/map/MapMeasure.js | 6 ++++-- .../map/controls/MeasureToolSettings.js | 9 +++++++++ src/helpers/vector2.js | 8 +++++++- src/icons/MeasureAlternatingIcon.js | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/icons/MeasureAlternatingIcon.js diff --git a/src/components/map/MapMeasure.js b/src/components/map/MapMeasure.js index e27541c..54ef535 100644 --- a/src/components/map/MapMeasure.js +++ b/src/components/map/MapMeasure.js @@ -77,9 +77,11 @@ function MapMeasure({ map, selectedToolSettings, active, gridSize }) { brushPosition, gridSize ); + // Round the grid positions to the nearest 0.1 to aviod floating point issues + const precision = { x: 0.1, y: 0.1 }; const length = Vector2.distance( - Vector2.divide(points[0], gridSize), - Vector2.divide(points[1], gridSize), + Vector2.roundTo(Vector2.divide(points[0], gridSize), precision), + Vector2.roundTo(Vector2.divide(points[1], gridSize), precision), selectedToolSettings.type ); setDrawingShapeData({ diff --git a/src/components/map/controls/MeasureToolSettings.js b/src/components/map/controls/MeasureToolSettings.js index cb7a0e5..b163a01 100644 --- a/src/components/map/controls/MeasureToolSettings.js +++ b/src/components/map/controls/MeasureToolSettings.js @@ -5,6 +5,7 @@ import ToolSection from "./ToolSection"; import MeasureChebyshevIcon from "../../../icons/MeasureChebyshevIcon"; import MeasureEuclideanIcon from "../../../icons/MeasureEuclideanIcon"; import MeasureManhattanIcon from "../../../icons/MeasureManhattanIcon"; +import MeasureAlternatingIcon from "../../../icons/MeasureAlternatingIcon"; import Divider from "../../Divider"; @@ -19,6 +20,8 @@ function MeasureToolSettings({ settings, onSettingChange }) { onSettingChange({ type: "euclidean" }); } else if (key === "c") { onSettingChange({ type: "manhattan" }); + } else if (key === "a") { + onSettingChange({ type: "alternating" }); } } @@ -31,6 +34,12 @@ function MeasureToolSettings({ settings, onSettingChange }) { isSelected: settings.type === "chebyshev", icon: , }, + { + id: "alternating", + title: "Alternating Diagonal Distance (A)", + isSelected: settings.type === "alternating", + icon: , + }, { id: "euclidean", title: "Line Distance (L)", diff --git a/src/helpers/vector2.js b/src/helpers/vector2.js index 8eecb65..c2a6561 100644 --- a/src/helpers/vector2.js +++ b/src/helpers/vector2.js @@ -363,7 +363,7 @@ export function compare(a, b, threshold) { * Returns the distance between two vectors * @param {Vector2} a * @param {Vector2} b - * @param {string} type - `chebyshev | euclidean | manhattan` + * @param {string} type - `chebyshev | euclidean | manhattan | alternating` */ export function distance(a, b, type) { switch (type) { @@ -373,6 +373,12 @@ export function distance(a, b, type) { return length(subtract(a, b)); case "manhattan": return Math.abs(a.x - b.x) + Math.abs(a.y - b.y); + case "alternating": + // Alternating diagonal distance like D&D 3.5 and Pathfinder + const delta = abs(subtract(a, b)); + const ma = max(delta); + const mi = min(delta); + return ma - mi + Math.floor(1.5 * mi); default: return length(subtract(a, b)); } diff --git a/src/icons/MeasureAlternatingIcon.js b/src/icons/MeasureAlternatingIcon.js new file mode 100644 index 0000000..a65e504 --- /dev/null +++ b/src/icons/MeasureAlternatingIcon.js @@ -0,0 +1,18 @@ +import React from "react"; + +function MeasureAlternatingIcon() { + return ( + + + + + ); +} + +export default MeasureAlternatingIcon;