**
Clarification of Answer by
theta-ga**
on
10 Oct 2006 20:31 PDT
Hi roland81-ga,
As you know, the midpoint algo provides a list of points which span
a range of 0-45 degrees on the circle. The points for the remaining
315 degrees are calculated by translating these coordinates.
Therefore, the algo to draw an arc between an angular range would
simply calculate the angle made by the current point, and then
translate it to get the other 7 coordinates only if the angles made by
them lie in the given angular range.
You can download the java code demoing this concept here:
http://rapidshare.de/files/36294168/CircleDemo.zip.html
The made arc drawing code is in the Midpoint.java file. The
drawMidpointCircle() method uses the midpoint algo to calculate the
coordinates in one octant, and the drawCirclePoints() method has the
logic for angle calculation and conditional setting of the pixels in
the various octants.
I am including the code for these two methods below:
=========================================================
private void drawMidpointCircle(Graphics g) {
// Settings
int startAngle = 1;
int endangle = 360;
int cx = 150; // x axis value for the center of the circle
int cy = 150; // x axis value for the center of the circle
int radius = 100;
// Standard Midpoint Circle algorithm
int p = (5 - radius * 4) / 4;
int x = 0;
int y = radius;
drawCirclePoints(g, cx, cy, x, y, startAngle, endangle);
while (x <= y) {
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
drawCirclePoints(g, cx, cy, x, y, startAngle, endangle);
}
}
private void drawCirclePoints(Graphics graphicsContext, int
centerX, int centerY, int x, int y, int startAngle, int endAngle) {
// Calculate the angle the current point makes with the circle center
int angle = (int) toDegrees(atan2(y, x));
// draw the circle points as long as they lie in the range specified
if (x < y) {
// draw point in range 0 to 45 degrees
if (90 - angle >= startAngle && 90 - angle <= endAngle) {
setPixel(graphicsContext, centerX - y, centerY - x);
}
// draw point in range 45 to 90 degrees
if (angle >= startAngle && angle <= endAngle) {
setPixel(graphicsContext, centerX - x, centerY - y);
}
// draw point in range 90 to 135 degrees
if (180 - angle >= startAngle && 180 - angle <= endAngle) {
setPixel(graphicsContext, centerX + x, centerY - y);
}
// draw point in range 135 to 180 degrees
if (angle + 90 >= startAngle && angle + 90 <= endAngle) {
setPixel(graphicsContext, centerX + y, centerY - x);
}
// draw point in range 180 to 225 degrees
if (270 - angle >= startAngle && 270 - angle <= endAngle) {
setPixel(graphicsContext, centerX + y, centerY + x);
}
// draw point in range 225 to 270 degrees
if (angle + 180 >= startAngle && angle + 180 <= endAngle) {
setPixel(graphicsContext, centerX + x, centerY + y);
}
// draw point in range 270 to 315 degrees
if (360 - angle >= startAngle && 360 - angle <= endAngle) {
setPixel(graphicsContext, centerX - x, centerY + y);
}
// draw point in range 315 to 360 degrees
if (angle + 270 >= startAngle && angle + 270 <= endAngle) {
setPixel(graphicsContext, centerX - y, centerY + x);
}
}
}
=========================================================
Hope this helps.
If you need any clarifications, just ask!
Regards,
Theta-ga
:)