// Point inside polygon
// Returns true iff p is inside poly.
// PRE: The vertices of poly are ordered (either clockwise or counter-clockwise)
// POST: Modify code inside to handle the special case of "on an edge".
// REQUIRES: polarAngle(), #include <math.h>, #include <vector>, #define EPS 0.000000001
bool pointInPoly(P p, vector<P> &poly) {
	int n = poly.size();
	double ang = 0.0;
	for (int i = n - 1, j = 0; j < n; i = j++) {
		P v(poly[i].x - p.x, poly[i].y - p.y);
		P w(poly[j].x - p.x, poly[j].y - p.y);
		double va = polarAngle(v);
		double wa = polarAngle(w);
		double xx = wa - va;
		if (va < -0.5 || wa < -0.5 || fabs(fabs(xx) - 2 * acos(0)) < EPS){
			// POINT IS ON THE EDGE
			assert(false);
			ang += 2 * acos(0);
			continue;
		}
		if (xx < -2 * acos(0)) ang += xx + 4 * acos(0);
		else if (xx > 2 * acos(0)) ang += xx - 4 * acos(0);
		else ang += xx;
	}
	return (ang * ang > 1.0);
}
int main()
{
   float xcoord, ycoord;
   float pRadius;
   float pAngle;

   printf("Enter your x and y coordinates: ");
   scanf("%f %f", &xcoord, &ycoord);

   pAngle = polarAngle(xcoord, ycoord);
   pRadius = polarRadius(xcoord, ycoord);

   printf("The polar coordinates are (%.2f, %.2f deg)\n", pRadius, pAngle);
}
Пример #3
0
void Arrow::adaptTriangle() const
{
	// Don't draw triangle when arrow is too short or when arrow is actually a line
	if (mLength <= zeroVectorTolerance || mStyle == Line)
	{
		mTriangle = sf::ConvexShape();
	}
	
	// Draw normal triangle
	else
	{
		const float end   = std::max(mLength, getTriangleHeight());
		const float begin = end - getTriangleHeight();

		mTriangle.setFillColor(mColor);
		mTriangle.setRotation(polarAngle(mDirection));

		mTriangle.setPointCount(3);
		mTriangle.setPoint(0, sf::Vector2f(end, 0.f));
		mTriangle.setPoint(1, sf::Vector2f(begin,  1.5f * mThickness));
		mTriangle.setPoint(2, sf::Vector2f(begin, -1.5f * mThickness));
	}
}