Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
142 user(s) are online (136 user(s) are browsing Forums)

Members: 0
Guests: 142

more...

Support us!

Headlines

 
  Register To Post  

Draw an arc with graphics.library
Amigans Defender
Amigans Defender


See User information
What's the best way to draw an arc using graphics.library, given two angles in degrees which depict the starting and ending angle of the arc from the horizontal?

I came up with this unoptimised monstrosity, which unsurprisingly doesn't work:

void arc_gfxlib(int xint yint radiusint angle1int angle2int quarter)
{
    
double angle1_r angle1 * (M_PI 180.0);
    
double angle2_r angle1 * (M_PI 180.0);
    
double anglebc;
    
double step = (angle2_r angle1_r) / ((angle2_r angle1_r) * (double)radius);
    
int x0y0x1y1;

    switch(
quarter) {
        case 
1:
            
x0 x;
            
y0 y;
        break;
        
        case 
2:
            
x0 = -x;
            
y0 y;
        break;
        
        case 
3:
            
x0 = -x;
            
y0 = -y;
        break;
        
        case 
4:
            
x0 x;
            
y0 = -y;
        break;
    }
    
    
angle1_r;
    
angle2_r;
    
    for(
angle bangle <= cangle += step) {
        
x1 = (int)(cos(angle) * (double)radius);
        
y1 = (int)(sin(angle) * (double)radius);
        
WritePixel(rpx0 x1y0 y1);
    }
}

void arc(int xint yint radiusint angle1int angle2)
{
    
int a1a2;

    
a1 angle1;
        
    if(
angle1 <= 90) {
        if(
angle2 90a2 90;
            else 
a2 angle2;
        
arc_gfxlib(xyradiusa1a21);
        
a1 90;
    }
    
    if(
angle1 <= 180) {
        if(
angle2 180a2 180;
            else 
a2 angle2;
        if((
angle2 90) && angle2 <= 180)
            
arc_gfxlib(xyradiusa1a22);
        
a1 180;
    }
    
    if(
angle1 <= 270) {
        if(
angle2 270a2 270;
            else 
a2 angle2;
        if((
angle2 180) && angle2 <= 270)
            
arc_gfxlib(xyradiusa1a23);
        
a1 270;
    }

    if(
angle2 270) {
        
arc_gfxlib(xyradiusa1angle24);
    }
}


Go to top
Re: Draw an arc with graphics.library
Home away from home
Home away from home


See User information
Quote:

What's the best way to draw an arc using graphics.library, given two angles in degrees which depict the starting and ending angle of the arc from the horizontal?


I would use a paramtric approach, to determine coordinates on the arc, then draw straight llines between each set of coordinates. Set the ineterval small eneough to get a smooth arc.

Also this looks suspect.

(angle2_r - angle1_r) / ((angle2_r - angle1_r) * (double)radius);

if I'm not reading it wrong don;t the terms in (angle2_r - angle1_r) cancel leaving you with just

1 / (double)radius

ouch and

double angle1_r = angle1 * (M_PI / 180.0);
double angle2_r = angle1 * (M_PI / 180.0);

means that angle2_r = angle1_r

Unless you made a typo whilst posting that's definetly not goining to work!


http://www.mathopenref.com/coordparamellipse.html
http://www.mathopenref.com/coordcirclealgorithm.html




Go to top
Re: Draw an arc with graphics.library
Amigans Defender
Amigans Defender


See User information
Quote:

broadblues wrote:

double angle1_r = angle1 * (M_PI / 180.0);
double angle2_r = angle1 * (M_PI / 180.0);

means that angle2_r = angle1_r

Unless you made a typo whilst posting that's definetly not goining to work!


Oops. That might explain why I was only getting a dot rather than an arc.

I'll fix that tomorrow and see if I get more expected results.

Go to top
Re: Draw an arc with graphics.library
Just popping in
Just popping in


See User information
Hi Chris

The algorithm I used is based upon Bresenham's (http://en.wikipedia.org/wiki/Bresenham's_line_algorithm) and is at http://en.wikipedia.org/wiki/Midpoint_circle_algorithm. If you're looking for a smooth transition between 2 points then quaternions work well as they allow you to smoothly interpolate motion in a way that matrices can't

cheers

billy

Go to top
Re: Draw an arc with graphics.library
Amigans Defender
Amigans Defender


See User information
Quote:

Chris wrote:
I'll fix that tomorrow and see if I get more expected results.


Yes, it works now, with some tweaks (such as remembering screen y coordinates are upside down) - thanks!

Quote:
billyfish wrote:
The algorithm I used is based upon Bresenham's


I think that was what I looked at originally. The examples always draw a whole circle, and how to get it to only plot an arc is a bit vague. It's likely miles faster than using trigonometry for each point though, so it'll be good to get that working.

Go to top
Re: Draw an arc with graphics.library
Just popping in
Just popping in


See User information
Quote:

Chris wrote:

Quote:
billyfish wrote:
The algorithm I used is based upon Bresenham's


I think that was what I looked at originally. The examples always draw a whole circle, and how to get it to only plot an arc is a bit vague. It's likely miles faster than using trigonometry for each point though, so it'll be good to get that working.


It works for arc sections too, take a look at the "Drawing incomplete octants" on the midpoint circle algorithm page.

If you can get a copy of Computer Graphics by Foley et al. it really is a goldmine of useful stuff (http://www.amazon.co.uk/Computer-Grap ... TF8&qid=1351020762&sr=1-3)

cheers

billy

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project