Created
August 10, 2022 09:26
-
-
Save kitsuyui/5680e053b75647a5fe882d86041962d1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
interface Point { | |
x: number; | |
y: number; | |
} | |
interface Vector { | |
x: number; | |
y: number; | |
} | |
const HorizontalDirections = ["left", "neutral", "right"] as const; | |
const VerticalDirections = ["top", "neutral", "bottom"] as const; | |
type HorizontalDirection = typeof HorizontalDirections[number]; | |
type VerticalDirection = typeof VerticalDirections[number]; | |
interface VectorDirection { | |
horizontal: HorizontalDirection; | |
vertical: VerticalDirection; | |
} | |
function vectorFromTwoPoints(prev: Point, curr: Point): Vector { | |
return { | |
x: curr.x - prev.x, | |
y: curr.y - prev.y, | |
}; | |
} | |
function vectorToDirection(vector: Vector): VectorDirection { | |
const horizontal = HorizontalDirections[Math.sign(vector.x) + 1]; | |
const vertical = VerticalDirections[Math.sign(vector.y) + 1]; | |
return { | |
horizontal, | |
vertical, | |
}; | |
} | |
function moveDirectionFromTwoPoints(prev: Point, curr: Point): VectorDirection { | |
const vector = vectorFromTwoPoints(prev, curr); | |
return vectorToDirection(vector); | |
} | |
// 目的のやつ | |
export function detectMoveDirection( | |
prevX: number, | |
prevY: number, | |
currX: number, | |
currY: number | |
) { | |
const prev = { x: prevX, y: prevY }; | |
const curr = { x: currX, y: currY }; | |
return moveDirectionFromTwoPoints(prev, curr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment