stickineisdfjsdf

pull/3284/head
Lu[ke] Wilson 2024-03-28 12:43:56 +00:00
rodzic ff97a0c944
commit b2c0670fae
2 zmienionych plików z 37 dodań i 7 usunięć

Wyświetl plik

@ -91,7 +91,11 @@ import { Vec, VecLike } from '../primitives/Vec'
import { EASINGS } from '../primitives/easings'
import { Geometry2d } from '../primitives/geometry/Geometry2d'
import { Group2d } from '../primitives/geometry/Group2d'
import { intersectPolygonPolygon } from '../primitives/intersect'
import {
intersectPolygonBounds,
intersectPolygonPolygon,
intersectPolylineBounds,
} from '../primitives/intersect'
import { PI2, approximately, areAnglesCompatible, clamp, pointInPolygon } from '../primitives/utils'
import { ReadonlySharedStyleMap, SharedStyle, SharedStyleMap } from '../utils/SharedStylesMap'
import { WeakMapCache } from '../utils/WeakMapCache'
@ -5038,14 +5042,21 @@ export class Editor extends EventEmitter<TLEventMap> {
if (shapeBounds.w * shapeBounds.h > otherBounds.w * otherBounds.h * 2) continue
// don't stick if your geometry doesn't intersect the other shape's geometry
// TODO: make this actually work!
const otherBoundsInShapeSpace = Box.FromPoints(
otherBounds.corners.map((v) => this.getPointInShapeSpace(shape, v))
)
const shapeGeometry = this.getShapeGeometry(shape)
const shapePageTransform = this.getShapePageTransform(shape)
if (otherBoundsInShapeSpace.contains(shapeGeometry.bounds)) return other
if (
!shapeGeometry.vertices.some((v) => {
const point = shapePageTransform.applyToPoint(v)
return otherBounds.containsPoint(point)
})
shapeGeometry.isClosed &&
!intersectPolygonBounds(shapeGeometry.vertices, otherBoundsInShapeSpace)
) {
continue
} else if (
!shapeGeometry.isClosed &&
!intersectPolylineBounds(shapeGeometry.vertices, otherBoundsInShapeSpace)
) {
continue
}

Wyświetl plik

@ -223,6 +223,25 @@ export function intersectPolygonBounds(points: VecLike[], bounds: Box) {
return result
}
/**
* Find the intersections between a polyline and a bounding box.
*
* @public
*/
export function intersectPolylineBounds(points: VecLike[], bounds: Box) {
const result: VecLike[] = []
let segmentIntersection: VecLike[] | null
for (const side of bounds.sides) {
segmentIntersection = intersectLineSegmentPolyline(side[0], side[1], points)
if (segmentIntersection) result.push(...segmentIntersection)
}
if (result.length === 0) return null // no intersection
return result
}
function ccw(A: VecLike, B: VecLike, C: VecLike) {
return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x)
}