Skip to content

Commit

Permalink
fix: function get rect coordinate
Browse files Browse the repository at this point in the history
  • Loading branch information
peterparkernho committed Feb 3, 2024
1 parent bbe7737 commit 2c1a711
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/modules/PublicSale/luckyMoney/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ export function isPointInsideRotatedObject(
return isInsideConvexHull;
}

export function getRectangleCoordinates(
topLeftX: number,
topLeftY: number,
angleInRadians: number,
width: number,
height: number,
) {
// Calculate the other three corners relative to the top-left point
const topRightX = topLeftX + width * Math.cos(angleInRadians);
const topRightY = topLeftY + width * Math.sin(angleInRadians);

const bottomLeftX = topLeftX - height * Math.sin(angleInRadians);
const bottomLeftY = topLeftY + height * Math.cos(angleInRadians);

const bottomRightX = topRightX - height * Math.sin(angleInRadians);
const bottomRightY = topRightY + height * Math.cos(angleInRadians);

return {
topLeft: { x: topLeftX, y: topLeftY },
topRight: { x: topRightX, y: topRightY },
bottomLeft: { x: bottomLeftX, y: bottomLeftY },
bottomRight: { x: bottomRightX, y: bottomRightY },
};
}

export function getRotatedObjectCoordinates(
centerX: number,
centerY: number,
Expand Down
5 changes: 3 additions & 2 deletions src/modules/PublicSale/luckyMoney/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import dayjs from 'dayjs';

import {
Money,
getRectangleCoordinates,
getRotatedObjectCoordinates,
isPointInsideRotatedObject,
} from './helpers';
Expand Down Expand Up @@ -95,12 +96,12 @@ function LuckyMoney() {

let grabbedIndex = -1;
fallingMoney.forEach(function (money, index) {
const coordinates = getRotatedObjectCoordinates(
const coordinates = getRectangleCoordinates(
money.x,
money.y,
money.angle,
currentImageWidth,
currentImageHeight,
money.angle,
);

if (isPointInsideRotatedObject(mouseX, mouseY, coordinates)) {
Expand Down

0 comments on commit 2c1a711

Please sign in to comment.