Geometry> Clockwise angle calculation of two vectors> Link: python implementation / C implementation

Related http://qiita.com/7of9/items/dd241297a7231485767d

Calculate the angle between one vector and another. The angle is in the clockwise direction.

Below is the python implementation. http://stackoverflow.com/questions/31735499/calculate-angle-clockwise-between-two-points

The determinant comes out. Ad-bc for 2x2. For 3x3, use Saras's formula. http://www.mathsisfun.com/algebra/matrix-determinant.html


I made the above python implementation the C implementation I need. Naming is quite appropriate.

http://ideone.com/CWu8Z1

#include <stdio.h>
#include <math.h>

float get_length(float vs[2])
{
	return sqrt( pow(vs[0],2) + pow(vs[1],2) );
}
float get_dot_product(float vs[2], float ws[2])
{
	return vs[0]*ws[0] + vs[1]*ws[1];	
}
float get_determinant(float vs[2], float ws[2])
{
	return vs[0]*ws[1] - vs[1]*ws[0];	
}
float get_inner_product(float vs[2], float ws[2])
{
	float cosx = get_dot_product(vs,ws);
	cosx = cosx / (get_length(vs) * get_length(ws));
	float rad = acos(cosx);
	return rad * 180.0 / acos(-1.0);
}
float get_angle_clockwise(float As[2], float Bs[2])
{
	float inner = get_inner_product(As, Bs);
	float det = get_determinant(As, Bs);
	if (det < 0.0) {
		return inner;
	} else {
		return 360.0 - inner;
	}
}

float get_angle_clockwise_wrapper(float x1, float x2, float y1, float y2)
{
	float Xs[2];
	float Ys[2];
	
	Xs[0] = x1;
	Xs[1] = x2;
	Ys[0] = y1;
	Ys[1] = y2;
	return get_angle_clockwise(Xs, Ys);
}

int main(void) {
	printf("%f \n", get_angle_clockwise_wrapper(0,1, 1,0));
	printf("%f \n", get_angle_clockwise_wrapper(1,0, 0,1));
	printf("%f \n", get_angle_clockwise_wrapper(0,1, 1,1));

	return 0;
}

result


Success	time: 0 memory: 2156 signal:0
90.000000 
270.000000 
45.000000 

In the above python implementation (and C implementation based on it), if two vectors overlap, the angle will be 360 degrees instead of 0 degrees </ font> This area needs to be corrected as appropriate.

Recommended Posts

Geometry> Clockwise angle calculation of two vectors> Link: python implementation / C implementation
Python implementation of particle filters
Implementation of quicksort in Python
Comparison of calculation speed by implementation of python mpmath (arbitrary precision calculation) (Note)
Python implementation of self-organizing particle filters
Implementation of life game in Python
Implementation of desktop notifications using Python
implementation of c / c ++> RingBuffer (N margins)
Python implementation of non-recursive Segment Tree
Implementation of Light CNN (Python Keras)
Implementation of Dijkstra's algorithm with python
[Python] Calculation of Kappa (k) coefficient
[Unity (C #), Python] API communication study memo ③ Implementation of simplified login function