如何找到矩形区域内的像素(How to find a pixel which is inside rectangular area or not)

79 IT屋

For eg i have a image of 1280*720 width and height. How to find a particular pixel contains inside x0,y0,x1,y1 co-ordinate

x0,y0
+--------+
|        |    
|        |    
|     .  |    
|        |    
|        |
+--------+x1,y1

Will below algirthm work
typedef struct {
    int left;
    int top;
    int right;
    int bot;
} rect;

int inrect (int x, int y, rect r) {
    if (x<r.left || x>r.right || y<r.top || y>r.bot)
        return 0;
    return 1;
}
rect rectarr[1] = { {10, 10, 50, 50}}

Here 0 means outside and 1 means inside
解决方案
Yes, it would work. However, for performance reasons, relatively big structs are usually passed via pointers (see, for instance, PtInRect Windows function signature)
int inrect (int x, int y, const rect *pr) {
    if (x<pr->left || x>pr->right || y<pr->top || y>pr->bot)
        return 0;
    return 1;
}

例如,我的图像宽度和高度为1280 * 720。如何查找特定像素包含x0,y0,x1,y1坐标内部



 x0,y0 
+ -------- +
| |
| |
| 。 |
| |
| |
+ -------- + x1,y1


将低于algirthm工作

 typedef  struct  {
int left;
int top;
int right;
int bot;
} rect;

int inrect( int x, int y,rect r){
if (x< r.left || x> r.right || y< r .top || y> r.bot)
return 0 ;
return 1 ;
}
rect rectarr [ 1 ] = {{ 10 10 50 50 }}


这里0表示外部,1表示内部
解决方案
是的,它会起作用。但是,出于性能原因,相对较大的 struct s通常通过指针传递(例如,参见 PtInRect Windows函数签名)

  int  inrect( int  x, int  y, const  rect * pr){
if (x< pr-> left || x> pr-> right || y< pr-> top || y> pr-> bot)
return 0 ;
return 1 ;
}

本文地址:IT屋 » 如何找到矩形区域内的像素