[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# determining if 3 points are stored in cw or ccw

Posted on 2005-05-06
Medium Priority
517 Views
Hi Experts.

I have a little problem. I have 3 points (p1, p2, and p3). i need to know if p1-p2-p3 is stored in clockwise or counterclockwise order.
these points are just 2d, so this should be pretty easy

0
Question by:Tai-San
• 3
• 2

LVL 16

Expert Comment

ID: 13943650
You need to compute the cross product of the p2p1 and p2pr vectors. If it is oriented like Z axiz (pozitive value) it's cw. If it is opposed to Z axis (negative value) it's ccw.
0

LVL 16

Expert Comment

ID: 13943656
0

LVL 2

Author Comment

ID: 13943670
float somefloat = Vector_Cross(Vector_Sub(p2, p1), Vector_Sub(p2, p3));
^ is this what you mean, valid
and i check it by:

if (somefloat >= 0)
{
//cw
}
else
{
//ccw
}

did i understand this correctly?
0

LVL 16

Expert Comment

ID: 13943685
Techincaly a cross product between to vecros produce also a vector. The 'somfloat' should be it's norm.
0

LVL 2

Author Comment

ID: 13943692
VECTOR3D_T tResult = Vector_Cross(Vector_Sub(p2, p1), Vector_Sub(p2, p3)); //p1, p2, and p3 must be 3d i suppose. just zero-out Y

//how do i check it then?
0

LVL 48

Accepted Solution

AlexFM earned 200 total points
ID: 13944875
Having three points p0, p1 and p2 calculate:

(p1 - p0) × (p2 - p0) = (x1 - x0)(y2 - y0) - (x2 - x0)(y1 - y0).

If this cross product is positive, then vector p0p1 is clockwise from vector p0p2; if negative, it is counterclockwise.

Positive:

p2
p0
p1

Negative:

p1
p0
p2
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
###### Suggested Courses
Course of the Month19 days, 2 hours left to enroll