-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76848ad
commit 52123b5
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
interface Point { | ||
x: number | ||
y: number | ||
} | ||
|
||
export interface RotatedRect { | ||
center: Point | ||
width: number | ||
height: number | ||
rotation: number | ||
} | ||
|
||
interface Rect { | ||
center: Point | ||
width: number | ||
height: number | ||
} | ||
|
||
export function generateApproximatingRects( | ||
rotatedRect: RotatedRect, | ||
numRects: number = 4, | ||
): Rect[] { | ||
const { center, width, height, rotation } = rotatedRect | ||
const rects: Rect[] = [] | ||
|
||
const angleRad = (rotation * Math.PI) / 180 | ||
const cosAngle = Math.cos(angleRad) | ||
const sinAngle = Math.sin(angleRad) | ||
|
||
const normalizedRotation = ((rotation % 360) + 360) % 360 | ||
const sliceAlongWidth = | ||
height <= width | ||
? (normalizedRotation >= 45 && normalizedRotation < 135) || | ||
(normalizedRotation >= 225 && normalizedRotation < 315) | ||
: (normalizedRotation >= 135 && normalizedRotation < 225) || | ||
normalizedRotation >= 315 || | ||
normalizedRotation < 45 | ||
|
||
if (sliceAlongWidth) { | ||
const sliceWidth = width / numRects | ||
|
||
for (let i = 0; i < numRects; i++) { | ||
const x = (i - numRects / 2 + 0.5) * sliceWidth | ||
|
||
const rotatedX = -x * cosAngle | ||
const rotatedY = -x * sinAngle | ||
|
||
const coverageWidth = sliceWidth * 1.1 | ||
const coverageHeight = | ||
Math.abs(height * cosAngle) + Math.abs(sliceWidth * sinAngle) | ||
|
||
rects.push({ | ||
center: { | ||
x: center.x + rotatedX, | ||
y: center.y + rotatedY, | ||
}, | ||
width: coverageWidth, | ||
height: coverageHeight, | ||
}) | ||
} | ||
} else { | ||
const sliceHeight = height / numRects | ||
|
||
for (let i = 0; i < numRects; i++) { | ||
const y = (i - numRects / 2 + 0.5) * sliceHeight | ||
|
||
const rotatedX = -y * sinAngle | ||
const rotatedY = y * cosAngle | ||
|
||
const coverageWidth = | ||
Math.abs(width * cosAngle) + Math.abs(sliceHeight * sinAngle) | ||
const coverageHeight = sliceHeight * 1.1 | ||
|
||
rects.push({ | ||
center: { | ||
x: center.x + rotatedX, | ||
y: center.y + rotatedY, | ||
}, | ||
width: coverageWidth, | ||
height: coverageHeight, | ||
}) | ||
} | ||
} | ||
|
||
return rects | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters