Esempio n. 1
0
	double Triangle2D::distance(const ofxVec2f &v) {
		int sameHalfPlanes = 0;
		Line2D *line;
		ofxVec2f end;
		if (pAB.positionTo(v) == pSign) {
			sameHalfPlanes++;
		}
		else {
			line = &pAB;
			end = pBC.getOrigin();
		}
		if (pBC.positionTo(v) == pSign) {
			sameHalfPlanes++;
		}
		else {
			line = &pBC;
			end = pCA.getOrigin();
		}
		if (pCA.positionTo(v) == pSign) {
			sameHalfPlanes++;
		}
		else {
			line = &pCA;
			end = pAB.getOrigin();
		}

		if (sameHalfPlanes == 3) {
			// point is inside the triangle
			return 0;
		}
		else if (sameHalfPlanes == 2) {
			// point could have a projection on a side
			ofxVec2f vProjected = line->projectTo(v);
			bool vProjectedInSegment =
				((line->getOrigin().x <= vProjected.x && vProjected.x <= end.x)
				|| (line->getOrigin().x >= vProjected.x && vProjected.x >= end.x))
				&&
				((line->getOrigin().y <= vProjected.y && vProjected.y <= end.y)
				|| (line->getOrigin().y >= vProjected.y && vProjected.y >= end.y));

			if (vProjectedInSegment) {
				return line->distance(v);
			}
		}
		return MIN(MIN(pAB.getOrigin().distance(v), pBC.getOrigin().distance(v)), pCA.getOrigin().distance(v));
	}
Esempio n. 2
0
int main()
{
    for(auto& p : points)
        cout << l2.distance(p.coord) << ", ";
    cout << endl;

    std::multiset<Point> s(points.begin(), points.end());
    cout << std::distance(points.begin(), points.end()) << endl;
    cout << s.size() << endl;
    for(auto& p : s)
        cout << p.num << " < ";
    cout << endl;

    Line2D ll(Coord2D(2, 1), Coord2D(1, 3));
    Line2D ly(INFINITY, 0);
    Line2D lx(0, 0);

    cout << ll.cross(ly).toString() << endl;
    cout << ll.cross(lx).toString() << endl;

    Line2D l45(Coord2D(0, 0), Vector2D::unit(3.14159/4));
    Line2D lv1(Coord2D(1, 0), INFINITY);
    cout << Line2D::cross(l45, lv1).toString() << endl;

    auto radian = Vector2D(-1, -1).unit().radian();
    cout << radian << endl;
    cout << Vector2D::unit(radian).toString() << endl;


    auto pos = Coord2D(1, 2);
    auto dir = Vector2D(-1, -1).unit();
    auto length = 5;

    Line2D l0(pos, dir);
    auto c0x = Line2D::cross(lx, l0);
    auto c0y = Line2D::cross(ly, l0);
    cout << Line2D::cross(ly, l0).toString() << endl;
    cout << Line2D::cross(lx, l0).toString() << endl;


    return 0;
}
Esempio n. 3
0
bool operator < (const Point& p1, const Point& p2)
{
    return l.distance(p1.coord) < l.distance(p2.coord);
}