Check vector direction

Hi,
I have two vectors, how I can check that vectors same direction as the hands of an analogue clock

Thanks

image

hey

you can substract them and check if the result is zero. if is, they are parallel.

V(1,0,0) - V(1,0,0) = 0,0,0 parallel (same direction)
V(1,0,0) - V(0,1,0) = 1,-1,0 not parallel (not same direction)

or with this method:
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Vector3d_IsParallelTo.htm

hope that helps

B

Hi @benedict ,

How I can check the angle of that vectors is follow clockwise

Thanks

I’m not sure to understand your question. you mean an angle between the two vectors that should be smaller than 180°?

Hi @benedict
Please see picture below

image

you could cross product 2 vectors, then check the reslt direction

Another way is to measure the angles of the vectors against whatever 12:00/24:00 is:


211026_ClockAngles_00.gh (13.0 KB)

2 Likes

to have a clockwise or counterclockwise direction you need a plane, else its several transformations, which cannot be simplified to a simple rotation direction. also you need to know in which quadrant you are, as conditions repeat themselves, when just comparing the vectors.

however, in a simple xy plane the conditions to check the direction would be the following:

clockwise
V0 V1 crossproduct quadrant
1,0,0 0,1,0 V0.x>V1.x V0.y<V1.y 1,1,0 1
0,1,0 -1,0,0 V0.x>V1.x V0.y<V1.y -1,1,0 4
-1,0,0 0,-1,0 V0.x<V1.x V0.y>V1.y -1,-1,0 3
0,-1,0 1,0,0 V0.x<V1.x V0.y<V1.y 1,-1,0 2
counterclockwise
V0 V1
1,0,0 0,-1,0 V0.x>V1.x V0.y<V1.y 1,-1,0 2
0,-1,0 -1,0,0 V0.x>V1.x V0.y<V1.y -1,-1,0 3
-1,0,0 0,1,0 V0.x<V1.x V0.y<V1.y -1,1,0 4
0,1,0 1,0,0 V0.x<V1.x V0.y>V1.y 1,1,0 1

:

here you go with the example: please note that its working on xy plane only and ignores 180°, as it can be considered none of both conditions.

Vectoranalysis.gh (10.2 KB)

oh, bugger, I got the quadrants wrong numbered…

@AndersDeleuran 's solution looks way easier though:) nice one!

3 Likes

Based on your drawing:
The blue and the red vectors always have the same direction.
And how do you want to know the orientation (clockwise or counterclockwise) of a vector? you need a reference

image

The angle have no relation with the orientation.

image

I think you need to post the real example or the real idea and what you want exactly

1 Like

I think you confuse something here, the red and blue vector do not have the same direction in your drawing and are compared to each other. having the first one as a reference, you can easily determine whether its rotating clockwise or counter clockwise. With @AndersDeleuran 's method you’d repeat the process with both vectors and check if the references vectors angle is bigger than the references angle or smaller.

How do you know if the blue axis orientation is clockwise or counterclockwise?
We have two possibilities here

image

I guess counterclockwise in this case, as alpha is smaller than beta. at least that’s what I understood from his question. given that just getting the angle of the two vectors with the default ‘vector angle’ node gives you the smaller angle, otherwise it would be the reflex angle
"

but I can see your point from math’s point of view
:

We can’t know even Grasshopper without reference can’t know , that why we have Angle and Reflex in this component

image

It’s like : is this circle orientation clockwise or counterclockwise?!

image

https://www.mathsisfun.com/reflex.html

reflex is always the larger angle. I think he’s looking for the shortest angle TRANSFORMATION.

looking at my example above, you can see the 90° angle between the vectors in both cases, but it’s not clear when referencing to the first vector (the one you want to transform) which of the two angles (angle or reflex) is needed to get the new vector.
in counterclockwise case, you’d use the acute angle to transform, while in clockwise you’d use the reflex angle to transform.

angles for transformations do have a direction and are counter clockwise.

1 Like

@AndersDeleuran @anon39580149 @nguyentheta2002

finally, because I know you’re interested in that kind of stuff and I learned this today which made me think of this thread:

the direction of the second vector in relation from the first vector of two vectors is quite important to know for algorithms like hull ( I think graham’s, but not sure at all). it’s very simple to calculate by using the crossproduct of two vectors:

public Vector3d xProduct(Vector3d u, Vector3d v)
{
double cx = (u.Y * v.Z) - (u.Z * v.Y);
double cy = (u.Z * v.X) - (u.X * v.Z);
double cz = (u.X * v.Y) - (u.Y * v.X);
Vector3d xVector = new Vector3d(cx, cy, cz);
return xVector;
}

the same thing can be done by using

Vector3d.Crossproduct(u,v);

if the result (of the crossvectors Z value) is positive, it is clockwise. if it’s negative its counterclockwise and if it’s zerolength, it’s straight.

by consequence, a polyline defined by three points can be analysed if relative from the first line it makes a right or left turn to the third point. the vectors are created and translated to center to repeat get the crossproduct:

public string direction(Point3d A, Point3d B, Point3d C)
{
string sOut = “nullcheck”;
Vector3d u = new Vector3d(B - A);
Vector3d v = new Vector3d(C - B);
Vector3d uxv = xProduct(u, v);
if(Math.Round(uxv.Z, 4) < 0) sOut = “right”;
if(Math.Round(uxv.Z, 4) == 0) sOut = “straight”;
if(Math.Round(uxv.Z, 4) > 0) sOut = “left”;
return sOut;
}

given the points are in xy plane.

the references and proof as well as more acurate information on the topic can be found here:

cheers and happy new year

Ben

1 Like