Skip to content

Commit

Permalink
add: utils to determine whether click happens inside rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
zenkyuv committed Jan 8, 2024
1 parent 2ae5932 commit 743c84c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions s/context/controllers/compositor/utils/get_pixels_by_angle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function get_pixels_by_angle(x: number, y: number, width: number, height: number, angle: number) {
// var radians1 = angle * Math.PI / 180;
const radians = angle
return [
//upper left
[x + width/2 + width/-2 * Math.cos(radians) - height/-2 * Math.sin(radians), y + height/2 + width/-2 * Math.sin(radians) + height/-2 * Math.cos(radians)],
//upper right
[x + width/2 + width/2 * Math.cos(radians) - height/-2 * Math.sin(radians), y + height/2 + width/2 * Math.sin(radians) + height/-2 * Math.cos(radians)],
//bottom right
[x + width/2 + width/2 * Math.cos(radians) - height/2 * Math.sin(radians), y + height/2 + width/2 * Math.sin(radians) + height/2 * Math.cos(radians)],
//bottom left
[x + width/2 + width/-2 * Math.cos(radians) - height/2 * Math.sin(radians), y + height/2 + width/-2 * Math.sin(radians) + height/2 * Math.cos(radians)],
]
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {get_pixels_by_angle} from "./get_pixels_by_angle.js"

export function is_point_inside_rectangle(clickX: number, clickY: number, x: number, y: number, width: number, height: number, angle: number) {
const corners = get_pixels_by_angle(x, y, width, height, angle);

let inside = false;
for (let i = 0, j = corners.length - 1; i < corners.length; j = i++) {
const xi = corners[i][0], yi = corners[i][1]
const xj = corners[j][0], yj = corners[j][1]

const intersect = ((yi > clickY) !== (yj > clickY)) &&
(clickX < (xj - xi) * (clickY - yi) / (yj - yi) + xi)
if (intersect) inside = !inside
}

return inside
}

0 comments on commit 743c84c

Please sign in to comment.