前几天说要传军标 的算法思想呢,结果有事耽误了,现在我 把我军标 思想给说一下。
首先利用的技术:前端as语言绘制军标 (其实就是个多边 形),把多边形的点组成 XML字符串《这里也可以传对象,但是在后台java段也要有相同的对应对象》通过BlazeDS的 RemoteObject方式传到后台。
后台可以通过java保存到数据库中,这个最好是保存为Clob字段。
下面我把关键代码给大家分享一下。。
# //处理鼠标单击事件 # public function map_mouseClickHandler(event:MouseEvent):void # { # map.mapNavigationEnabled=false; # //确定点击的地图点的坐标,然后放到数组中 # var m_point:MapPoint=map.toMapFromStage(event.stageX, event.stageY); # if (m_firstpoints == null) # { # m_firstpoints=new Array(); # } # m_firstpoints.push(m_point); # } # //处理鼠标双击事件 # public function map_mouseDoubleClickHandler(event:MouseEvent):void # { # m_gbjGeometry=new Polyline(); # //定义军标的多边形 # if (m_firstpoints.length <= 2) # { # m_firstpoints.splice(0); # Alert.show("请最少点击三个点才能完成对军标的画法", "Error INFO"); # return; # } # //根据贝塞尔曲线算法来画线和面 # drawBezierCurve(); # //形成graphic # full_polygon_graphic=new Graphic(full_gbjpolygon, gbjSymbol); # full_polygon_graphic.toolTip="gbj"; # full_gbjpolygon.spatialReference=map.spatialReference; # graphicsLayer.add(full_polygon_graphic); # map.removeEventListener(MouseEvent.CLICK, map_mouseClickHandler); # map.removeEventListener(MouseEvent.DOUBLE_CLICK, map_mouseDoubleClickHandler); # m_firstpoints.splice(0); # this.dispatchEvent(new DrawEvent("drawEnd", full_polygon_graphic)); # } # # /** # *这个是贝塞尔曲线算法函数用于画军标 # * # */ # private function drawBezierCurve():void # { # //主要算法思想是:由中轴线的两点确定一条直线,做直线的垂线,并取垂线上一定距离的点,作成上边曲线和下边曲线 # if ((m_gbjGeometry.paths != null) && (m_gbjGeometry.paths.length > 0)) # { # m_gbjGeometry.removePath(0); # } # //判断点击点的季偶性,得到军标的多边形 # var jishu:Boolean=true; # if (m_firstpoints.length % 2 == 0) # { # jishu=false; # } # clogger.logger.info("你点击的个数是不是奇数:"+jishu); # var curvepoints:Array=getzz_geometry(m_firstpoints, jishu); # # var dgbj_geometry:Array=getdbx_geometry(curvepoints); # full_gbjpolygon=new Polygon(dgbj_geometry); # } # /** # * 这个函数就是用来求中轴线的,中轴线上的所有点 # * @param clickarray # * @param jishu # * @return # * # */ # private function getzz_geometry(clickarray:Array, jishu:Boolean):Array # { # //首先定义中轴曲线的点坐标 # var curvepoints:Array=new Array(); # //数组长度 # var array_length:Number=clickarray.length; # if (jishu) # { # clogger.logger.info("你点击的次数是奇数个:并且他大于等于三,是:" + clickarray.length + "个"); # for (var i:int=0; i < clickarray.length - 2; i=i + 2) # { # for (var j:Number=0; j <= 1; j=j + 0.1) # { # if (i != 0 && j == 0) # continue; # var pos_x:Number=Math.pow((1 - j), 2) * clickarray.x + 2 * j * (1 - j) * clickarray[i + 1].x + Math.pow(j, 2) * clickarray[i + 2].x; # var pos_y:Number=Math.pow((1 - j), 2) * clickarray.y + 2 * j * (1 - j) * clickarray[i + 1].y + Math.pow(j, 2) * clickarray[i + 2].y; # var m_point:MapPoint=new MapPoint(pos_x, pos_y); # curvepoints.push(m_point); # } # } # } # //下面的是偶数时后,前面那些点还是用三个点来确定,后面四个点用三次贝塞尔曲线来搞定。 # if (!jishu) # { # //判断点击的点是不是大于6个,如果是六个的话,首先是先用二次贝塞尔曲线算出前三个来。 # if (clickarray.length >= 6) # { # clogger.logger.info("你点击的次数是偶数个:并且他大于等于6,是:" + clickarray.length + "个"); # for (var i:int=0; i < clickarray.length - 5; i=i + 2) # { # for (var j:Number=0; j <= 1; j=j + 0.1) # { # if (i != 0 && j == 0) # continue; # var pos_x:Number=Math.pow((1 - j), 2) * clickarray.x + 2 * j * (1 - j) * clickarray[i + 1].x + Math.pow(j, 2) * clickarray[i + 2].x; # var pos_y:Number=Math.pow((1 - j), 2) * clickarray.y + 2 * j * (1 - j) * clickarray[i + 1].y + Math.pow(j, 2) * clickarray[i + 2].y; # var m_point:MapPoint=new MapPoint(pos_x, pos_y); # //中轴线的点数 # curvepoints.push(m_point); # } # } # //这最后四个点用三次贝塞尔曲线来确定 # var p_1:MapPoint=clickarray[array_length - 4]; # var p_2:MapPoint=clickarray[array_length - 3]; # var p_3:MapPoint=clickarray[array_length - 2]; # var p_4:MapPoint=clickarray[array_length - 1]; # for (var k:Number=0; k < 1; k=k + 0.1) # { # if (k == 0) # continue; # var pos_x:Number=p_1.x * Math.pow((1 - k), 3) + 3 * p_2.x * k * Math.pow((1 - k), 2) + 3 * p_3.x * Math.pow(k, 2) * (1 - k) + p_4.x * Math.pow(k, 3); # var pos_y:Number=p_1.y * Math.pow((1 - k), 3) + 3 * p_2.y * k * Math.pow((1 - k), 2) + 3 * p_3.y * Math.pow(k, 2) * (1 - k) + p_4.y * Math.pow(k, 3); # var m_point:MapPoint=new MapPoint(pos_x, pos_y); # //中心轴的添加 # curvepoints.push(m_point); # } # } # //如果仅仅就四个点的话,下面是求只有四个点的公式 # else # { # clogger.logger.info("你点击的次数是4个,他是:" + clickarray.length + "个"); # var p_1:MapPoint=clickarray[0]; # var p_2:MapPoint=clickarray[1]; # var p_3:MapPoint=clickarray[2]; # var p_4:MapPoint=clickarray[3]; # for (var k:Number=0; k < 1; k=k + 0.1) # { # var pos_x:Number=p_1.x * Math.pow((1 - k), 3) + 3 * p_2.x * k * Math.pow((1 - k), 2) + 3 * p_3.x * Math.pow(k, 2) * (1 - k) + p_4.x * Math.pow(k, 3); # var pos_y:Number=p_1.y * Math.pow((1 - k), 3) + 3 * p_2.y * k * Math.pow((1 - k), 2) + 3 * p_3.y * Math.pow(k, 2) * (1 - k) + p_4.y * Math.pow(k, 3); # var m_point:MapPoint=new MapPoint(pos_x, pos_y); # //中心轴的添加 # curvepoints.push(m_point); # } # } # } # clogger.logger.info("这个中轴线上的所有的点的个数是:" + curvepoints.length); # return curvepoints; # } # # /** # * 这个函数是用来求多边行的各个点的 # * 他利用的原理就是求垂线的距离 # * @return # * # */ # private function getdbx_geometry(curvepoints:Array):Array # { # //定义上边曲线的点坐标数组(当确定军标多边形的时候我是从这个数组开始的) # var topcurepoints:Array=new Array(); # //定义下边曲线的点坐标数组 # var bottomcurepoints:Array=new Array(); # //定义左上测的直线 # var left_top_points:Array=new Array(); # //定义左下侧的直线 # var left_bottom_points:Array=new Array(); # //定义右上角边的数组 # var right_top_points:Array=new Array(); # //定义中轴线离上下两边的距离 # var dis:Number=(8 - map.level) * (8 - map.level) * 40; # //大的框架肯定是从0--1 # var c_length:Number=curvepoints.length; # for (var u:Number=0; u < curvepoints.length; u++) # { # if (u == 0) # { # continue; # } # else # { # var m_point:MapPoint=curvepoints; # var pos_x:Number=m_point.x; # var pos_y:Number=m_point.y; # //先求上一点的坐标 # var pre_point:MapPoint=curvepoints[u - 1]; # //进行计算取的上下边的点 # var pre_pos_x:Number=pre_point.x; # var pre_pos_y:Number=pre_point.y; # var slope:Number=(pos_y - pre_pos_y) / (pos_x - pre_pos_x); # //上边点确定 # var bb:Number=dis * (1 - u / c_length * 0.9); # var cos_number:Number=Math.cos(Math.atan(slope) - Math.PI / 2); # var sin_number:Number=Math.sin(Math.atan(slope) - Math.PI / 2); # var top_pos_x:Number=0.0; # var top_pos_y:Number=0.0; # //下面分四种情况画线。 # if (pre_pos_x <= pos_x && pre_pos_y <= pos_y) # { # top_pos_x=pre_pos_x - ((bb * cos_number) < 0 ? (-bb * cos_number) : (bb * cos_number)); # top_pos_y=pre_pos_y + ((bb * sin_number) < 0 ? (-bb * sin_number) : (bb * sin_number)); # } # else if (pre_pos_x <= pos_x && pre_pos_y > pos_y) # { # top_pos_x=pre_pos_x + ((bb * cos_number) < 0 ? (-bb * cos_number) : (bb * cos_number)); # top_pos_y=pre_pos_y + ((bb * sin_number) < 0 ? (-bb * sin_number) : (bb * sin_number)); # } # else if (pre_pos_x >= pos_x && pre_pos_y >= pos_y) # { # top_pos_x=pre_pos_x + ((bb * cos_number) < 0 ? (-bb * cos_number) : (bb * cos_number)); # top_pos_y=pre_pos_y - ((bb * sin_number) < 0 ? (-bb * sin_number) : (bb * sin_number)); # } # else if (pre_pos_x >= pos_x && pre_pos_y <= pos_y) # { # top_pos_x=pre_pos_x - ((bb * cos_number) < 0 ? (-bb * cos_number) : (bb * cos_number)); # top_pos_y=pre_pos_y - ((bb * sin_number) < 0 ? (-bb * sin_number) : (bb * sin_number)); # } # var top_point:MapPoint=new MapPoint(top_pos_x, top_pos_y); # clogger.logger.info("上半部分是:" + top_pos_x + "yy:" + top_pos_y); # topcurepoints.push(top_point); # //下面来确定下边的曲线 # var bottom_pos_x:Number; # var bottom_pos_y:Number; # if (pre_pos_x <= pos_x && pre_pos_y <= pos_y) # { # bottom_pos_x=pre_pos_x + ((bb * cos_number) < 0 ? (-bb * cos_number) : (bb * cos_number)); # bottom_pos_y=pre_pos_y - ((bb * sin_number) < 0 ? (-bb * sin_number) : (bb * sin_number)); # } # else if (pre_pos_x <= pos_x && pre_pos_y > pos_y) # { # bottom_pos_x=pre_pos_x - ((bb * cos_number) < 0 ? (-bb * cos_number) : (bb * cos_number)); # bottom_pos_y=pre_pos_y - ((bb * sin_number) < 0 ? (-bb * sin_number) : (bb * sin_number)); # } # else if (pre_pos_x >= pos_x && pre_pos_y >= pos_y) # { # bottom_pos_x=pre_pos_x - ((bb * cos_number) < 0 ? (-bb * cos_number) : (bb * cos_number)); # bottom_pos_y=pre_pos_y + ((bb * sin_number) < 0 ? (-bb * sin_number) : (bb * sin_number)); # } # else if (pre_pos_x >= pos_x && pre_pos_y <= pos_y) # { # bottom_pos_x=pre_pos_x + ((bb * cos_number) < 0 ? (-bb * cos_number) : (bb * cos_number)); # bottom_pos_y=pre_pos_y + ((bb * sin_number) < 0 ? (-bb * sin_number) : (bb * sin_number)); # } # var bottom_point:MapPoint=new MapPoint(bottom_pos_x, bottom_pos_y); # bottomcurepoints.push(bottom_point); # } # } # /* # **下面是添加军标箭头的程序,因为现在已经得到上下边界的曲线了。 # ** # */ # var end_curvepoint:MapPoint=curvepoints[curvepoints.length - 1]; # var end_top_point:MapPoint=topcurepoints[topcurepoints.length - 1]; # var end_top_point2:MapPoint=topcurepoints[topcurepoints.length - 2]; # //求箭头上方的点 # var x2:Number=end_curvepoint.x; # var y2:Number=end_curvepoint.y; # var pos_x:Number=end_top_point.x; # var pos_y:Number=end_top_point.y; # var pre_pos_x:Number=end_top_point2.x; # var pre_pos_y:Number=end_top_point2.y; # var end_slope:Number=(pre_pos_y - pos_y) / (pre_pos_x - pos_x); # var b:Number=dis; # var end_point_x:Number; # var end_point_y:Number; # var cos_number:Number=Math.cos(Math.atan(end_slope) - Math.PI / 2); # var sin_number:Number=Math.sin(Math.atan(end_slope) - Math.PI / 2); # //下面分四种情况画线。 # if (pre_pos_x <= pos_x && pre_pos_y <= pos_y) # { # end_point_x=pre_pos_x - ((b * cos_number) < 0 ? (-b * cos_number) : (b * cos_number)); # end_point_y=pre_pos_y + ((b * sin_number) < 0 ? (-b * sin_number) : (b * sin_number)); # } # else if (pre_pos_x <= pos_x && pre_pos_y > pos_y) # { # end_point_x=pre_pos_x + ((b * cos_number) < 0 ? (-b * cos_number) : (b * cos_number)); # end_point_y=pre_pos_y + ((b * sin_number) < 0 ? (-b * sin_number) : (b * sin_number)); # } # else if (pre_pos_x >= pos_x && pre_pos_y >= pos_y) # { # end_point_x=pre_pos_x + ((b * cos_number) < 0 ? (-b * cos_number) : (b * cos_number)); # end_point_y=pre_pos_y - ((b * sin_number) < 0 ? (-b * sin_number) : (b * sin_number)); # } # else if (pre_pos_x >= pos_x && pre_pos_y <= pos_y) # { # end_point_x=pre_pos_x - ((b * cos_number) < 0 ? (-b * cos_number) : (b * cos_number)); # end_point_y=pre_pos_y - ((b * sin_number) < 0 ? (-b * sin_number) : (b * sin_number)); # } # //创建第三个点,然后创建三角形 # clogger.logger.info("右上面的点的横坐标点x:" + end_point_x + "----------右上方点的纵坐标的y:" + end_point_y); # //得到右上方的点 # var end_right_point:MapPoint=new MapPoint(end_point_x, end_point_y); # topcurepoints.push(end_right_point); # //最右边,箭头顶点的坐标是 # clogger.logger.info("顶点的横坐标是x:" + end_curvepoint.x + "---------顶点的纵坐标y:" + end_curvepoint.y); # topcurepoints.push(end_curvepoint); # //下边线 # var end_bottom_point:MapPoint=bottomcurepoints[bottomcurepoints.length - 1]; # var end_bottom_point2:MapPoint=bottomcurepoints[bottomcurepoints.length - 2]; # //求箭头下方向的点 # var bx1:Number=end_bottom_point.x; # var by1:Number=end_bottom_point.y; # var bx0:Number=end_bottom_point2.x; # var by0:Number=end_bottom_point2.y; # var bend_slope:Number=(by1 - by0) / (bx1 - bx0); # var bend_point_x:Number; # var bend_point_y:Number; # var bend_cos_number:Number=Math.cos(Math.atan(bend_slope) - Math.PI / 2); # var bend_sin_number:Number=Math.sin(Math.atan(bend_slope) - Math.PI / 2); # if (bx0 <= bx1 && by0 <= by1) # { # bend_point_x=bx0 + ((b * bend_cos_number) < 0 ? (-b * bend_cos_number) : (b * bend_cos_number)); # bend_point_y=by0 - ((b * bend_sin_number) < 0 ? (-b * bend_sin_number) : (b * bend_sin_number)); # } # else if (bx0 <= bx1 && by0 > by1) # { # bend_point_x=bx0 - ((b * bend_cos_number) < 0 ? (-b * bend_cos_number) : (b * bend_cos_number)); # bend_point_y=by0 - ((b * bend_sin_number) < 0 ? (-b * bend_sin_number) : (b * bend_sin_number)); # } # else if (bx0 >= bx1 && by0 >= by1) # { # bend_point_x=bx0 - ((b * bend_cos_number) < 0 ? (-b * bend_cos_number) : (b * bend_cos_number)); # bend_point_y=by0 + ((b * bend_sin_number) < 0 ? (-b * bend_sin_number) : (b * bend_sin_number)); # } # else if (bx0 >= bx1 && by0 <= by1) # { # bend_point_x=bx0 + ((b * bend_cos_number) < 0 ? (-b * bend_cos_number) : (b * bend_cos_number)); # bend_point_y=by0 + ((b * bend_sin_number) < 0 ? (-b * bend_sin_number) : (b * bend_sin_number)); # } # clogger.logger.info("下方的点的横坐标x为:" + bend_point_x + "-------下方的点的纵坐标y为:" + bend_point_y); # //得到右下方的点 # var bend_right_point:MapPoint=new MapPoint(bend_point_x, bend_point_y); # topcurepoints.push(bend_right_point); # for (var k:Number=bottomcurepoints.length - 1; k >= 0; k--) # { # // 这个是下半部分 # clogger.logger.info("由此开始下半部分:" + bottomcurepoints[k].x + "yyyyy:" + bottomcurepoints[k].y); # topcurepoints.push(bottomcurepoints[k]); # } # //添加尾部的部分,至此完成对军标多边行的算法, # topcurepoints.push(curvepoints[1]); # topcurepoints.push(topcurepoints[0]); # clogger.logger.info("军标上的点的个数是" + topcurepoints.length); # var rings:Array=new Array(); # rings.push(topcurepoints); # return rings; # }