2021-05-11 10:13:07 +00:00
|
|
|
import {
|
|
|
|
boundsCollide,
|
|
|
|
boundsContain,
|
|
|
|
pointInBounds,
|
|
|
|
} from "state/sessions/brush-session"
|
2021-05-12 09:18:11 +00:00
|
|
|
import { Bounds, ShapeType, Shapes } from "types"
|
2021-05-11 10:13:07 +00:00
|
|
|
import { intersectCircleBounds } from "./intersections"
|
|
|
|
import * as vec from "./vec"
|
|
|
|
|
|
|
|
type BaseShapeUtils<K extends ShapeType> = {
|
|
|
|
getBounds(shape: Shapes[K]): Bounds
|
2021-05-12 09:51:53 +00:00
|
|
|
hitTest(shape: Shapes[K], test: number[]): boolean
|
2021-05-11 10:13:07 +00:00
|
|
|
rotate(shape: Shapes[K]): Shapes[K]
|
|
|
|
translate(shape: Shapes[K]): Shapes[K]
|
|
|
|
scale(shape: Shapes[K], scale: number): Shapes[K]
|
|
|
|
stretch(shape: Shapes[K], scaleX: number, scaleY: number): Shapes[K]
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------- Dot ---------------------- */
|
|
|
|
|
|
|
|
const DotUtils: BaseShapeUtils<ShapeType.Dot> = {
|
|
|
|
getBounds(shape) {
|
|
|
|
const {
|
|
|
|
point: [cx, cy],
|
|
|
|
} = shape
|
|
|
|
|
|
|
|
return {
|
2021-05-12 09:52:20 +00:00
|
|
|
minX: cx,
|
|
|
|
maxX: cx + 4,
|
|
|
|
minY: cy,
|
|
|
|
maxY: cy + 4,
|
2021-05-11 10:13:07 +00:00
|
|
|
width: 4,
|
|
|
|
height: 4,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape, test) {
|
|
|
|
return vec.dist(shape.point, test) < 4
|
|
|
|
},
|
|
|
|
|
|
|
|
rotate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
translate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
scale(shape, scale: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
stretch(shape, scaleX: number, scaleY: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------- Circle --------------------- */
|
|
|
|
|
|
|
|
const CircleUtils: BaseShapeUtils<ShapeType.Circle> = {
|
|
|
|
getBounds(shape) {
|
|
|
|
const {
|
|
|
|
point: [cx, cy],
|
|
|
|
radius,
|
|
|
|
} = shape
|
|
|
|
|
|
|
|
return {
|
2021-05-12 09:51:53 +00:00
|
|
|
minX: cx,
|
|
|
|
maxX: cx + radius * 2,
|
|
|
|
minY: cy,
|
|
|
|
maxY: cy + radius * 2,
|
2021-05-11 10:13:07 +00:00
|
|
|
width: radius * 2,
|
|
|
|
height: radius * 2,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape, test) {
|
2021-05-12 09:51:53 +00:00
|
|
|
return (
|
|
|
|
vec.dist(vec.addScalar(shape.point, shape.radius), test) < shape.radius
|
|
|
|
)
|
2021-05-11 10:13:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
rotate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
translate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
scale(shape, scale: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
stretch(shape, scaleX: number, scaleY: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------- Ellipse -------------------- */
|
|
|
|
|
|
|
|
const EllipseUtils: BaseShapeUtils<ShapeType.Ellipse> = {
|
|
|
|
getBounds(shape) {
|
|
|
|
return {
|
|
|
|
minX: 0,
|
|
|
|
minY: 0,
|
|
|
|
maxX: 0,
|
|
|
|
maxY: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape) {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
|
|
|
rotate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
translate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
scale(shape, scale: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
stretch(shape, scaleX: number, scaleY: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------- Line ---------------------- */
|
|
|
|
|
|
|
|
const LineUtils: BaseShapeUtils<ShapeType.Line> = {
|
|
|
|
getBounds(shape) {
|
|
|
|
return {
|
|
|
|
minX: 0,
|
|
|
|
minY: 0,
|
|
|
|
maxX: 0,
|
|
|
|
maxY: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape) {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
|
|
|
rotate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
translate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
scale(shape, scale: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
stretch(shape, scaleX: number, scaleY: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------- Ray ---------------------- */
|
|
|
|
|
|
|
|
const RayUtils: BaseShapeUtils<ShapeType.Ray> = {
|
|
|
|
getBounds(shape) {
|
|
|
|
return {
|
|
|
|
minX: Infinity,
|
|
|
|
minY: Infinity,
|
|
|
|
maxX: Infinity,
|
|
|
|
maxY: Infinity,
|
|
|
|
width: Infinity,
|
|
|
|
height: Infinity,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape) {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
|
|
|
rotate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
translate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
scale(shape, scale: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
stretch(shape, scaleX: number, scaleY: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------ Line Segment ------------------ */
|
|
|
|
|
2021-05-12 09:18:11 +00:00
|
|
|
const PolylineUtils: BaseShapeUtils<ShapeType.Polyline> = {
|
2021-05-11 10:13:07 +00:00
|
|
|
getBounds(shape) {
|
2021-05-12 09:18:11 +00:00
|
|
|
let minX = 0
|
|
|
|
let minY = 0
|
|
|
|
let maxX = 0
|
|
|
|
let maxY = 0
|
|
|
|
|
|
|
|
for (let [x, y] of shape.points) {
|
|
|
|
minX = Math.min(x, minX)
|
|
|
|
minY = Math.min(y, minY)
|
|
|
|
maxX = Math.max(x, maxX)
|
|
|
|
maxY = Math.max(y, maxY)
|
|
|
|
}
|
|
|
|
|
2021-05-11 10:13:07 +00:00
|
|
|
return {
|
2021-05-12 09:18:11 +00:00
|
|
|
minX: minX + shape.point[0],
|
|
|
|
minY: minY + shape.point[1],
|
|
|
|
maxX: maxX + shape.point[0],
|
|
|
|
maxY: maxY + shape.point[1],
|
|
|
|
width: maxX - minX,
|
|
|
|
height: maxY - minY,
|
2021-05-11 10:13:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape) {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
|
|
|
rotate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
translate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
scale(shape, scale: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
stretch(shape, scaleX: number, scaleY: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------- Rectangle ------------------- */
|
|
|
|
|
|
|
|
const RectangleUtils: BaseShapeUtils<ShapeType.Rectangle> = {
|
|
|
|
getBounds(shape) {
|
|
|
|
const {
|
|
|
|
point: [x, y],
|
|
|
|
size: [width, height],
|
|
|
|
} = shape
|
|
|
|
|
|
|
|
return {
|
|
|
|
minX: x,
|
|
|
|
maxX: x + width,
|
|
|
|
minY: y,
|
|
|
|
maxY: y + height,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape) {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
|
|
|
rotate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
translate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
scale(shape, scale: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
stretch(shape, scaleX: number, scaleY: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const shapeUtils: { [K in ShapeType]: BaseShapeUtils<K> } = {
|
|
|
|
[ShapeType.Dot]: DotUtils,
|
|
|
|
[ShapeType.Circle]: CircleUtils,
|
|
|
|
[ShapeType.Ellipse]: EllipseUtils,
|
|
|
|
[ShapeType.Line]: LineUtils,
|
|
|
|
[ShapeType.Ray]: RayUtils,
|
2021-05-12 09:18:11 +00:00
|
|
|
[ShapeType.Polyline]: PolylineUtils,
|
2021-05-11 10:13:07 +00:00
|
|
|
[ShapeType.Rectangle]: RectangleUtils,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default shapeUtils
|