Fixes bounds for circle

This commit is contained in:
Steve Ruiz 2021-05-12 10:51:53 +01:00
parent 602bd67a74
commit 09dd62c8ec

View file

@ -9,7 +9,7 @@ import * as vec from "./vec"
type BaseShapeUtils<K extends ShapeType> = { type BaseShapeUtils<K extends ShapeType> = {
getBounds(shape: Shapes[K]): Bounds getBounds(shape: Shapes[K]): Bounds
hitTest(shape: Shapes[K], test: number[] | Bounds): boolean hitTest(shape: Shapes[K], test: number[]): boolean
rotate(shape: Shapes[K]): Shapes[K] rotate(shape: Shapes[K]): Shapes[K]
translate(shape: Shapes[K]): Shapes[K] translate(shape: Shapes[K]): Shapes[K]
scale(shape: Shapes[K], scale: number): Shapes[K] scale(shape: Shapes[K], scale: number): Shapes[K]
@ -35,9 +35,6 @@ const DotUtils: BaseShapeUtils<ShapeType.Dot> = {
}, },
hitTest(shape, test) { hitTest(shape, test) {
if ("minX" in test) {
return pointInBounds(shape.point, test)
}
return vec.dist(shape.point, test) < 4 return vec.dist(shape.point, test) < 4
}, },
@ -68,24 +65,19 @@ const CircleUtils: BaseShapeUtils<ShapeType.Circle> = {
} = shape } = shape
return { return {
minX: cx - radius, minX: cx,
maxX: cx + radius, maxX: cx + radius * 2,
minY: cy - radius, minY: cy,
maxY: cy + radius, maxY: cy + radius * 2,
width: radius * 2, width: radius * 2,
height: radius * 2, height: radius * 2,
} }
}, },
hitTest(shape, test) { hitTest(shape, test) {
if ("minX" in test) { return (
const bounds = CircleUtils.getBounds(shape) vec.dist(vec.addScalar(shape.point, shape.radius), test) < shape.radius
return ( )
boundsContain(bounds, test) ||
intersectCircleBounds(shape.point, shape.radius, bounds).length > 0
)
}
return vec.dist(shape.point, test) < 4
}, },
rotate(shape) { rotate(shape) {