mishkapp
mishkapp

Reputation: 79

Straight line in c++

What libraries does Windows provide to draw lines? I am only interested in 2D libraries, not OpenGL or DirectX. I am working in C++.

Upvotes: 1

Views: 7453

Answers (3)

Josh
Josh

Reputation: 12566

cout << ".------------------------------------------------------------." << endl;

cout << ".\n\
         |\n\
         |\n\
         |\n\
         |\n\
         |\n\
         |\n\
         |\n\
         |\n\
         |\n\
         |\n\
         ." << endl;

EDIT: Forgot the dots.

EDIT 2: Diagonal:

for( int i=0; i<10; i++ )
{
    for( int j=0; j<10; j++ )
    {       
        if( i == j )
        {
            for( int k=0; k<i; k++ )
            {
                cout << " ";
            }

            if( i == 0 || i == 9 )
            {
                cout << ".\n";
            }
            else
            {
                cout << "\\\n";
            }
        }
    }       
}

Upvotes: 9

Nathanael
Nathanael

Reputation: 1772

Depends on your platform.

In Windows you could use GDI or GDI+.

For Mac OS I'm sure both Carbon and Cocoa provide this feature - though I confess to having little knowledge of either API.

Qt provides cross-platform drawing libraries that will work on any of Linux, Windows or Mac.

EDIT:

Direct2D is another C++ option for Windows. It's fully hardware accelerated too which is cool. As for drawing on a fullscreen window, it's no different than drawing in a regular window. You'll just need some extra code to maximize the window and set it to fullscreen mode.

Upvotes: 3

semisight
semisight

Reputation: 964

It depends. What system are you on? How would you like to draw it? In 3D or 2D? Do you want fullscreen?

Honestly, OpenGL is pretty easy to use with a library like GLUT. After you set it up, all it takes is

glBegin(GL_LINES);
    //Vertex pair
    glVertex2f(...);
    glVertex2f(...);
glEnd();

(Purists will yell at me for using a form of OpenGL that isn't around in the 3.0 standard, but I assume this isn't a major project needing forward-compatibility for a long time).

Two other quick libraries that come to mind:

  1. SDL Sounds like your library: it has quick setup, lots of tutorials, and it's plain C. Unfortunately, you'll have to use another library (i.e. SDL_Draw) to actually draw the line.
  2. Allegro I used this library a few years ago, but not since then. My recollection is that it's a lot like SDL in form and function. It works well on Windows, but is not as easy as SDL on OS X. Allegro does(!) have a built in line drawing function called line() (gotta love that brevity).

Upvotes: 0

Related Questions