Efficiently find points inside a circle sector

前端 未结 3 1168
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 23:02

I have a set of 2d points distributed randomly. I need to perform a time intensive operation on a small subset of these points but I need to first figure out what points I n

3条回答
  •  Happy的楠姐
    2020-11-30 23:52

    @Oren Trutner answer was great so I decided to make a visual example of it and make some improvements to make it work on all angles.

    no further speaking, check the example below.

    $(document).on('keypress',function (e) {
            if(e.which === 13)
            {
                $("#calc").click();
            }
        });
    
        function areClockwise(v1, v2) {
            return -v1.x*v2.y + v1.y*v2.x > 0;
        }
    
        function vector(x = 0, y = 0) {
            return {x:x,y:y}
        }
    
        function degToRad(degree) {
            return degree * Math.PI / 180;
        }
    
        function isIn()
        {
            let illustration = $("#illustration");
            illustration.html("");
            let r = 250;
            let fieldOfViewAngle = 150;
            let x = Number($("#x").val());
            let y = Number($("#y").val());
            let startAngle = Number($("#startAngle").val());
            let startSectorAngle = degToRad(startAngle);
            let endSectorAngle = degToRad(startAngle+fieldOfViewAngle);
    
            $("#startLine").attr("x2",250 + r*Math.cos(-startSectorAngle)).attr("y2",250 + r*Math.sin(-startSectorAngle));
            $("#endLine").attr("x2",250 + r*Math.cos(-endSectorAngle)).attr("y2",250 + r*Math.sin(-endSectorAngle));
            $("#point").attr("cx",250 +x).attr("cy",250 -y);
    
            let sectorStartVector = vector(r * Math.cos(startSectorAngle),r * Math.sin(startSectorAngle));
            let sectorEndVector = vector(r * Math.cos(endSectorAngle),r * Math.sin(endSectorAngle));
            let relPoint = vector(x,y);
    
            if(!this.areClockwise(sectorStartVector, relPoint) &&
                this.areClockwise(sectorEndVector, relPoint))
                $("#result").html("Result: in");
            else{
                $("#result").html("Result: out")
            }
        }
    .flixy {
                display: flex;
                flex-direction: column;
            }
    
            .flixy > div {
                margin-bottom: 20px;
                width:300px
            }
    
            .flixy > div > input {
                float: right;
            }
    
    

提交回复
热议问题