Thread: How to make circle in C++?

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    37

    How to make circle in C++?

    Hello sir,
    How to make circle in c++ i am using Dev C++

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    What exactly do you want to do ?

    Create a class Circle that describes a circle

    or

    Draw some circles on the screen.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    37
    HI SIR,
    how to make simple circle in c++ only

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    One option:
    Code:
    class Circle
    {
      Circle(int x, int y, int radius): mCenterX(x), mCenterY(y), mRadius(radius)
      {}
      int GetCenterX() const {return mCenterX;}
      int GetCenterY() const { return mCenterY;}
      int GetRadius() const {return mRadius;}
    private:
      int mCenterX, mCenterY, mRadius;
    };

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    And if you wanted to draw the circle you could do something like this:
    Code:
    // It returns an array of vertices to be used with GL_LINE_STRIP or equivalent
    const std::vector<glm::vec2> genMesh(int segments) {
        float step = 6.283185f/segments;
        float angle = 0.0f;
        std::vector<glm::vec2> vertices;
    
        vertices.reserve(segments + 1);
        for (int i = 0; i < segments; ++i, angle+=step) {
            float vertX = mCenterX + cosf(angle)*mRadius;
            float vertY = mCenterY + sinf(angle)*mRadius;
            vertices.push_back(glm::vec2(vertX, vertY));
        }
        vertices.push_back(glm::vec2(mCenterX+mRadius, mCenterY));
    
        return vertices;
    }
    Last edited by GReaper; 10-28-2017 at 07:50 AM.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    37
    Quote Originally Posted by GReaper View Post
    And if you wanted to draw the circle you could do something like this:
    Code:
    // It returns an array of vertices to be used with GL_LINE_STRIP or equivalent
    const std::vector<glm::vec2> genMesh(int segments) {
        float step = 6.283185f/segments;
        float angle = 0.0f;
        std::vector<glm::vec2> vertices;
    
        vertices.reserve(segments + 1);
        for (int i = 0; i < segments; ++i, angle+=step) {
            float vertX = mCenterX + cosf(angle)*mRadius;
            float vertY = mCenterY + sinf(angle)*mRadius;
            vertices.push_back(glm::vec2(vertX, vertY));
        }
        vertices.push_back(glm::vec2(mCenterX+mRadius, mCenterY));
    
        return vertices;
    }
    Hello
    How to make it
    please tell full code

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It would be a huge undertaking to try to explain and/or program an entire OpenGL or DirectX program here, look them up if you're interested. That or experiment with a simple 2D library such as Allegro.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    For windows users:
    Code:
    #include <Windows.h>
    #include <iostream>
    
    int LastWinError()
    {
      DWORD err = ERROR_SUCCESS;
    
      err = GetLastError();
      std::cerr << "\aWindows error: " << err << "\n\n";
      return static_cast<int>(err);
    }
    
    int main()
    {
      HWND hConsole = GetConsoleWindow();
      if (hConsole ==nullptr)
      {
        return LastWinError();
      }
      HDC hDC = GetDC(hConsole);
      if (hDC == nullptr)
      {
        return LastWinError();
      }
      Ellipse(hDC, 10, 10, 100, 100);
      Sleep(5000); // wait 5 seconds to admire the circle
      ReleaseDC(hConsole, hDC);
    }
    Normally you would create a full Windows application, but for simple demos the console is sufficient.

  9. #9
    Registered User
    Join Date
    Oct 2017
    Posts
    37
    Quote Originally Posted by OldGuy2 View Post
    For windows users:
    Code:
    #include <Windows.h>
    #include <iostream>
    
    int LastWinError()
    {
      DWORD err = ERROR_SUCCESS;
    
      err = GetLastError();
      std::cerr << "\aWindows error: " << err << "\n\n";
      return static_cast<int>(err);
    }
    
    int main()
    {
      HWND hConsole = GetConsoleWindow();
      if (hConsole ==nullptr)
      {
        return LastWinError();
      }
      HDC hDC = GetDC(hConsole);
      if (hDC == nullptr)
      {
        return LastWinError();
      }
      Ellipse(hDC, 10, 10, 100, 100);
      Sleep(5000); // wait 5 seconds to admire the circle
      ReleaseDC(hConsole, hDC);
    }
    Normally you would create a full Windows application, but for simple demos the console is sufficient.
    hello sir
    it is showing error
    Attached Images Attached Images How to make circle in C++?-cplusplus-jpg 

  10. #10
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    You need to compile it with C++11 or replace nullptr with NULL or 0

  11. #11
    Registered User
    Join Date
    Oct 2017
    Posts
    37
    hello sir,
    now it showing error
    how compile it with C++11
    Attached Images Attached Images How to make circle in C++?-aaaaa-jpg 

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    yes, now you have to make everything conform with that standard.
    ( I cannot make out whatever is in the photo)
    essentially if you get an error, update that bit of code to conform to the c++11 standard, google is a good helper.
    Just type in the error into the search, and refine it if needed to see if someone else had same problem.
    So you can get the solution. or post everything in here and wait.

  13. #13
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    You need to include Gdi32.lib

  14. #14
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    This guy has absolutely no idea what he's doing and hasn't explained why or shown anything that he's tried. I don't even think he knows what he's doing.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  15. #15
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Quote Originally Posted by Rodaxoleaux View Post
    This guy has absolutely no idea what he's doing and hasn't explained why or shown anything that he's tried. I don't even think he knows what he's doing.
    Well I guess that's the way nowadays with the internet. Instead of getting a book and learning people just ask questions on forums.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-09-2009, 02:39 PM
  2. Replies: 2
    Last Post: 07-08-2006, 04:42 PM
  3. Circle
    By rifatgunduz in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2005, 03:56 PM
  4. Best way to draw circle!!??
    By OneStiffRod in forum Game Programming
    Replies: 12
    Last Post: 04-28-2003, 07:57 AM
  5. Circle in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 11-06-2001, 09:00 AM

Tags for this Thread