Jump to content
  • Advertisement
Sign in to follow this  
tcige

how to draw a circle with 'Bresenham'

This topic is 1971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

first i use D3DFVF_XYZRHW, the effect is ok with D3DPT_POINTLIST, but i do not know how to arrange the vertex to draw the solid circle with D3DPT_TRIANGLELIST

 

then i move to D3DFVF_XYZ, i have to change the parameter of D3DXMatrixLookAtLH to suit the radius, but the drawing effect is awful

Share this post


Link to post
Share on other sites
Advertisement

Why not with triangle fan?

 

 

D3DXVECTOR2* D3DXVec2Rotate(D3DXVECTOR2* vOut, D3DXVECTOR2* vIn, float rad, const D3DXVECTOR2* vCenter)
{
    float cr = std::cos(rad);
    float sr = std::sin(rad);

    float X = vIn->x;
    float Y = vIn->y;

    X -= vCenter->x;
    Y -= vCenter->y;

    vOut->x = X * cr - Y * sr;
    vOut->y = X * sr + Y * cr;

    vOut->x += vCenter->x;
    vOut->y += vCenter->y;

    return vOut;
}
 
...
struct VERTEX
    {
        D3DXVECTOR4 p;
    };

    static const std::size_t numPts = 20; // points cnt on circle
    std::vector<VERTEX> vVertices;
    d3d9device->SetFVF(D3DFVF_XYZRHW); // pre-tranformed vertex
    VERTEX center; // vertex in center of circle
    center.p = D3DXVECTOR4(500.0f, 400.0f, 0.0f, 1.0f); // set circle center on screen center (1000x800) / 2
    D3DXVECTOR2 ct(center.p.x, center.p.y);
    vVertices.push_back(center); // add center first beause of triangle fan "rule"
    static const float rad = 200.0f; // circle radius
    for(std::size_t i = 0; i < numPts; ++i)
    {
        float ang = (D3DX_PI * 2.0f) * ((float)i / (float)numPts);
        D3DXVECTOR2 pt(center.p.x + rad, center.p.y + rad);
        D3DXVec2Rotate(&pt, &pt, ang, &ct);
        VERTEX vert;
        vert.p = D3DXVECTOR4(pt.x, pt.y, 0.0f, 1.0f);
        vVertices.push_back(vert);
    }
    vVertices.push_back(vVertices[1]); // add first point (on circle) to close
    d3d9device->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, numPts, &vVertices[0], sizeof(VERTEX) );
Edited by belfegor

Share this post


Link to post
Share on other sites

Check this thread: 

 

http://www.gamedev.net/topic/259860-drawing-circle-in-directx/

 

Maybe the source code does what you are looking for.

 

[edit] How many thousand / million circles you are planning to draw in order to be bottle necked by sin/cos ??? I'd say that sin&cos isn't the biggest problem here. Especially if you pre-create your geometry, the sin&cos won't have any effect while drawing.

 

Cheers!

Edited by kauna

Share this post


Link to post
Share on other sites

+1 What kauna said.

 

Alternative fast sin/cos

 

float fast_sin(float rad)
{
    static const float B = 4 / D3DX_PI;
    static const float C = -4 / (D3DX_PI * D3DX_PI);

    float y = B * rad + C * rad * std::abs(rad);
    return y;
}

float fast_cos(float rad)
{
    static const float B = 4 / D3DX_PI;
    static const float C = -4 / (D3DX_PI * D3DX_PI);

    rad += 1.57079632f;
    float y = B * rad + C * rad * std::abs(rad);
    return y;
}

 

http://devmaster.net/forums/topic/4648-fast-and-accurate-sinecosine/

 

My code above is just an example, you could precalculate most stuff at init time, no need to do that every frame.

Edited by belfegor

Share this post


Link to post
Share on other sites

Why not draw a line list then? You don't need to draw triangles in wireframe to get lines.

 

Cheers!

Share this post


Link to post
Share on other sites

wireframe is an example

 

using Bresenham, the vertex is out of order, i have no idea how to arrange the vertex

 

using cosf and sinf according to the angle is simple, but not effecitve

Share this post


Link to post
Share on other sites

What is the thing with Bresenham's algorithm? Can you make a drawing for us what you want to accomplish?

 

You want to draw a circle using points? That's highly inefficient (even more inefficient than using sin and cos btw). 

 

Have you made some performance analysis which point out that sin and cos are your problem?

 

Best regards!

Share this post


Link to post
Share on other sites

that algorithm is simple, and it works fine with D3DFVF_XYZRHW and D3DPT_POINTLIST

 

i want to apply it to D3DFVF_XYZ and solid circle

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!