Google Answers Logo
View Question
 
Q: Circular Arc Drawing - Computer Graphics ( Answered,   0 Comments )
Question  
Subject: Circular Arc Drawing - Computer Graphics
Category: Computers > Algorithms
Asked by: roland81-ga
List Price: $10.00
Posted: 30 Sep 2006 11:57 PDT
Expires: 30 Oct 2006 10:57 PST
Question ID: 769733
I'm looking for the algorithm used to do draw circular arcs based 
on the midpoint circle drawing algorithm. I'm not interested in the
trigonometric methods or explanations how to use the arcdraw methods, 
nor in the midpoint algorithm to draw full circle.

I'm looking to draw arcs (begining and ending angle & radius are given)
and I want to plot the arc pixel by pixel.

Some sample code, preferably in java would be helpful.
Answer  
Subject: Re: Circular Arc Drawing - Computer Graphics
Answered By: theta-ga on 02 Oct 2006 11:56 PDT
 
Hi roland81-ga,
   You can find a java applet demonstrating exactly what you want
(drawing an arc pixel by pixel using the midpoint circle drawing algo)
on this webpage:
         - Java Applets Center
           [http://www.cosc.canterbury.ac.nz/people/mukundan/cogr/CircleMP.html]
   The page contains the applet, the java method that draws the arc,
as well as a link to the full java code.

Hope this helps.
If you need any clarifications, just ask!

Regards,
Theta-ga
:)



=====================================================================
Google Search Terms Used:
  graphics algorithm to draw arc using  midpoint

Request for Answer Clarification by roland81-ga on 02 Oct 2006 16:14 PDT
Thanks. I found the link myself and this does not answer my question.

The link only presents the midpoint algorithm for drawing FULL circles
without the symmetric mapping part in the other 7 divisions of the
circle. This is the only reason that it is able to draw a circular
arc, which is in fact just the 1/8 th of the circle, the other 7/8th
being maped from the 1/8th.

The circular arc needs to be able to take a begining and ending value
anywhere between 0 -> 360 degrees, your example only works ONLY for
the for a begining value of 0 and a ending value of 45 degrees.

Clarification of Answer by theta-ga on 03 Oct 2006 12:54 PDT
Hi roland81-ga, 
    I will take a look at the algo, and post an updated solution in a
couple of days.

Regards,
Theta-ga

Clarification of Answer by theta-ga on 06 Oct 2006 19:45 PDT
Quick Update: I will be working on the code over the weekend, and will
post the java sample app on Monday.

Regards,
Theta-ga

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
:)
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy