# random useful code

Here are a few functions I find myself using constantly lately. Maybe it can be useful to someone.

//gets the angle of a point

``````

float getAngle(float _x, float _y) {
if (_x == 0.0) {
if(_y < 0.0)
return 270;
else
return 90;
} else if (_y == 0) {
if(_x < 0)
return 180;
else
return 0;
}

if ( _y > 0.0)
if (_x > 0.0)
else
return 180.0 - (atan(_y/-_x) * GRAD_PI);
else
if (_x > 0.0)
return 360.0 - (atan(-_y/_x) * GRAD_PI);
else
return 180.0 + (atan(-_y/-_x) * GRAD_PI);
}

``````

//rotates point (start) around origin using angle
this one took me alot of googling to figure out

``````

ofxVec2f rotatePoint(ofxVec2f start, ofxVec2f origin, float angle) {
/*
a counterclockwise rotation of a coordinate or vector about the origin,
where (x,y) is rotated θ and we want to know the coordinates after the rotation, (x',y'):
x' = x cos θ - y sin θ
y' = x sin θ + y cos θ
we need to subtract the center point, do our math and then add it in order to get the
coordinates around a point of origin or else we would get the coordinates around origin (0,0)*/
ofxVec2f result;
result.x = (cos(angle*PI/180.0) * (start.x - origin.x) - sin(angle*PI/180.0) * (start.y - origin.y)) + origin.x;
result.y = (cos(angle*PI/180.0) * (start.y - origin.y) + sin(angle*PI/180.0) * (start.x - origin.x)) + origin.y;
return result;
}

``````

//returns true if point p is inside polygon (N is the number of corners)
I usually use another version I did using vector and I get the N using vector.size() but this is the base I started with
http://local.wasp.uwa.edu.au/~pbourke/g-…-nsidepoly/

``````

bool InsidePolygon(ofPoint *polygon, int N, ofPoint p)
{
int counter = 0;
int i;
double xinters;
ofPoint p1,p2;

p1 = polygon;
for (i=1;i<=N;i++) {
p2 = polygon[i % N];
if (p.y > MIN(p1.y,p2.y)) {
if (p.y <= MAX(p1.y,p2.y)) {
if (p.x <= MAX(p1.x,p2.x)) {
if (p1.y != p2.y) {
xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
if (p1.x == p2.x || p.x <= xinters)
counter++;
}
}
}
}
p1 = p2;
}

if (counter % 2 == 0) return false;
else return true;
}

``````

hey!

I wanted to thank you for posting this code, I am specifically using now your point -> angle function, and I had been struggling against this for a long time now, so your code really helped me.

I just had to change the function a bit, so that the point position would not be only relative to 0.0, but to a specific point in the window (x0,y0) (which has to be passed in to the function additionally), like this:

``````
float getAngle(float _x, float _y,float _x0, float _y0) {
if (_x-_x0 == 0.0) {
if(_y-_y0 < 0.0)
return 270;
else
return 90;
} else if (_y-_y0 == 0) {
if(_x-_x0 < 0)
return 180;
else
return 0;
}

if ( _y-_y0 > 0.0)
if (_x-_x0 > 0.0)
else
return 180.0 - (atan((_y-_y0)/-(_x-_x0)) * GRAD_PI);
else
if (_x-_x0 > 0.0)
return 360.0 - (atan(-(_y-_y0)/(_x-_x0)) * GRAD_PI);
else
return 180.0 + (atan(-(_y-_y0)/-(_x-_x0)) * GRAD_PI);
}

``````

I also have to add that the GRAD_PI constant is not recognized by default, so I thought it would be nice to state its value in this thread:

``````

``````

Thanks again!

Rodrigo

I just have to add that the title of this thread makes it specifically hard to find! One must to come upon the magic-keywords by chance! (no criticism intended)

hahaha

Cheers

Thanks for posting the code.

People who dig this might also want to look into the ofxVectorMath addon.

It has methods like the following and many more (in 2D and 3D):

rotate a point around a pivot point:
ofxPoint2f& rotate( float angle, const ofxPoint2f& pivot )

get the angle between two vectors:
float angle( const ofxVec2f& vec )