GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Dec-2008, 04:33
juned juned is offline
Awaiting Email Confirmation
 
Join Date: Dec 2008
Posts: 9
juned is on a distinguished road
Thumbs down

Is ellipse only way to do circle?


Hi, my name is Juned Wahab.

When I draw a circle or ellipse, i use
CPP / C++ / C Code:
Ellipse(hDC, 40, 40, 240, 240);

The problem is, I do not know the detail of how the computer actually draws the circle from that command. I just presumed it was something to do with Pi, which is why I made the statement SINE VS. PYTHAGARUS (sorry about my spelling). Let me explain.

When I draw a circle using my sine formula - let's just say, they do not look exactly the same shape, but they are both circles. When you look close up and analyse the pixel positions, they are slighlty different. It could apply to embedded systems i.e., digital to analogue converters. I'll give you a little bit more code:
CIRCLE WITH SINE
CPP / C++ / C Code:
for (x = 0; x < 200; x ++)
{
  y = int (sin (x / (13 + 0.5)) * (20 * (5 + 0.25)));
  sinarrayx[x] = y;
  sinarrayy[x] = y;
  sinarrayy[x + 200] = y;
}

Maybe the title should have been:-

ELLIPSE VS. SINE

Also, when I said "manipulate a cirlce", I meant cirlces to ellipses. Oval shapes. Displaying a circle from different perspectives, as well as moving sprites in a circlular wave form, which to the best of my knowledge cannot be done simply with the 'Ellipse' command.

Which leads to the 'SPIRAL'.

Altering the code for 'circle with sine', slightly, you generate a spiral. You can display the spiral from many angles.

Can you do a spiral shape from a C++ command, like;

CPP / C++ / C Code:
LineTo(hDC, ...
or
CPP / C++ / C Code:
Ellipse(hDC, ...

To apply the code, you need to SetPixel.
CPP / C++ / C Code:
for (x = 0; x < 120; x ++)
{
  SetPixel(hDC, sinarrayx[x] + 200, sinarrayy[x + 20] + 200, 255);
}
The colour is red, so use appropriate backgound colours.

Again, when I alter the values in the sine program, you can see the circle from various angles - perspective. It looks to have more offsets than using the 'Ellipse' command.

If you would like to see a free demo, please email: junedwahab@yahoo.co.uk

Cheers.
  #2  
Old 26-Dec-2008, 10:57
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Is ellipse only way to do circle?


Quote:
Originally Posted by juned
When I draw a circle or ellipse, i use
CPP / C++ / C Code:
Ellipse(hDC, 40, 40, 240, 240);
OK. I perceive that you are using the Windows GDI, and that is a way to draw a circle.
Quote:
Originally Posted by juned
The problem is, I do not know the detail of how the computer actually draws the circle from that command.
Neither do I. The Microsoft Windows GDI is closed-source software, and the details may change from one Microsoft release to another.
Quote:
Originally Posted by juned
I just presumed it was something to do with Pi, which is why I made the statement SINE VS. PYTHAGARUS
What the heck does the Ellipse function have to do with pi? I mean, if I guessed that it may use pi internally in some way, what would be the difference to the user program interface?

Who cares?

Furthermore, what the heck does anything in any of your posts have to do with Pythagoras?
Quote:
Originally Posted by juned
When I draw a circle using my sine formula... I'll give you a little bit more code:
CIRCLE WITH SINE
CPP / C++ / C Code:
for (x = 0; x < 200; x ++)
{
  y = int (sin (x / (13 + 0.5)) * (20 * (5 + 0.25)));
  sinarrayx[x] = y;
  sinarrayy[x] = y;
  sinarrayy[x + 200] = y;
}

If you would plot sinarrayy[i] versus sinarrayx[i] as i goes from 0 to 199, you get, roughly, two and a half cycles of a sinusoid, not a circle. If you want specific advice, I respectfully suggest that you post the exact code that you are having problems with. (Or having problems understanding.)

If you want to plot a circle (or any other continuous curve) point-by-point, you can use the GDI function MoveToEx() followed by any number of LineTo() function calls.

For a complete example:
CPP / C++ / C Code:
/*
 * Draw circles with moveto-lineto and with Ellipse()
 * function calls.
 *
 * Tested with several command-line compilers on Windows XP:
 *
 * Microsoft:
 *     cl     drawing.cpp /link user32.lib gdi32.lib
 *       or, simply
 *     cl     drawing.cpp user32.lib gdi32.lib
 *
 * Borland
 *     bcc32 -W drawing.cpp
 *
 * GNU
 *     g++    drawing.cpp -luser32 -lgdi32 -o drawing
 *      or, simply
 *     g++    drawing.cpp -mwindows -o drawing
 *
 *     davekw7x
 */

//
// The following is needed for Visual C++ version 6 command line
// compiler and for g++ on windows xp
//
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif

#include <windows.h>
#include <math.h>

// Some math.h headers define M_PI; some do not
#ifndef M_PI
# define M_PI  3.14159265358979323846
#endif

const char g_szClassName[] = "davesCircleDrawingClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,
                         LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
                   MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                          g_szClassName,
                          "Window",
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT, 720, 480,
                          NULL, NULL, hInstance, NULL);

    if (hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
                   MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while (GetMessage(&Msg, NULL, 0, 0) > 0) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return int(Msg.wParam);
}

//
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    const int npoints = 200; // For point-by-point plotting

    switch (msg) {

    case WM_PAINT:
        HDC hDC;
        PAINTSTRUCT Ps;
        hDC = BeginPaint(hwnd, &Ps);
        int x[npoints], y[npoints];
        double cx;
        double cy;
        double omegat;

        int radius;
        int i;

        // Paint circles with moveto-lineto
        SelectObject(hDC, GetStockObject(DC_PEN));
        SetDCPenColor(hDC, RGB(0, 0, 255));
        radius = 100;
        cx = 2.25 * radius;
        cy = radius;
        for (i = 0; i < npoints; i++) {
            omegat = 2.0 * M_PI * double(i) / npoints + 0.5;
            x[i] = int(cx + radius * cos(omegat) + 0.5);
            y[i] = int(cy + radius * sin(omegat) + 0.5);
        }

        MoveToEx(hDC, x[0], y[0], NULL);
        for (i = 1; i < 200; i++) {
            LineTo(hDC, x[i], y[i]);
        }
        LineTo(hDC, x[0], y[0]);

        cy += 2 * radius;
        for (i = 0; i < npoints; i++) {
            omegat = 2.0 * M_PI * i / npoints + 0.5;
            x[i] = int(cx + radius * cos(omegat) + 0.5);
            y[i] = int(cy + radius * sin(omegat) + 0.5);
        }

        MoveToEx(hDC, x[0], y[0], NULL);
        for (i = 1; i < 200; i++) {
            LineTo(hDC, x[i], y[i]);
        }
        LineTo(hDC, x[0], y[0]);

        SelectObject(hDC, GetStockObject(DC_PEN));
        SetDCPenColor(hDC, RGB(255, 0, 0));

        //
        //Paint circles with Ellipse() function calls
        //
        radius = 100;
        cx += 2.5*radius;
        cy = radius;
        Ellipse(hDC, int(cx-radius), int(cy-radius), 
                     int(cx+radius), int(cy+radius));

        cy += 2 * radius;
        radius = 100;
        Ellipse(hDC, int(cx-radius), int(cy-radius), 
                     int(cx+radius), int(cy+radius));

        EndPaint(hwnd, &Ps);
        break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

By using Windows GDI pens to plot the lines (rather than trying explicitly to set the pixels) you can easily adjust pen width and other attributes. Furthermore, you can use a fairly small number of points since the LineTo() function fills in the pixels between the points. (The "smoothness" is determined by the number of points, obviously, since it plots the stuff in piece-wise linear fashion, but the output will be a continuous figure.)

Regards,

Dave
Last edited by davekw7x : 26-Dec-2008 at 11:36.
  #3  
Old 26-Dec-2008, 16:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Is ellipse only way to do circle?


Quote:
Originally Posted by davekw7x
If you would plot sinarrayy[i] versus sinarrayx[i] as i goes from 0 to 199, you get, roughly, two and a half cycles of a sinusoid, not a circle....
Oops...
What I should have written would indicate the following:

Actually, the way you wrote the code that would be a straight line. If you plot sinarrayy[i] versus i you get two and a half cycles of sinusoid.

My point remains the same: If you would like some help with some code, then I think it would be better if you showed us the actual code.

Dave
  #4  
Old 27-Dec-2008, 17:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Is ellipse only way to do circle?


Quote:
Originally Posted by juned
...
Can you do a spiral shape from ...

CPP / C++ / C Code:
/*
 * Draw a spiral with moveto-lineto
 *
 * Tested with several command-line compilers on Windows XP:
 *
 * Microsoft:
 *     cl     spiral.cpp /link user32.lib gdi32.lib
 *       or, simply
 *     cl     spiral.cpp user32.lib gdi32.lib
 *
 * Borland
 *     bcc32 -W spiral.cpp
 *
 * GNU
 *     g++    spiral.cpp -luser32 -lgdi32 -o spiral
 *      or, simply
 *     g++    spiral.cpp -mwindows -o spiral
 *
 *     davekw7x
 */

//
// The following is needed for Visual C++ version 6 command line
// compiler and for g++ on Windows xp
//
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif

#include <windows.h>
#include <math.h>

// Some math.h headers define M_PI; some do not
#ifndef M_PI
# define M_PI  3.14159265358979323846
#endif

const char g_szClassName[] = "davesCircleDrawingClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,
                         LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
                   MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                          g_szClassName,
                          "Dave's Spiral",
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
                          NULL, NULL, hInstance, NULL);

    if (hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
                   MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while (GetMessage(&Msg, NULL, 0, 0) > 0) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return int(Msg.wParam);
}

//
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    const int npoints = 1000; // For point-by-point plotting

    switch (msg) {

    case WM_PAINT:
        HDC hDC;
        PAINTSTRUCT Ps;
        HPEN hBluePen;
        hDC = BeginPaint(hwnd, &Ps);
        int x[npoints], y[npoints];
        double cx;
        double cy;
        double omega;
        double delta_omega;
        double revolutions;
        double total_omega;

        int radius;
        int i;
        double r_value;

        // Paint a blue spiral with lineto
        hBluePen = CreatePen(PS_SOLID, 5, RGB(0, 0, 255));

        SelectObject(hDC, hBluePen);
        //
        // This is the starting value of radius
        //
        // For a circle, the radius is constant.
        //
        // For a spiral, the radius decreases as omega increases.
        //
        radius = 200;
        //
        // Coordinates of the center
        //
        cx = 155;
        cy = 155;

        revolutions = 3.0;
        total_omega = 2.0*M_PI * revolutions;
        delta_omega = total_omega / npoints;
        for (i = 0, omega = 0; i < npoints; i++, omega += delta_omega) {

            //
            // The function for r_value determines the
            // "steepness" of the spiral
            //
            // Try different values.
            //
            // This is like a logarithmic spiral, except the
            // radius gets smaller rather than larger as
            // omega increases.
            // That is, it shrinks rather than grows.
            //
            // Like the growth of a nautilus with time running
            // backwards. (Benjamin Button?)
            //
            //r_value = radius * exp(-omega);
            r_value = radius * exp(-omega/(2.0*M_PI));

            x[i] = int(cx + r_value * cos(omega) + 0.5);
            y[i] = int(cy + r_value * sin(omega) + 0.5);
        }

        MoveToEx(hDC, x[0], y[0], NULL);
        for (i = 1; i < npoints; i++) {
            LineTo(hDC, x[i], y[i]);
        }

        EndPaint(hwnd, &Ps);
        break;


    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

See attachment for screen shot.

Regards,

Dave
Attached Images
File Type: pdf Spiral.pdf (8.1 KB, 26 views)
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help on Dealing with Archives. VersEtreOuNe C++ Forum 15 14-Feb-2008 05:31
Inheritance and classes mortomarty C++ Forum 2 29-Sep-2007 06:09
Help on homework! kemmal C Programming Language 1 06-Nov-2006 11:22
coordinates of a circle IRONMAN C++ Forum 2 02-Jun-2006 13:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:32.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.