admin管理员组

文章数量:821293

python绘制扇形代码

/****

* DrawSectorTest

* 动态绘制扇形实例(拖拽绘制)

****/

package fengzi.drawing

{

import flash.display.Shape;

import flash.display.Sprite;

import flash.events.MouseEvent;

public class DrawSectorTest extends Sprite

{

private var _sector:Sprite;

private var _dragSp:Sprite;

private var _radian:Number;//弧度

private var _radius:uint;//半径

private var _angle:Number;//初始角度

/***

* DrawSectorTest 构造

* @param radius 半径

* @param angle 初始角度

***/

public function DrawSectorTest(radius:uint=100,angle:Number=60)

{

_radius=radius

_angle=angle

_sector = new Sprite();

_dragSp = new Sprite();

_sector.x = _dragSp.x = stage.stageWidth * 0.5;

_sector.y = _dragSp.y = stage.stageHeight * 0.5;

this.addChild(_sector);

this.addChild(_dragSp);

_dragSp.graphics.beginFill(0,.2);

_dragSp.graphics.drawRect(0,-5,_radius,10);

_dragSp.graphics.endFill();

drawSector(_sector,0,0,_radius,_angle,0);

_dragSp.rotation = _angle;

_dragSp.addEventListener(MouseEvent.MOUSE_DOWN,onDragDown);

}

private function onDragDown(e:MouseEvent):void

{

stage.addEventListener(MouseEvent.MOUSE_MOVE,onDragMove);

stage.addEventListener(MouseEvent.MOUSE_UP,onDragOut);

}

private function onDragMove(e:MouseEvent):void

{

_dragSp.rotation = Math.atan2(_dragSp.parent.mouseY - _dragSp.y,_dragSp.parent.mouseX - _dragSp.x) * 180 / Math.PI;

_radian = (_dragSp.rotation) * Math.PI / 180;

/*

这是关键,因为rotation的区间是[0,180][0,-180],如果直接用_dragSp.rotation会导致拖拽绘制扇形错误

*/

if (_dragSp.rotation > 180)

{

_radian = _radian + Math.PI;

}

if (_radian < 0)

{

_radian = _radian + 2 * Math.PI;

}

drawSector(_sector,0,0,_radius,_radian*180/Math.PI,0);

}

private function onDragOut(e:MouseEvent):void

{

stage.removeEventListener(MouseEvent.MOUSE_MOVE,onDragMove);

stage.removeEventListener(MouseEvent.MOUSE_UP,onDragOut);

}

/***

* 绘制扇形

* @param mc 承载扇形的对象

* @param x 圆心角x

* @param y 圆心角y

* @param r 半径

* @param angle 绘制角度

* @param startAngle 起始角度

* @param color 填充颜色

* @param hasFrame 是否填充边框

***/

private function drawSector(sp:Sprite,x:Number=0,y:Number=0,r:Number=100,angle:Number=60,startAngle:Number=0,color:Number=0xff0000,hasFrame:Boolean=true):void

{

sp.graphics.clear();

if (hasFrame)

{

sp.graphics.lineStyle(0,0xffff00);

}

sp.graphics.beginFill(color,1);

sp.graphics.moveTo(x,y);

angle=(Math.abs(angle)>360)?360:angle;

var n:Number = Math.ceil(Math.abs(angle) / 45);

var angleA:Number = angle / n;

angleA = angleA * Math.PI / 180;

startAngle = startAngle * Math.PI / 180;

sp.graphics.lineTo(x+r*Math.cos(startAngle),y+r*Math.sin(startAngle));

for (var i:int=1; i<=n; i++)

{

startAngle += angleA;

var angleMid:Number = startAngle - angleA / 2;

var bx:Number=x+r/Math.cos(angleA/2)*Math.cos(angleMid);

var by:Number=y+r/Math.cos(angleA/2)*Math.sin(angleMid);

var cx:Number = x + r * Math.cos(startAngle);

var cy:Number = y + r * Math.sin(startAngle);

sp.graphics.curveTo(bx,by,cx,cy);

}

if (angle!=360)

{

sp.graphics.lineTo(x,y);

}

sp.graphics.endFill();

}

}

}

本文标签: python绘制扇形代码