I was working on an application which required to draw a circle using three different circular segment. First it seemed to be easy, but later found out that I needed to learn by trigonometry books again to achieve this. So here it is drawing a circle segment. You need to know the start point and end point of the arc, the center cordinates and the radius.

*public function drawSegment(holder:Sprite,Â startAngle:Number, endAngle:Number,* *segmentRadius:Number, xpos:Number, ypos:Number,* *step:Number, lineColor:Number, fillColor:Number):void {*

*Â Â Â Â //holder-display object, xpos and ypos-center*

*Â Â Â Â holder.graphics.lineStyle(2, lineColor);*

*Â Â Â Â holder.graphics.beginFill(fillColor);*

*Â Â Â Â *//If we need to draw a segment from greater angle to smaller angle, split it at 360 degrees and draw two times

*Â Â Â Â * var originalEnd:Number = -1;

*Â Â Â Â * if(startAngle > endAngle){

*Â Â Â Â **Â Â Â Â * originalEnd = endAngle;

*Â Â Â Â **Â Â Â Â * endAngle = 360;

*Â Â Â Â * }

*Â Â Â Â * var degreesPerRadian:Number = Math.PI / 180;

*Â Â Â Â * var theta:Number;

*Â Â Â Â * startAngle *= degreesPerRadian;

*Â Â Â Â * endAngle *= degreesPerRadian;

*Â Â Â Â * step *= degreesPerRadian;

*Â Â Â Â *// Draw the segment

*Â Â Â Â * holder.graphics.moveTo(xpos, ypos);

*Â Â Â Â * for (theta = startAngle; theta < endAngle; theta += Math.min(step, endAngle – theta)) {

*Â Â Â Â **Â Â Â Â * holder.graphics.lineTo(xpos + segmentRadius * Math.cos(theta), ypos + segmentRadius * Math.sin(theta));

*Â Â Â Â * }

*Â Â Â Â * holder.graphics.lineTo(xpos + segmentRadius * Math.cos(endAngle), ypos + segmentRadius * Math.sin(endAngle));

*Â Â Â Â *if(originalEnd > -1){ // Start angle was greater than end angle and drawing the second segment

*Â Â Â Â **Â Â Â Â * startAngle = 0;

*Â Â Â Â **Â Â Â Â * endAngle = originalEnd * degreesPerRadian;

*Â Â Â Â **Â Â Â Â * for (theta = startAngle; theta < endAngle; theta += Math.min(step, endAngle – theta)) {

*Â Â Â Â **Â Â Â Â **Â Â Â Â * holder.graphics.lineTo(xpos + segmentRadius * Math.cos(theta), ypos + segmentRadius * Math.sin(theta));

*Â Â Â Â **Â Â Â Â * }

*Â Â Â Â **Â Â Â Â * holder.graphics.lineTo(xpos + segmentRadius * Math.cos(endAngle), ypos + segmentRadius * Math.sin(endAngle));

*Â Â Â Â * }

*Â Â Â Â * holder.graphics.lineTo(xpos, ypos);

*Â Â Â Â * holder.graphics.endFill();

*Â Â Â Â * }

From the *updateDisplayList *method you can call the *drawSegment *method like this :

*drawSegment(this, 270, 30, radius, uw/2, uh/2, 2, 0xEEEEEE, 0x003da8);*

The idea is very simple. First draw line from the centre point to the starting point of the arc. Then keep on finding the next point in the circumference through which our arc will be passing. Then draw lines between these points until the end point is reached. After that draw the last line from the end point of the arc to the centre point.

The method considers horizontal point to the right side as angle 0. If we need to draw the line between say, angles 270 and 120, we will split this into two arcs. One from 270 to 360 and second from 0 to 120. We can tune it further to create simpler methods.

That’s it guys. Happy Coding. ðŸ™‚

### Like this:

Like Loading...