Friday, 15 August 2014

c# - Given 4 points of a rectangle, disregarding the 4 points, do any sides intersect? -



c# - Given 4 points of a rectangle, disregarding the 4 points, do any sides intersect? -

i'm messing around object recognition samples emgu:

rectangle rect = modelimage.roi; pointf p1 = new pointf(rect.left, rect.bottom); pointf p2 = new pointf(rect.right, rect.bottom); pointf p3 = new pointf(rect.right, rect.top); pointf p4 = new pointf(rect.left, rect.top); //check if opposite lines intersect //if so, don't add together final results //we should never have 2 opposite sides intersecting linesegment2df l1 = new linesegment2df(p1,p2); linesegment2df l2 = new linesegment2df(p2, p3); linesegment2df l3 = new linesegment2df(p3, p4); linesegment2df l4 = new linesegment2df(p4, p1) if (!(intersects(l1, l3) || intersects(l2, l4))) { //draw line }

however, fishy results such (grey ish):

and (red):

i other bad results too, notice trend these. these rectangles (or technically trapezoids...?) have lines cross on or lay on top of each other. want ignore drawing these results if that's case. there way determine given 4 points?

update: @ request of user @chris , check out this answer. attempted replicate pseudo code. however, may misunderstanding it. doesn't give expected results. seems homecoming true. may because translated pseudo code wrong.

public static bool intersects(linesegment2df l1, linesegment2df l2) { float x1 = l1.p1.x; float x2 = l1.p2.x; float x3 = l2.p1.x; float x4 = l2.p2.x; float y1 = l1.p1.y; float y2 = l1.p2.y; float y3 = l2.p1.y; float y4 = l2.p2.y; float intervalamin = math.min(x1, x2); float intervalamax = math.max(x1, x2); float intervalbmin = math.min(x3, x4); float intervalbmax = math.max(x3, x4); //if (math.max(l1.p1.x, l1.p2.x) < math.min(l2.p1.x, l2.p2.x)) homecoming false; if(intervalamax < intervalbmin) homecoming false; float a1 = (y1-y2)/(x1-x2); // pay attending not dividing 0 float a2 = (y3-y4)/(x3-x4); // pay attending not dividing 0 if (a1 == a2) homecoming false; // parallel segments float b1 = y1-a1*x1;// = y2-a1*x2; float b2 = y3-a2*x3;// = y4-a2*x4; float xa = (b2 - b1) / (a1 - a2);// 1 time again, pay attending not dividing 0 float ya = a1 * xa + b1; //float ya = a2 * xa + b2; if ((xa < math.max(math.min(x1, x2), math.min(x3, x4))) || (xa > math.min(math.max(x1, x2), math.max(x3, x4)))) homecoming false; // intersection out of bound homecoming true; }

i found cool simplified method online. simplified bit so:

public static bool intersects2df(linesegment2df thislinesegment, linesegment2df otherlinesegment) { float firstlineslopex, firstlineslopey, secondlineslopex, secondlineslopey; firstlineslopex = thislinesegment.p2.x - thislinesegment.p1.x; firstlineslopey = thislinesegment.p2.y - thislinesegment.p1.y; secondlineslopex = otherlinesegment.p2.x - otherlinesegment.p1.x; secondlineslopey = otherlinesegment.p2.y - otherlinesegment.p1.y; float s, t; s = (-firstlineslopey * (thislinesegment.p1.x - otherlinesegment.p1.x) + firstlineslopex * (thislinesegment.p1.y - otherlinesegment.p1.y)) / (-secondlineslopex * firstlineslopey + firstlineslopex * secondlineslopey); t = (secondlineslopex * (thislinesegment.p1.y - otherlinesegment.p1.y) - secondlineslopey * (thislinesegment.p1.x - otherlinesegment.p1.x)) / (-secondlineslopex * firstlineslopey + firstlineslopex * secondlineslopey); if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { // collision detected homecoming true; } homecoming false; // no collision }

after tweaking , debugging on of way round points in lines (basically code prior this), figured little in code need adjust. after fixing that, solution works!

c# geometry emgucv

No comments:

Post a Comment