Click here to Skip to main content
15,884,849 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
C#
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
Posted
Comments
Mehdi Gholam 16-Mar-15 15:15pm    
Try and see.
Sergey Alexandrovich Kryukov 16-Mar-15 15:24pm    
What prevents your from running it? What prevents you from using the debugger is you are not sure what's going on?
—SA

1 solution

Yes, it would work. However, for performance reasons, relatively big structs are usually passed via pointers (see, for instance, PtInRect Windows function signature)
C
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;
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Mar-15 16:29pm    
Good point, a 5.
—SA
CPallini 16-Mar-15 16:43pm    
Way too good man, thank you anyway :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900