ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C Program To Draw A Line In Dev C++
    카테고리 없음 2021. 4. 30. 06:37

    Home > Articles > Programming

    1. C Program To Draw A Line In Dev C Online
    2. C Program To Draw A Line In Dev C Pdf
    3. Draw A Line In Dev C++
    4. Draw A Line Game Online
    5. Draw A Line Game
    6. Draw A Line Math
    1. Graphics Essentials
    Page 1 of 6Next >

    Draw Rectangle in C graphics rectangle is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. Left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y. Drawing a Line.; 2 minutes to read; In this article. This topic demonstrates how to draw a line using GDI Plus. To draw a line in Windows GDI+ you need a Graphics object, a Pen object, and a Color object. The Graphics object provides the DrawLine Methods method, and the Pen object holds attributes of the line, such as color and width. The address of the Pen object. Apr 13, 2010  Windows Programming; How to draw lines in Dev C. How to draw lines in Dev C. How can I add a line like moveto and lineto in MFC? Which class these two functions stay in? Int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument. Line in c programming: line is a library function of graphics.c in c programming language which is used to draw a line from two coordinates. For example if you want to draw a line from point(x1,y1) to point(x2,y2) you have to use line function like line(x1,y1,x2,y2); Syntax (Declaration of line function in C) line(int x1,int y1, int x2,int y2).

    Learn how to use C++ code to create basic animated graphics in this chapter from Beginning Game Programming by Michael Morrison.
    This chapter is from the book
    Beginning Game Programming

    This chapter is from the book

    This chapter is from the book

    NOTE

    1979 was a critical milestone in the evolution of video games because it brought us Asteroids, which is one of the most enduring games ever. Created by Atari, Asteroids was one of the first vector graphics games, which means that it relied solely on lines to draw graphics, as opposed to little square pixels. Asteroids demonstrated that a very simple concept, with even simpler graphics and a soundtrack that gradually builds tension, creates a mix for success. In fact, Asteroids was so successful that arcade operators had to build larger coin boxes for the games—not a bad problem to have!

    A computer game consists of many different pieces, all of which must come together to form a unique entertainment experience for the player. By far, the most important pieces of any game are the graphics. Graphics are used to represent the characters and creatures in a game, as well as background worlds and other interesting objects that factor into the overall game design. Granted, games have certainly done well because of factors outside of graphics, such as game play and sound quality, but those games are very rare. Besides, nowadays game players expect to see high-quality graphics just as we all expect to see high-quality visual effects in Hollywood movies. So, it's important to develop a solid understanding of graphics programming and how to use graphics wisely in your games.

    In this chapter, you'll learn

    • The basics of drawing graphics using the Windows Graphics Device Interface

    • What a device context is and why it's important to GDI graphics

    • How to paint text and primitive graphics in Windows

    • How to create a sample program that demonstrates GDI graphics in the context of the game engine

    Graphics Essentials

    Before jumping into the details of how graphics work in Windows and how they are applied to games, it's important to establish some ground rules and gain an understanding of how computer graphics work in general. More specifically, you need to have a solid grasp on what a graphics coordinate system is, as well as how color is represented in computer graphics. The next couple of sections provide you with this knowledge, which you'll put to practical use a little later in the chapter.

    Understanding the Graphics Coordinate System

    All graphical computing systems use some sort of graphics coordinate system to specify how points are arranged in a window or on the screen. Graphics coordinate systems typically spell out the origin (0, 0) of the system, as well as the axes and directions of increasing value for each of the axes. If you're not a big math person, this simply means that a coordinate system describes how to pinpoint any location on the screen as an XY value. The traditional mathematical coordinate system familiar to most of us is shown in Figure 3.1.

    Figure 3.1 The traditional XY coordinate system is commonly used in math.

    Windows graphics relies on a similar coordinate system to specify how and where drawing operations take place. Because all drawing in Windows takes place within the confines of a window, the Windows coordinate system is applied relative to a particular window. The Windows coordinate system has an origin that is located in the upper-left corner of the window, with positive X values increasing to the right and positive Y values increasing down. All values in the Windows coordinate system are positive integers. Figure 3.2 shows how this coordinate system looks.

    Figure 3.2 The Windows XY coordinate system is similar to the traditional coordinate system except that it applies to the client area of windows.

    NOTE

    When I talk about drawing graphics in a window, I'm actually referring to the client area of a window, which doesn't include the title bar, menus, scrollbars, and so on. In the case of games, you can think of the client area of the main game window as the game screen. You learn more about the client area of a window later in this chapter in the section titled, 'Painting Windows.'

    If the Windows graphics coordinate system sounds a little complicated, just think of it in terms of a classic game of Battleship. In Battleship, you try to sink enemy ships by firing torpedoes at specific locations on a grid. Battleship uses its own coordinate system to allow you to specify locations on the grid where ships might be located. Similarly, when you draw graphics in Windows, you specify locations in the client area of a window, which is really just a grid of little squares called pixels.

    Learning the Basics of Color

    A topic that impacts almost every area of game graphics is color. Fortunately, most computer systems take a similar approach to representing color. The main function of color in a computer system is to accurately reflect the physical nature of color within the confines of a computer. This physical nature isn't hard to figure out; anyone who has experienced the joy of Play-Doh can tell you that colors react in different ways when they are combined with each other. Like Play-Doh, a computer color system needs to be capable of mixing colors with accurate, predictable results.

    Color computer monitors provide possibly the most useful insight into how software systems implement color. A color monitor has three electron guns: red, green, and blue. The output from these three guns converges on each pixel on the screen, stimulating phosphors to produce the appropriate color. The combined intensities of each gun determine the resulting pixel color. This convergence of different colors from the monitor guns is very similar to the convergence of different colored Play-Doh.

    NOTE

    Technically speaking, the result of combining colors on a monitor is different from that of combining similarly colored Play-Doh. The reason for this is that color combinations on a monitor are additive, meaning that mixed colors are added together to become white; Play-Doh color combinations are subtractive, meaning that mixed colors are subtracted from each other to become black. Whether the color combination is additive or subtractive depends on the physical properties of the particular medium involved.

    The Windows color system is very similar to the physical system used by color monitors; it forms unique colors by using varying intensities of the colors red, green, and blue. Therefore, Windows colors are represented by the combination of the numeric intensities of the primary colors (red, green, and blue). This color system is known as RGB (Red Green Blue) and is standard across most graphical computer systems.

    NOTE

    RGB isn't the only color system used by computers. Another color system used heavily in desktop publishing applications is CMYK, which stands for Cyan-Magenta-Yellow-Black. Colors in the CMYK color system are expressed in terms of the color components cyan, magenta, yellow, and black, as opposed to red, green, and blue in RGB. The CMYK color system is used heavily in printing because CMYK printing inks are subtractive in nature, making it easier to print using a four-color ink combination (cyan, magenta, yellow, and black); hence the term four-color printing.

    Table 3.1 shows the numeric values for the red, green, and blue components of some basic colors. Notice that the intensities of each color component range from 0 to 255 in value.

    Table 3.1 Numeric RGB Color Component Values for Commonly Used Colors

    C Program To Draw A Line In Dev C Online

    Color

    Red

    Green

    Blue

    White

    255

    255

    255

    Black

    0

    0

    0

    Light Gray

    192

    192

    192

    Medium Gray

    128

    128

    128

    Dark Gray

    64

    64

    64

    Red

    255

    0

    0

    Green

    0

    255

    0

    Blue

    0

    0

    255

    Yellow

    255

    255

    0

    Purple

    255

    0

    255


    The Win32 API defines a structure named COLORREF that combines the red, green, and blue components of an RGB color into a single value. The COLORREF structure is important because it is used throughout the Win32 API to represent RGB colors. To create a color as a COLORREF structure, you use the RGB() macro, which accepts red, green, and blue color components as arguments. Here is an example of creating a solid green color using RGB():

    The color created in this line of code is green because the green component (the middle argument) is specified as 255, whereas the red and blue components are specified as 0. Changing the values of these three arguments alters the mix of the color—with lower numbers resulting in darker colors and higher numbers resulting in brighter colors.

    You can experiment with RGB color combinations in the standard Paint program that comes with Windows. Double-click one of the colors in the color palette in the lower left corner of the Paint window. Then, click the Define Custom Colors button in the Edit Colors dialog box. You can then either type numbers into the Red, Green, and Blue edit fields or click to select a color and intensity (see Figure 3.3).

    Figure 3.3 The standard Windows Paint program allows you to specify colors via RGB values.


    Related Resources

    • Book $35.99
    • Book $39.99
    • Book $27.99

    Paint program in c:- This program can draw different shapes using mouse such as line, circle, pixel and many other shapes. You can also change the color, clear the screen. Code of paint program in C is given below:-

    C Program To Draw A Line In Dev C Pdf

    /* To understand the code see output below the code, it will help you in understanding the code. */

    C programming code

    #include<graphics.h>

    Draw A Line In Dev C++

    #include<dos.h>
    #include<math.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<conio.h>

    union REGS i, o;
    int leftcolor[15];

    int get_key()
    {
    union REGS i,o;

    i.h.ah=0;
    int86(22,&i,&o);

    return( o.h.ah);
    }

    void draw_color_panel()
    {
    int left, top, c, color;

    left =100;
    top =436;

    color = getcolor();
    setcolor(GREEN);
    rectangle(4,431,635,457);
    setcolor(RED);
    settextstyle(TRIPLEX_FONT,0,2);
    outtextxy(10,431,'Colors : ');

    for( c =1; c <=15; c++)
    {
    setfillstyle(SOLID_FILL, c);
    bar(left, top, left+16, top+16);
    leftcolor[c-1]= left;
    left +=26;
    }

    setcolor(color);
    }

    void draw_shape_panel()
    {
    int left, top, c, color;

    left =529;
    top =45;

    color = getcolor();
    setcolor(GREEN);
    rectangle(525,40,633,255);

    for( c =1; c <=7; c++)
    {
    rectangle(left, top, left+100, top+25);
    top +=30;
    }
    setcolor(RED);
    outtextxy(530,45,'Bar');
    outtextxy(530,75,'Line');
    outtextxy(530,105,'Pixel');
    outtextxy(530,135,'Ellipse');
    outtextxy(530,165,'Freehand');
    outtextxy(530,195,'Rectangle');
    outtextxy(530,225,'Clear');
    setcolor(color);
    }

    void change_color(int x,int y)
    {
    int c;

    for( c =0; c <=13; c++)
    {
    if( x > leftcolor[c]&& x < leftcolor[c+1]&& y >437&& y <453)
    setcolor(c+1);
    if( x > leftcolor[14]&& x <505&& y >437&& y <453)
    setcolor(WHITE);
    }
    }

    char change_shape(int x,int y)
    {
    if( x >529&& x <625&& y >45&& y <70)
    return'b';
    elseif( x >529&& x <625&& y >75&& y <100)
    return'l';
    elseif( x >529&& x <625&& y >105&& y <130)
    return'p';
    elseif( x >529&& x <625&& y >135&& y <160)
    return'e';
    elseif( x >529&& x <625&& y >165&& y <190)
    return'f';
    elseif( x >529&& x <625&& y >195&& y <220)
    return'r';
    elseif( x >529&& x <625&& y >225&& y <250)
    return'c';
    }

    void showmouseptr()
    {
    i.x.ax=1;
    int86(0x33,&i,&o);
    }

    void hidemouseptr()
    {
    i.x.ax=2;
    int86(0x33,&i,&o);
    }

    void restrictmouseptr(int x1,int y1,int x2,int y2)
    {
    i.x.ax=7;
    i.x.cx= x1;
    i.x.dx= x2;
    int86(0x33,&i,&o);

    i.x.ax=8;
    i.x.cx= y1;
    i.x.dx= y2;
    int86(0x33,&i,&o);
    }

    void getmousepos(int*button,int*x,int*y)
    {
    i.x.ax=3;
    int86(0x33,&i,&o);

    C Program To Draw A Line In Dev C++

    *button = o.x.bx;
    *x = o.x.cx;
    *y = o.x.dx;
    }

    main()
    {
    int gd = DETECT,gm;

    Draw A Line Game Online

    int maxx,maxy,x,y,button,prevx,prevy,temp1,temp2,key,color;
    char ch ='f';// default free-hand drawing

    initgraph(&gd,&gm,'C:TCBGI');

    maxx = getmaxx();
    maxy = getmaxy();

    setcolor(BLUE);
    rectangle(0,0,maxx,maxy);

    setcolor(WHITE);
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
    outtextxy(maxx/2-180,maxy-28,'<a href='http://www.programmingsimplified.com'>www.programmingsimplified.com'</a>);

    draw_color_panel();
    draw_shape_panel();

    setviewport(1,1,maxx-1,maxy-1,1);

    restrictmouseptr(1,1,maxx-1,maxy-1);
    showmouseptr();
    rectangle(2,2,518,427);
    setviewport(1,1,519,428,1);

    while(1)
    {
    if(kbhit())
    {
    key = get_key();

    if( key 1)
    {
    closegraph();
    exit(0);
    }
    }

    C program to draw a line in dev c online

    getmousepos(&button,&x,&y);

    if( button 1)
    {
    if( x >4&& x <635&& y >431&& y <457)
    change_color( x, y );
    elseif( x >529&& x <625&& y >40&& y <250)
    ch = change_shape( x, y );

    temp1 = x ;
    temp2 = y ;

    if( ch 'f')
    {
    hidemouseptr();
    while( button 1)
    {
    line(temp1,temp2,x,y);
    temp1 = x;
    temp2 = y;
    getmousepos(&button,&x,&y);
    }
    showmouseptr();
    }

    while( button 1)
    getmousepos(&button,&x,&y);

    Draw A Line Game

    /* to avoid interference of mouse while drawing */
    hidemouseptr();

    if( ch 'p')
    putpixel(x,y,getcolor());

    Draw A Line Math

    elseif( ch 'b')
    {
    setfillstyle(SOLID_FILL,getcolor());
    bar(temp1,temp2,x,y);
    }
    elseif( ch 'l')
    line(temp1,temp2,x,y);
    elseif( ch 'e')
    ellipse(temp1,temp2,0,360,abs(x-temp1),abs(y-temp2));
    elseif( ch 'r')
    rectangle(temp1,temp2,x,y);
    elseif( ch 'c')
    {
    ch ='f';// setting to freehand drawing
    clearviewport();
    color = getcolor();
    setcolor(WHITE);
    rectangle(2,2,518,427);
    setcolor(color);
    }

    showmouseptr();
    }
    }
    }

    댓글

Designed by Tistory.