• Advertisement
Sign in to follow this  

Point in (non-axis aligned) rectangle

This topic is 3701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Searching the web, I found the following code, which lost some characters due to I suppose conversion to HTML:
int CTestDlg::PointInrectangle(int x1, int y1, int x2, int y2, int x, int y)
int a, b, d;
int D1, D2;
int F1, F2;

d = abs(x1 - x2) abs(y1 - y2);

a = x1 - x2;
b = y1 - y2;

D1 = int(d * sqrt((a * a) (b * b)));
D2 = ((a * a) (b * b))/2 D1;

F1 = b * (x - x1) - a * (y - y1);
F2 = a * (x - (x1 x2)/2) b * (y - (y1 y2)/2);

if (abs(F1) <= D1 && abs(F2) <= D2)
return 1;

return 0;

My first guess is that it's a '+' because I don't see any, but then I'm not sure if it's the same one in each missing spot, and I'm not sure if it makes sense in the last line (with the x - (x1 ? x2)/2 ...) I also have no idea why it's using int instead of float... The alternative code I found uses barycentric coordinate computation, so I could just skip the last check (u + v < 1) to get a parallelogram instead of triangle test. Though that has early exists, I'm wondering whether maybe the multiple if statements would make that approach less efficient:
bool PointInTriangle (i, p0, p1, p2) 
    returns ({TRUE, FALSE},u,v);
 1: e0=i-p0 
 2: e1=p1-p0 
 3: e2=p2-p0 
 4: if (e1x=0) 
 5:   if (e2x=0) return (FALSE,0,0); 
 6:   u=e0x/e2x
 7:   if (u<0 or u>1) return FALSE,0,0); 
 8:   if (e1y=0) return FALSE,0,0) 
 9:   v=( e0y-e2yu)/e1y
10:   if (v<0) return (FALSE,0,0); 
11: else 
12:   d = e2ye1x-e2xe1y 
13:   if (d=0) return (FALSE,0,0); 
14:   u = (e0ye1x-e0xe1y)/d 
15:   if (u<0 or u>1) return (FALSE,0,0);
16:   v = (e0x-e2xu) / e1x
17:   if (v<0) return (FALSE,0,0);
18: if (u+v>1) return (FALSE,0,0); 
19: return (TRUE,u,v);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement