Vector clockwise
Table of contents
# Explanation
First, calculate cross product.
If cross product is negative, the answer is counter-clockwise.
If positive, the answer is clockwise.
If 0, they are on a line and the answer still cannot be detemined.
Second, calculate dot product.
If dot product is negative, the answer is opposite directions.
If not, same directions.
# Code
Receive 2 position vectors and return an integer like below.
- 0: Counter-clockwise
- 1: Clockwise
- 2: Opposite directions
- 3: Same directions
int clockwise(Vector2 &v01, Vector2 &v02) {
double cross_ = v01.cross(v02);
if (cross_ > 0.0) return 0;
else if (cross_ < 0.0) return 1;
double dot_ = v01.dot(v02);
if (v01.dot(v02) < 0.0) return 2;
return 3;
}
Shun
Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!