Skip to content

Commit

Permalink
fix(critical): isCoordinateInsideBoundingBox (#158)
Browse files Browse the repository at this point in the history
* fix(critical): isCoordinateInsideBoundingBox now supports negative coordinates, and is simpler

* style: using js standard style

---------

Co-authored-by: Helmut (Oertel) Hoffer von Ankershoffen <[email protected]>
  • Loading branch information
helmut-hoffer-von-ankershoffen and Helmut (Oertel) Hoffer von Ankershoffen authored Nov 13, 2024
1 parent 2cf1e62 commit c3c5e0c
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions src/bulkAnnotations/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ export const getViewportBoundingBox = ({ view, pyramid, affine }) => {
}
}

const swapIfGreater = (array1, array2, index) => {
if (array1[index] > array2[index]) {
const temp = array1[index]
array1[index] = array2[index]
array2[index] = temp
}
}

/**
* Check if coordinate is inside bounding box
*
Expand All @@ -55,29 +47,15 @@ export const isCoordinateInsideBoundingBox = (
topLeft,
bottomRight
) => {
const result = !(
Math.abs(topLeft[0]) > Math.abs(coordinate[0]) ||
Math.abs(coordinate[0]) > Math.abs(bottomRight[0]) ||
Math.abs(topLeft[1]) > Math.abs(coordinate[1]) ||
Math.abs(coordinate[1]) > Math.abs(bottomRight[1])
) || !(
Math.abs(bottomRight[0]) > Math.abs(coordinate[0]) ||
Math.abs(coordinate[0]) > Math.abs(topLeft[0]) ||
Math.abs(bottomRight[1]) > Math.abs(coordinate[1]) ||
Math.abs(coordinate[1]) > Math.abs(topLeft[1])
)
if (result === true) {
return result
} else {
swapIfGreater(topLeft, bottomRight, 0)
swapIfGreater(topLeft, bottomRight, 1)
return !(
Math.abs(topLeft[0]) > Math.abs(coordinate[0]) ||
Math.abs(coordinate[0]) > Math.abs(bottomRight[0]) ||
Math.abs(topLeft[1]) > Math.abs(coordinate[1]) ||
Math.abs(coordinate[1]) > Math.abs(bottomRight[1])
)
}
const minX = Math.min(topLeft[0], bottomRight[0])
const maxX = Math.max(topLeft[0], bottomRight[0])
const minY = Math.min(topLeft[1], bottomRight[1])
const maxY = Math.max(topLeft[1], bottomRight[1])

return coordinate[0] >= minX &&
coordinate[0] <= maxX &&
coordinate[1] >= minY &&
coordinate[1] <= maxY
}

/**
Expand Down

0 comments on commit c3c5e0c

Please sign in to comment.