virtual void run(int subcase) {
		typedef Point<double> P;
		P r;
		check(lineIntersection(P(0,0),P(1,0),P(0,2),P(1,1),r),1);
		if (!(r==P(2,0)))
			fail("1");
		check(lineIntersection(P(0,0),P(1,0),P(6,-1),P(0,2),r),1);
		if (!(r==P(4,0)))
			fail("2");
		check(lineIntersection(P(1,2),P(3,4),P(-5,1),P(-3,2),r),1);
		if (!(r==P(5,6)))
			fail("3");
		check(lineIntersection(P(1,2),P(2,4),P(5,-1),P(1,-9),r),0);
		check(lineIntersection(P(1,2),P(2,4),P(9,18),P(7,14),r),-1);
	}
Exemple #2
0
bool Obstacles::findIntersectionFarthest(int polygonIndex, Vector2 from, Vector2 to,
                                         int *outVertexIndex, float *outDistance, Vector2 *out) const
{
	float   maxDistance = 0.0f;
	Vector2 maxIntersection;
	int     maxVertexIndex = -1;

	bool hasDistance = false;

	for (int i = 0; i < _polygons[polygonIndex].verticeCount; ++i) {
		int nextIndex = (i + 1) % _polygons[polygonIndex].verticeCount;
		Vector2 *vertices = _polygons[polygonIndex].vertices;
		Vector2 intersection;
		bool intersects = lineIntersection(from, to, vertices[i], vertices[nextIndex], &intersection);
		if (intersects) {
			float distance2 = distance(from, intersection);
			if (!hasDistance || distance2 > maxDistance) {
				maxDistance = distance2;
				maxIntersection = intersection;
				maxVertexIndex = i;
				hasDistance = true;
			}
		}
	}

	*outDistance    = maxDistance;
	*outVertexIndex = maxVertexIndex;
	*out            = maxIntersection;

	return maxVertexIndex != -1;
}
Exemple #3
0
CPoint DiaTriangle::getCrossPoint(const CPoint& from, const CPoint& to) const
{
	CPoint intersection_point;

	bool found = lineIntersection(from, to, CPoint(m_xA , m_yA), CPoint(m_xB , m_yB), &intersection_point);

	if (found)
		return intersection_point;

	found = lineIntersection(from, to, CPoint(m_xB, m_yB), CPoint(m_xC, m_yC), &intersection_point);

	if (found)
		return intersection_point;

	found = lineIntersection(from, to, CPoint(m_xC , m_yC), CPoint(m_xA , m_yA), &intersection_point);

	if (found)
		return intersection_point;	

	return CPoint(m_xA , m_yA); // stub
}
Exemple #4
0
// Grows shape defined by list LS, does this by expanding two adj. sides outward a distance of SWELL.
// corners greater than ~100deg are rounded to keep sharp angles from over extending.
// shallower angles use the intersection of the expanded sides
QList<btVector3> cSpace::growShape(float swell, QList<btVector3> ls)
{
	QList<btVector3> list;
	btVector3 p;
	btVector3 p1,p2,p3,p4;
	btVector3 cxl,cxr;
	int i,j,nexti,prei;
	float alpha;
	
	for(i=0;i<ls.size();i++){												// traverse the shape in a CCW direction
		if(i == ls.size()-1) nexti = 0;										// make sure indices stay within bounds of the shape list
		else nexti = i+1;
		if(i == 0) prei = ls.size()-1;
		else prei = i-1;
		
		cxr = (ls[prei] - ls[i]).cross((ls[i]-btVector3(0,0,1)) - ls[i]);	// find the normal vector to the side pointing away from obj. center
		cxr.normalize();													// normalize the right vector
		
		cxl = (ls[nexti] - ls[i]).cross((ls[i]+btVector3(0,0,1)) - ls[i]);	// find the normal vector to the side pointing away from obj. center
		cxl.normalize();
		
		alpha = cxr.angle(cxl);												// get the angle between the two sides for exit condition
		
		if(alpha > HALFPI + 0.15){											// if the angle is larger than 90 then round the corner, reduces global vertex count
			j=1;
			list << ls[i] + (cxr * swell);									// add the first or right point
			while(alpha > CORNERRESOLUTION * j){
				p = ls[i] + (cxr.rotate(btVector3(0,0,1),CORNERRESOLUTION * j) * swell);	// add points every few rads until the left has been reached
				list << p;
				j++;
			}
			list << ls[i] + (cxl * swell);									// add the last or left point
		}
		else{
			p1 = ls[i] + (cxl * swell);											// p1 and p2 represent the left swelled side
			p2 = ls[nexti] + (cxl * swell);
			p3 = ls[i] + (cxr * swell);											// p3 and p4 represent the right swelled side
			p4 = ls[prei] + (cxr * swell);
			
			btVector3 corner;
			if(lineIntersection(p1,p2,p3,p4,&corner))							// find the intersection
				list << corner;													// insert the intersection point
			else
				list << ls[i];													// if lines are parallel just use the current point
		}
	}
	
	return list;
}
bool segmentIntersection(Line l1,Line l2)
{
    Point* p=lineIntersection(l1,l2);
    if(p!=NULL)
    {
        if((p->x<l1.p1.x && p->x<l1.p2.x)
           ||(p->x>l1.p1.x && p->x>l1.p2.x))
           {
               delete p;
               return false;
           }

        if((p->x<l2.p1.x && p->x<l2.p2.x)
           ||(p->x>l2.p1.x && p->x>l2.p2.x))
           {
               delete p;
               return false;
           }

        if((p->y<l1.p1.y && p->y<l1.p2.y)
           ||(p->y>l1.p1.y && p->y>l1.p2.y))
           {
               delete p;
               return false;
           }

        if((p->y<l2.p1.y && p->y<l2.p2.y)
           ||(p->y>l2.p1.y && p->y>l2.p2.y))
           {
               delete p;
               return false;
           }
        delete p;
        return true;
    }
    delete p;
    return false;
}
Exemple #6
0
//------------------------------------------------------
bool saveCrossPoints(const sData* data)
{
    const char* crossName="cross.txt";
    int id=0;

    std::ofstream crossFile(crossName);
    if(!crossFile) {
        return false;
    }
    crossFile.clear();

    int nStrLines=10;
    int nStrCount=10;
    LINE *strLines=0;
    genISOLines(data, data->s1, strLines, nStrLines, nStrCount);

    int nPotLines=10;
    int nPotCount=10;
    LINE *potLines=0;
    genISOLines(data, data->s2, potLines, nPotLines, nPotCount);

    double x;
    double y;
    for(int i=0; i<nStrLines; i++) {
        for(int j=0; j<nPotLines; j++) {
            if(lineIntersection(strLines[i], potLines[j], x,y)) {
                crossFile << i << j << x << y << std::endl;
            }
        }
    }

    crossFile.close();

    //free(strLines);
    //free(potLines);
    return true;
}
Exemple #7
0
std::vector<const Coords*> LcpFinder::segmentPolygonIntersection(int polygon, const Coords* a, const Coords* b, int* nextPolygon) {
    std::vector<const Coords*> v;
    std::vector<std::vector < const Coords*>> p = this->polygons[polygon];
    for (int ring = 0; ring < p.size(); ring++) {
        for (int i = 0; i < p[ring].size(); i++) {
            const Coords* prev = p[ring][i];
            const Coords* next = p[ring][(i + 1) % p[ring].size()];
            if (segmentCrossing(a, b, prev, next) == 1) {
                std::array<double, 2> xy = lineIntersection(a, b, prev, next);
                std::pair < std::tr1::unordered_set<Coords>::iterator, bool> success = this->coordmap.insert(Coords{xy[0], xy[1], polygon});
                if (success.second) {

                    *nextPolygon = neighbouringPolygon(prev, next, polygon);
                    success.first->addToPolygon(*nextPolygon);
                }
                this->linePoints[polygon].push_front(&*success.first);
                this->linePoints[*nextPolygon].push_front(&*success.first);
                v.push_back(&*success.first);
            }
        }
    }
    return v;

}
void lineInterSectionMarked(const vector<DjiPoint>& edge, const DjiLine line, vector<DjiPoint>& interSec, int markType)
{
	if (edge.size() < 3)
	{
		printLog("Pts.size()<3, in lineInterSectionMarked", true);
	}
	if (!(markType == INTERSEC_MARKED_WITH_POINT_INDEX || markType == INTERSEC_MARKED_WITH_POINT_S))
		printLog("input Error, in lineInterSectionMarked", true);

	DjiPoint pt0 = line.pt;
	DjiVec vec = line.vec;
	//直线方程    ny*(x - x0)-nx*(y - y0)   = 0;
	double x0 = pt0.x;
	double y0 = pt0.y;
	double nx = vec.x;
	double ny = vec.y;

	double x1 = 0;
	double y1 = 0;
	double x2 = 0;
	double y2 = 0;

	double result = 0;
	double res_next = 0;
	double res_back = 0;
	for (int i = 0; i < (int)edge.size(); i++)
	{
		result = ny*(edge[i].x - x0) - nx*(edge[i].y - y0);
		res_next = ny*(edge[(i + 1) % edge.size()].x - x0) - nx*(edge[(i + 1) % edge.size()].y - y0);
		res_back = ny*(edge[(i - 1 + edge.size()) % edge.size()].x - x0)
			- nx*(edge[(i - 1 + edge.size()) % edge.size()].y - y0);

		if (abs(result)<ZERO_ABS)
		{
			if (abs(res_back)>ZERO_ABS && abs(res_next)>ZERO_ABS && res_back*res_next < 0)
			{
				if (markType == INTERSEC_MARKED_WITH_POINT_S)
				{
					interSec.push_back(edge[i]);
				}
				if (markType == INTERSEC_MARKED_WITH_POINT_INDEX)
				{
					interSec.push_back(DjiPoint(edge[i].x, edge[i].y, i));
				}
			}
		}
		else  //第i点不在直线上
		{
			if (abs(res_next) < ZERO_ABS)
				continue;
			//第i点和第i+1点都不在直线上
			if (result*res_next < 0)
			{
				DjiPoint intersec;
				if (!lineIntersection(line, DjiLine(edge[i], edge[(i + 1) % edge.size()] - edge[i]), intersec))
					printLog("no intersection, in lineInterSectionMarked", true);
				if (markType == INTERSEC_MARKED_WITH_POINT_S)
				{

					interSec.push_back(DjiPoint(intersec.x, intersec.y, edge[(i + 1) % edge.size()].s));
				}
				if (markType == INTERSEC_MARKED_WITH_POINT_INDEX)
				{
					interSec.push_back(DjiPoint(intersec.x, intersec.y, (i + 1) % edge.size()));
				}
			}
		}
	}
	//判断
	if (interSec.size() % 2 != 0)
	{
		// 		drawPoint(mapImg, interSec[0], COL_RED, 1);
		// 		drawPoint(mapImg, line.pt, COL_RED, 0);
		// 		drawPoint(mapImg, line.pt+line.vec, COL_RED, 1);
		printLog("interSec.size() % 2 != 0, in intersectLine", true);
	}
}
Exemple #9
0
int getQlList(int listSize, double *list, quadrilateral *qlList) {

	double center_thr = 25;
	int listDim = 7;
	long int NP = listSize;
	int i, j, k, l;
	int I[4];

	double vertices[4][2];

	for (i = 0; i < NP; i += QL_NB_VERTICES) {
		I[0] = i;
		I[1] = i + 1;
		I[2] = i + 2;
		I[3] = i + 3;

		lineIntersection(list[0 + I[0] * listDim], list[1 + I[0] * listDim],
				list[2 + I[0] * listDim], list[3 + I[0] * listDim],
				list[0 + I[1] * listDim], list[1 + I[1] * listDim],
				list[2 + I[1] * listDim], list[3 + I[1] * listDim],
				&vertices[0][0], &vertices[0][1]);
		lineIntersection(list[0 + I[0] * listDim], list[1 + I[0] * listDim],
				list[2 + I[0] * listDim], list[3 + I[0] * listDim],
				list[0 + I[2] * listDim], list[1 + I[2] * listDim],
				list[2 + I[2] * listDim], list[3 + I[2] * listDim],
				&vertices[1][0], &vertices[1][1]);
		lineIntersection(list[0 + I[2] * listDim], list[1 + I[2] * listDim],
				list[2 + I[2] * listDim], list[3 + I[2] * listDim],
				list[0 + I[3] * listDim], list[1 + I[3] * listDim],
				list[2 + I[3] * listDim], list[3 + I[3] * listDim],
				&vertices[2][0], &vertices[2][1]);
		lineIntersection(list[0 + I[1] * listDim], list[1 + I[1] * listDim],
				list[2 + I[1] * listDim], list[3 + I[1] * listDim],
				list[0 + I[3] * listDim], list[1 + I[3] * listDim],
				list[2 + I[3] * listDim], list[3 + I[3] * listDim],
				&vertices[3][0], &vertices[3][1]);

		/* Segment configuration for coherent vertex extraction
		 *
		 * -Sides (x)
		 * -Vertices x
		 * -Two posible results:
		 *      	    (1)      			    (2)
		 * 			0----------3 			1----------2
		 * 			|		   | 			|		   |
		 * 		 (0)|          |(3) 	 (0)|          |(3)
		 * 			|          | 			|          |
		 * 			1----------2 			0----------3
		 * 			    (2)		 			    (1)
		 * */

		qlList[i / QL_NB_VERTICES] = quadrilateralNew(vertices);

		//patch to get coherent ql's
		for (int j=0;j<4;j++){
			if (vertices[j][0]<0 || vertices[j][0]>900 || vertices[j][1]<0 || vertices[j][1]>900 ){
				qlList[i / QL_NB_VERTICES].id = -1;
				break;
			}

		}
	}

	return 0;
}
Exemple #10
0
////////////////////////////////////////////////////////////////////////////////
// findPointOnEdge
BcBool MaBSPTree::lineIntersection( const MaVec3d& A, const MaVec3d& B, BcBSPPointInfo* pPointInfo, MaBSPNode* pNode /*= NULL*/ )
{
	// Check if we want the root node.
	if( pNode == NULL )
	{
		pNode = pRootNode_;
		if( pPointInfo != NULL )
		{
			pPointInfo->Point_ = B;
			pPointInfo->Distance_ = 1e12f;
		}
	}

	// Really naive way...
	BcBool Intersected = BcFalse;

	// TODO: Handle coincident intersection!
	MaPlane::eClassify ClassifyA = pNode->Plane_.classify( A, 0.0f );
	MaPlane::eClassify ClassifyB = pNode->Plane_.classify( B, 0.0f );

	if( ClassifyA == MaPlane::bcPC_FRONT && ClassifyB == MaPlane::bcPC_BACK )
	{
		BcF32 T;
		MaVec3d Intersection;
		if( pNode->Plane_.lineIntersection( A, B, T, Intersection ) )
		{
			// Is point in vertices? If so go down front to find nearer intersection.
			if( pointOnNode( Intersection, pNode ) )
			{
				// If we want point info recurse deeper to get it.
				if( pPointInfo != NULL )
				{
					BcF32 DistanceSquared = ( A - Intersection ).magnitudeSquared();

					// Check the distance, if it's less then go deeper!
					if( DistanceSquared < ( pPointInfo->Distance_ * pPointInfo->Distance_ ) )
					{
						pPointInfo->Plane_ = pNode->Plane_;
						pPointInfo->Distance_ = BcSqrt( DistanceSquared );
						pPointInfo->Point_ = Intersection;
					}
				}

				Intersected |= BcTrue;
			}
		}
	}
	
	// NOTE: ClassifyB shouldn't be required here ...right?
	if( pNode->pFront_ != NULL )
	{
		if( ClassifyA == MaPlane::bcPC_FRONT || ClassifyB == MaPlane::bcPC_FRONT )
		{
			Intersected |= lineIntersection( A, B, pPointInfo, pNode->pFront_ );
		}
	}

	if( pNode->pBack_ != NULL )
	{
		if( ClassifyA == MaPlane::bcPC_BACK || ClassifyB == MaPlane::bcPC_BACK )
		{
			Intersected |= lineIntersection( A, B, pPointInfo, pNode->pBack_ );
		}
	}

	return Intersected;
}
Exemple #11
0
void subdivision(const TPointD &p00, const TPointD &p10, const TPointD &p11,
                 const TPointD &p01, const TPointD &tex00, const TPointD &tex10,
                 const TPointD &tex11, const TPointD &tex01,
                 const TRectD &clippingRect, int details) {
  if (details == 1) {
    glBegin(GL_QUADS);

    glTexCoord2d(tex00.x, tex00.y);
    tglVertex(p00);

    glTexCoord2d(tex10.x, tex10.y);
    tglVertex(p10);

    glTexCoord2d(tex11.x, tex11.y);
    tglVertex(p11);

    glTexCoord2d(tex01.x, tex01.y);
    tglVertex(p01);

    glEnd();
  } else {
    TPointD A = p00;
    TPointD B = p10;
    TPointD C = p11;
    TPointD D = p01;

    /*
*     D                L2               C
*     +----------------+----------------+
*     |                |                |
*     |                |                |
*     |                |                |
*     |                |                |
*     |                |                |
*  H1 +----------------+----------------+ H2
*     |                | M              |
*     |                |                |
*     |                |                |
*     |                |                |
*     |                |                |
*     +----------------+----------------+
*     A                L1               B
*
*/

    TPointD M, L1, L2, H1, H2, P1, P2;
    bool intersection;

    // M
    intersection = lineIntersection(A, C, B, D, M);
    assert(intersection);

    // P1 (punto di fuga)
    intersection = lineIntersection(D, C, A, B, P1);
    if (!intersection) {
      P1.x = 0.5 * (A.x + D.x);
      P1.y = 0.5 * (A.y + D.y);
    }
    // H1
    intersection = lineIntersection(A, D, P1, M, H1);
    assert(intersection);

    // H2
    intersection = lineIntersection(B, C, P1, M, H2);
    assert(intersection);

    // P2 (punto di fuga)
    intersection = lineIntersection(A, D, B, C, P2);
    if (!intersection) {
      P2.x = 0.5 * (A.x + B.x);
      P2.y = 0.5 * (A.y + B.y);
    }
    // L1
    intersection = lineIntersection(A, B, P2, M, L1);
    assert(intersection);

    // L2
    intersection = lineIntersection(D, C, P2, M, L2);
    assert(intersection);

    TPointD texA = (tex00 + tex10) * 0.5;
    TPointD texB = (tex10 + tex11) * 0.5;
    TPointD texC = (tex11 + tex01) * 0.5;
    TPointD texD = (tex01 + tex00) * 0.5;
    TPointD texM = (texA + texC) * 0.5;

    details--;

    TRectD r1 = TRectD(
        std::min({A.x, L1.x, M.x, H1.x}), std::min({A.y, L1.y, M.y, H1.y}),
        std::max({A.x, L1.x, M.x, H1.x}), std::max({A.y, L1.y, M.y, H1.y}));

    TRectD r2 = TRectD(
        std::min({L1.x, B.x, H2.x, M.x}), std::min({L1.y, B.y, H2.y, M.y}),
        std::max({L1.x, B.x, H2.x, M.x}), std::max({L1.y, B.y, H2.y, M.y}));

    TRectD r3 = TRectD(
        std::min({M.x, H2.x, C.x, L2.x}), std::min({M.y, H2.y, C.y, L2.y}),
        std::max({M.x, H2.x, C.x, L2.x}), std::max({M.y, H2.y, C.y, L2.y}));

    TRectD r4 = TRectD(
        std::min({H1.x, M.x, L2.x, D.x}), std::min({H1.y, M.y, L2.y, D.y}),
        std::max({H1.x, M.x, L2.x, D.x}), std::max({H1.y, M.y, L2.y, D.y}));

    if (r1.overlaps(clippingRect))
      subdivision(A, L1, M, H1, tex00, texA, texM, texD, clippingRect, details);

    if (r2.overlaps(clippingRect))
      subdivision(L1, B, H2, M, texA, tex10, texB, texM, clippingRect, details);

    if (r3.overlaps(clippingRect))
      subdivision(M, H2, C, L2, texM, texB, tex11, texC, clippingRect, details);

    if (r4.overlaps(clippingRect))
      subdivision(H1, M, L2, D, texD, texM, texC, tex01, clippingRect, details);
  }
}
    static void addEdgeAndJoint (Path& destPath,
                                 const PathStrokeType::JointStyle style,
                                 const float maxMiterExtensionSquared, const float width,
                                 const float x1, const float y1,
                                 const float x2, const float y2,
                                 const float x3, const float y3,
                                 const float x4, const float y4,
                                 const float midX, const float midY)
    {
        if (style == PathStrokeType::beveled
            || (x3 == x4 && y3 == y4)
            || (x1 == x2 && y1 == y2))
        {
            destPath.lineTo (x2, y2);
            destPath.lineTo (x3, y3);
        }
        else
        {
            float jx, jy, distanceBeyondLine1EndSquared;

            // if they intersect, use this point..
            if (lineIntersection (x1, y1, x2, y2,
                                  x3, y3, x4, y4,
                                  jx, jy, distanceBeyondLine1EndSquared))
            {
                destPath.lineTo (jx, jy);
            }
            else
            {
                if (style == PathStrokeType::mitered)
                {
                    if (distanceBeyondLine1EndSquared < maxMiterExtensionSquared
                        && distanceBeyondLine1EndSquared > 0.0f)
                    {
                        destPath.lineTo (jx, jy);
                    }
                    else
                    {
                        // the end sticks out too far, so just use a blunt joint
                        destPath.lineTo (x2, y2);
                        destPath.lineTo (x3, y3);
                    }
                }
                else
                {
                    // curved joints
                    float angle1 = std::atan2 (x2 - midX, y2 - midY);
                    float angle2 = std::atan2 (x3 - midX, y3 - midY);
                    const float angleIncrement = 0.1f;

                    destPath.lineTo (x2, y2);

                    if (std::abs (angle1 - angle2) > angleIncrement)
                    {
                        if (angle2 > angle1 + float_Pi
                             || (angle2 < angle1 && angle2 >= angle1 - float_Pi))
                        {
                            if (angle2 > angle1)
                                angle2 -= float_Pi * 2.0f;

                            jassert (angle1 <= angle2 + float_Pi);

                            angle1 -= angleIncrement;
                            while (angle1 > angle2)
                            {
                                destPath.lineTo (midX + width * std::sin (angle1),
                                                 midY + width * std::cos (angle1));

                                angle1 -= angleIncrement;
                            }
                        }
                        else
                        {
                            if (angle1 > angle2)
                                angle1 -= float_Pi * 2.0f;

                            jassert (angle1 >= angle2 - float_Pi);

                            angle1 += angleIncrement;
                            while (angle1 < angle2)
                            {
                                destPath.lineTo (midX + width * std::sin (angle1),
                                                 midY + width * std::cos (angle1));

                                angle1 += angleIncrement;
                            }
                        }
                    }

                    destPath.lineTo (x3, y3);
                }
            }
        }
    }
void A4PreciseDetector::detect(A4MemoryBank *memoryBank, const std::list<A4PreDetectedRecord> &A4PreDetected)
{
	for(A4PreDetectedRecord a4pd : A4PreDetected)
	{
		int x = a4pd.ulptBorder.x;
		int y = a4pd.ulptBorder.y;

		int widthBorderBlock = a4pd.widthBorderBlock*memoryBank->resizeFactor;
		int heightBorderBlock = a4pd.heightBorderBlock*memoryBank->resizeFactor;
		int safetyWidthMargin = a4pd.safetyWidthMargin*memoryBank->resizeFactor;
		int safetyHeightMargin = a4pd.safetyHeightMargin*memoryBank->resizeFactor;
		
		unsigned char *dataUBorders = (unsigned char *)memoryBank->uBorders->imageData;
		unsigned char *dataDBorders = (unsigned char *)memoryBank->dBorders->imageData;
		unsigned char *dataLBorders = (unsigned char *)memoryBank->lBorders->imageData;
		unsigned char *dataRBorders = (unsigned char *)memoryBank->rBorders->imageData;
		int step = memoryBank->uBorders->widthStep;
		
		CvPoint lineHorUL, lineHorUR, lineHorDL, lineHorDR;
		CvPoint lineVerUL, lineVerUR, lineVerDL, lineVerDR;
		localHoughTransformer.fullReset(-20, 20, 2*widthBorderBlock, 2*safetyHeightMargin, step, NULL);
		
		try {
			lineHorUL = findPreciseBorderAlignedLinesFindLineSubroutine(dataUBorders, step, x, y);
		} catch(...) {
			lineHorUL = cvPoint(0, -a4pd.ulpt.y);
		}

		try {
			lineHorUR = findPreciseBorderAlignedLinesFindLineSubroutine(dataUBorders, step, x + 2*widthBorderBlock, y);
		} catch(...) { 
			lineHorUR = cvPoint(0, -a4pd.ulpt.y); 
		}

		try {
			lineHorDL = findPreciseBorderAlignedLinesFindLineSubroutine(dataDBorders, step, x, y + 4*heightBorderBlock - 2*safetyHeightMargin);
		} catch(...) {
			lineHorDL = cvPoint(0, -y - 4*heightBorderBlock + safetyHeightMargin); 
		}

		try {
			lineHorDR = findPreciseBorderAlignedLinesFindLineSubroutine(dataDBorders, step, x + 2*widthBorderBlock, y + 4*heightBorderBlock - 2*safetyHeightMargin);
		} catch(...) {
			lineHorDR = cvPoint(0, -y - 4*heightBorderBlock + safetyHeightMargin); 
		}

		localHoughTransformer.fullReset(70, 110, 2*safetyWidthMargin, 2*heightBorderBlock, step, NULL);
		
		try {
			lineVerUL = findPreciseBorderAlignedLinesFindLineSubroutine(dataLBorders, step, x, y);
		} catch(...) {
			lineVerUL = cvPoint(90, -a4pd.ulpt.x);
		}
		try {
			lineVerDL = findPreciseBorderAlignedLinesFindLineSubroutine(dataLBorders, step, x, y + 2*heightBorderBlock);
		} catch(...) {
			lineVerDL = cvPoint(90, -a4pd.ulpt.x);
		}
		try {
			lineVerUR = findPreciseBorderAlignedLinesFindLineSubroutine(dataRBorders, step, x + 4*widthBorderBlock - 2*safetyWidthMargin, y);
		} catch(...) {
			lineVerUR = cvPoint(90, -x - 4*widthBorderBlock + safetyWidthMargin);
		}
		try {
			lineVerDR = findPreciseBorderAlignedLinesFindLineSubroutine(dataRBorders, step, x + 4*widthBorderBlock - 2*safetyWidthMargin, y + 2*heightBorderBlock);
		} catch(...) {
			lineVerDR = cvPoint(90, -x - 4*widthBorderBlock + safetyWidthMargin);
		}
		
		CvPoint cornerUL = lineIntersection(lineHorUL.x*M_PI/180, -lineHorUL.y, lineVerUL.x*M_PI/180, -lineVerUL.y);
		CvPoint cornerUR = lineIntersection(lineHorUR.x*M_PI/180, -lineHorUR.y, lineVerUR.x*M_PI/180, -lineVerUR.y);
		CvPoint cornerDL = lineIntersection(lineHorDL.x*M_PI/180, -lineHorDL.y, lineVerDL.x*M_PI/180, -lineVerDL.y);
		CvPoint cornerDR = lineIntersection(lineHorDR.x*M_PI/180, -lineHorDR.y, lineVerDR.x*M_PI/180, -lineVerDR.y);

		A4PreciseDetected.push_back( A4PreciseDetectedRecord(cornerUL, cornerUR, cornerDL, cornerDR
			#ifdef DEBUG_LINES_INFORMATION 
			,
									lineHorUL, lineHorUR, lineHorDL, lineHorDR, 
									lineVerUL, lineVerDL, lineVerUR, lineVerDR
			#endif DEBUG_LINES_INFORMATION 
									) );
	}
}
bool hitboxCollision(int a_x,int a_y,int a_width,int a_height,float a_angle,
              int b_x,int b_y,int b_width,int b_height,float b_angle)
{
//parche feminist kill bot
	if(a_width>b_width
		&& a_height > b_height
		&& a_x<b_x
		&& a_x+a_width>b_x+b_width
		&& a_y<b_y
		&& a_y+a_height>b_y+b_height)
		return true;
	if(b_width>a_width
		&& b_height > a_height
		&& b_x<a_x
		&& b_x+b_width>a_x+a_width
		&& b_y<a_y
		&& b_y+b_height>a_y+a_height)
		return true;


    Point pa1(a_x,
              a_y);

    Point pa2(a_x + cos (a_angle*PI/180) * a_width
             ,a_y - sin (a_angle*PI/180) * a_width);

    Point pa3(a_x + cos (a_angle*PI/180) * a_width + sin (a_angle*PI/180) * a_height,
              a_y - sin (a_angle*PI/180) * a_width + cos (a_angle*PI/180) * a_height);

    Point pa4(a_x + sin (a_angle*PI/180) * a_height,
              a_y + cos (a_angle*PI/180) * a_height);


    Point pb1(b_x,
              b_y);

    Point pb2(b_x + cos (b_angle*PI/180) * b_width
             ,b_y - sin (b_angle*PI/180) * b_width);

    Point pb3(b_x + cos (b_angle*PI/180) * b_width + sin (b_angle*PI/180) * b_height,
              b_y - sin (b_angle*PI/180) * b_width + cos (b_angle*PI/180) * b_height);

    Point pb4(b_x + sin (b_angle*PI/180) * b_height,
              b_y + cos (b_angle*PI/180) * b_height);

    Line la1(pa1,pa2);
    Line la2(pa2,pa3);
    Line la3(pa3,pa4);
    Line la4(pa4,pa1);

    Line lb1(pb1,pb2);
    Line lb2(pb2,pb3);
    Line lb3(pb3,pb4);
    Line lb4(pb4,pb1);

    if(segmentIntersection(la1,lb1))
        return true;
    if(segmentIntersection(la1,lb2))
        return true;
    if(segmentIntersection(la1,lb3))
        return true;
    if(segmentIntersection(la1,lb4))
        return true;

    if(segmentIntersection(la2,lb1))
        return true;
    if(segmentIntersection(la2,lb2))
        return true;
    if(segmentIntersection(la2,lb3))
        return true;
    if(segmentIntersection(la2,lb4))
        return true;

    if(segmentIntersection(la3,lb1))
        return true;
    if(segmentIntersection(la3,lb2))
        return true;
    if(segmentIntersection(la3,lb3))
        return true;
    if(segmentIntersection(la3,lb4))
        return true;

    if(segmentIntersection(la4,lb1))
        return true;
    if(segmentIntersection(la4,lb2))
        return true;
    if(segmentIntersection(la4,lb3))
        return true;
    if(segmentIntersection(la4,lb4))
        return true;






    vector<Point*>intersections;
    intersections.push_back(lineIntersection(la1,lb1));
    intersections.push_back(lineIntersection(la1,lb2));
    intersections.push_back(lineIntersection(la1,lb3));
    intersections.push_back(lineIntersection(la1,lb4));

    intersections.push_back(lineIntersection(la2,lb1));
    intersections.push_back(lineIntersection(la2,lb2));
    intersections.push_back(lineIntersection(la2,lb3));
    intersections.push_back(lineIntersection(la2,lb4));

    intersections.push_back(lineIntersection(la3,lb1));
    intersections.push_back(lineIntersection(la3,lb2));
    intersections.push_back(lineIntersection(la3,lb3));
    intersections.push_back(lineIntersection(la3,lb4));

    intersections.push_back(lineIntersection(la4,lb1));
    intersections.push_back(lineIntersection(la4,lb2));
    intersections.push_back(lineIntersection(la4,lb3));
    intersections.push_back(lineIntersection(la4,lb4));


    int x_min=0;int x_max=0;
    int y_max=0;int y_min=0;

    if(a_width*a_height>b_width*b_height)
    {
        x_min = pa1.x;
        x_min=min(x_min,pa2.x);
        x_min=min(x_min,pa3.x);
        x_min=min(x_min,pa4.x);
        x_max = pa1.x;
        x_max=max(x_max,pa2.x);
        x_max=max(x_max,pa3.x);
        x_max=max(x_max,pa4.x);

        y_min = pa1.y;
        y_min=min(y_min,pa2.y);
        y_min=min(y_min,pa3.y);
        y_min=min(y_min,pa4.y);
        y_max = pa1.y;
        y_max=max(y_max,pa2.y);
        y_max=max(y_max,pa3.y);
        y_max=max(y_max,pa4.y);
    }else
    {
        x_min = pb1.x;
        x_min=min(x_min,pb2.x);
        x_min=min(x_min,pb3.x);
        x_min=min(x_min,pb4.x);
        x_max = pb1.x;
        x_max=max(x_max,pb2.x);
        x_max=max(x_max,pb3.x);
        x_max=max(x_max,pb4.x);

        y_min = pb1.y;
        y_min=min(y_min,pb2.y);
        y_min=min(y_min,pb3.y);
        y_min=min(y_min,pb4.y);
        y_max = pb1.y;
        y_max=max(y_max,pb2.y);
        y_max=max(y_max,pb3.y);
        y_max=max(y_max,pb4.y);
    }

    int cont=0;

    for(int i=0;i<(int)intersections.size();i++)
    {
        Point* point=intersections[i];
        if(point!=NULL)
        {
            if(point->x > x_min
               && point->x < x_max
               && point->y > y_min
               && point->y < y_max)
            {
                cont++;
            }
        }
    }

    vector<Point*>::iterator i;
    for ( i = intersections.begin() ; i < intersections.end(); i++ )
    {
        delete * i;
    }


    if(cont>=8)
        return true;

    return false;
}
Exemple #15
0
bool Math::lineOverlapsRect(pair<sf::Vector2f, sf::Vector2f> line, sf::FloatRect rect, sf::Vector2f &intersectionPoint)
{
	pair<sf::Vector2f, sf::Vector2f> edge;
	pair<float, float> t;

	//Redundance
	//Test with top edge
	edge.first.x=rect.left;
	edge.first.y=rect.top;
	edge.second.x=rect.left+rect.width;
	edge.second.y=rect.top;
	
	t=lineIntersection(line, edge, true);

	if(tCheck(t))
	{
		return true;
	}
	//cout << "Top edge " << t.first << " " << t.second << endl;
	
	//Test with bottom edge
	edge.first.x=rect.left;
	edge.first.y=rect.top+rect.height;
	edge.second.x=rect.left+rect.width;
	edge.second.y=rect.top+rect.height;
	
	t=lineIntersection(line, edge, true);
	
	if(tCheck(t))
	{
		return true;
	}
	//cout << "Bottom edge " << t.first << " " << t.second << endl;
	
	//Test with left edge
	edge.first.x=rect.left;
	edge.first.y=rect.top;
	edge.second.x=rect.left;
	edge.second.y=rect.top+rect.height;
	
	t=lineIntersection(line, edge, false);
	
	if(tCheck(t))
	{
		return true;
	}
	//cout << "Left edge " << t.first << " " << t.second << endl;
	
	//Test with right edge
	edge.first.x=rect.left+rect.width;
	edge.first.y=rect.top;
	edge.second.x=rect.left+rect.width;
	edge.second.y=rect.top+rect.height;
	
	t=lineIntersection(line, edge, false);
	
	if(tCheck(t))
	{
		return true;
	}
	
	return false;
	//cout << "Right edge " << t.first << " " << t.second << endl;
}