Point-in-rectangle testing

前端 未结 3 1002
悲哀的现实
悲哀的现实 2021-01-19 08:40

I have this matrix

/// as if the create a rectangle
int [][] loc = {
  {5, 15},//(x1, y1)
  {5, 30}, // (x1, y2)
  {20, 15},// (x2, y1)
  {20, 30}, // (x2,          


        
3条回答
  •  借酒劲吻你
    2021-01-19 09:14

    The code in the question is Java or C or some other language that defines array literals with {}, but since the tag is Javascript and this shows up on Google for Javascript, here is a reasonable way to do point-rectangle intersection in JS.

    function pointRectangleIntersection(p, r) {
        return p.x > r.x1 && p.x < r.x2 && p.y > r.y1 && p.y < r.y2;
    }
    
    var point = {x: 1, y: 2};
    var rectangle = {x1: 0, x2: 10, y1: 1, y2: 7};
    pointRectangleIntersection(point, rectangle);
    

    As mentioned in the comments, change > to >= and/or < to <= to do an intersection which includes the rectangle boundaries.

提交回复
热议问题