转载

JAVA Polygon 在配送区域超区校验的实践

业务背景

物流同城配送,在用户下单的时候会进行配送超区校验,前端会将用户的定位传入后台服务,后台服务针对商家的自定义配送区域(通过地图控件拖拽出来的不规则图形)进行校验。

实现方案

利用java se工具类来做 java.awt.Polygon

api地址: https://docs.oracle.com/javas...

使用到的api主要有:

public Polygon(int[] xpoints,int[] ypoints,int npoints)

Parameters:

xpoints - an array of X coordinates 所有点的x坐标

ypoints - an array of Y coordinates 所有点的y坐标

npoints - the total number of points in the Polygon 总共的点数

scene:

把商家通过地图拖拽的多边形各点传入进来,实例化Polygon对象

public boolean contains(double x,double y)

Parameters:

x - the specified X coordinate to be tested x坐标

y - the specified Y coordinate to be tested y坐标

scene:

获取用户当前定位的经纬度,作为x,y坐标系传入进行判断

使用示例

判断规则矩形包含点

JAVA Polygon 在配送区域超区校验的实践

/**
     * 用例描述:测试场景 多边形为矩形 判断点是否在范围内
     *
     *
     * @since  2016-12-06 by 青芒
     */
    @Test
    public void test_rec_ContainsPoint(){
        try {
            int[] xpoints = {1,1,5,5};
            int[] ypoints = {1,5,5,1};
            Polygon polygon = new Polygon(xpoints, ypoints,4);

            boolean contains;

            Point p = new Point(2,2);
            contains = polygon.contains(p);
            Assert.assertTrue(contains);

            contains = polygon.contains(2, 2);
            Assert.assertTrue(contains);

            contains = polygon.contains(1, 4);
            Assert.assertTrue(contains);

            contains = polygon.contains(2, 2);
            Assert.assertTrue(contains);

            contains = polygon.contains(2.5, 2.5);
            Assert.assertTrue(contains);
        } catch (Exception e) {
            Assert.fail();
        }
    }

判断规则矩形包含矩形(拓展下,当前场景用不到)

JAVA Polygon 在配送区域超区校验的实践

/**
     * 用例描述:测试场景 多边形为矩形 判断矩形是否在范围内
     *
     *
     * @since  2016-12-06 by 青芒
     */
    @Test
    public void test_rec_ContainsRectangle(){
        try {
            int[] xpoints = {1,1,5,5};
            int[] ypoints = {1,5,5,1};
            Polygon polygon = new Polygon(xpoints, ypoints,4);

            Rectangle2D rectangle = new Rectangle(2,2,1,1);
            boolean contains = polygon.contains(rectangle);
            Assert.assertTrue(contains);
        } catch (Exception e) {
            Assert.fail();
        }
    }

判断不规则图形包含点

JAVA Polygon 在配送区域超区校验的实践

/**
     * 用例描述:测试场景 不规则多边形 包含点
     *
     *
     * @since  2016-12-06 by 青芒
     */
    @Test
    public void test_polygon_ContainsPoint(){
        try {
            int[] xpoints = {1,2,4,1,2};
            int[] ypoints = {3,4,3,3,2};
            Polygon polygon = new Polygon(xpoints, ypoints,5);
            boolean contains = polygon.contains(3, 3 );
            Assert.assertTrue(contains);

            contains = polygon.contains(2, 1);
            Assert.assertFalse(contains);
        } catch (Exception e) {
            Assert.fail();
        }
    }

判断交叉不规则多边形包含点

JAVA Polygon 在配送区域超区校验的实践

/**
     * 用例描述:测试场景 交叉多边形 包含点
     *
     *
     * @since  2016-12-06 by 青芒
     */
    @Test
    public void test_crossing_ContainsPoint(){
        try {
            int[] xpoints = {1,2,1,3,3};
            int[] ypoints = {1,2,3,3,1};
            Polygon polygon = new Polygon(xpoints, ypoints,5);
            boolean contains = polygon.contains(2, 2 );
            Assert.assertTrue(contains);

            contains = polygon.contains(2, 1 );
            Assert.assertTrue(contains);

            contains = polygon.contains(2, 1.5);
            Assert.assertTrue(contains);

            contains = polygon.contains(2.5, 2.5 );
            Assert.assertTrue(contains);

            //多边形外围
            contains = polygon.contains(0, 1);
            Assert.assertFalse(contains);

            contains = polygon.contains(1, 2);
            Assert.assertFalse(contains);

            contains = polygon.contains(3, 2);
            Assert.assertFalse(contains);
        } catch (Exception e) {
            Assert.fail();
        }
    }

结束

至此对于该超区校验场景的实践已经介绍完毕,使用起来也不难,主要是让大家知道通过javase 的awt类也是可以容易实现的。

原文  https://segmentfault.com/a/1190000007736473
正文到此结束
Loading...