C++ Programming Tutorial

 
 
 

Graphics Programming

Quadratic Surfaces

Draw a Sphere using Ellipses Draw a Sphere using Parametric Equations Draw an Ellipsoid using Parametric Equations

Character Generation

Urdu Alphabets using Stroke Method Urdu Alphabets using Matrix Method

Arc

Circular Arc using Trigo. Method Elliptical Arc using Trigo. Method

General Programs

C-Curve of nth order K-Curve of nth order Cubic Bezier Curve Bezier Curve of nth degree Scanfill algorithm Boundary Fill - 8 Connected Point Flood fill algorithm Rotate About Origin Rotate about reference point Scaling about origin Scaling about reference point Polyline translation Reflection in x axis Reflection in y Axis Reflection on any line Midpoint Circle Drawing Bresenhams Line Algorithm (BLA) Generate a pattern Draw a Chess Board Draw a Luddo Board Deterministic Finite Automation for identifier Kurskals algo - Minimum Cost Spanning Tree

Windows Programs

Checkbox like windows Simple windows & buttons Moving message box like windows Text box Graphical Rep. of tower of hanoi Graphical menu - operate it using arrow keys Text animation

Line

Line using Parametric equations Line-Cartesian Slope-Intercept Equation simple imp Line using Cartesian Slope-Intercept Equation Line - BLA - slopes negative and greater than 1 Line - BLA - slopes negative and less than 1 Line - BLA - slopes positive and greater than 1 Line - BLA - slopes positive and less than 1 DDA line drawing algorithm Bresenham line drawing algorithm Cohen sutherland Line clipping algo.

Line Styles

Different kinds of Dashed Lines Different kinds of Thick Lines

Polygons

Draw a Polygon Draw a Triangle Draw a Rectangle Sutherland-Hodgeman Polygon Clipping Algo

Circle

Circle using Trigo. Method Circle using Polynomial Method Circle using Bresenhams Circle algo. Circle using MidPoint Circle algo.

Ellipse

Ellipse using Polynomial Method Ellipse using Trigo. Method Ellipse using MidPoint Ellipse algo.

2D Transformations

Translation Transformation Scaling Transformation Scaling Trans along a Fixed Point Scaling Trans along Arbitrary Direction Rotation Transformation Rotation Trans along a Pivot Point Reflection tran of x-axix, y-axis and w.r.t origin Reflection tran of line y=x and y=-x X-Direction Shear Transformation Y-Direction Shear Transformation

2D Viewing - Clipping

Window-to-Viewport Coordinate Tran Point Clipping Algorithm Cohen-Sutherland Line Clipping Algo Cohen-Sutherland MidPoint Subdivision Line Nicol Lee Nicol algo. for Line Clipping Liang-Barsky Line Clipping Algo Window-to-Viewport Transformaton None-or-All String Clipping Strategy None-or-All Character Clipping Strategy

3D Object Representations

3D object using Polygon-Mesh Rep. 3D object - Translational Sweep Representatiom 3D object - Rotational Sweep Rep.

3D Transformations

3D Rotation Trans along x-axis 3D Rotation Trans along y-axis 3D Rotation Trans along z-axis 3D Reflection Trans along xy-plane 3D Reflection Trans along yz-plane 3D Reflection Trans along zx-plane 3D Shearing Trans along x-axis 3D Shearing Trans along y-axis 3D Shearing Trans along z-axis

Bezier Curves - Surfaces

3D Cubic Bezier Curve 3D Bezier Curve of nth degree 3D Piece-Wise Bezier Curve of nth degree 3D Bezier Surface for MxN control points

Projection

3D objects - Standard Perspective Projection 3D obj - Arbitrary Plane and Center of Projection 3D objects using General Perspective Projection 3D obj-Orthographics Proje Parallel onto xy-plane 3D obj-Cavalier Oblique Parallel prj-xy-plane 3D obj-Cabinet Oblique Parallel prj - xy-plane

Fill Algorithm or Area Filling

Geometric shapes using Boundary Geometric shapes - Boundary - Linked List Geometric shapes using Flood Geometric shapes - Flood - Linked-List Polygon using Scan Line Polygon Rectangle using Scan-Line Rectangle Circle using Scan-Line Circle Circle - Scan-Line Circle - Polar Coordinates
 
 
/*
    The Main class is TextField

    The constructors are

        TextField(int x,int y,int len,char * str) &
        TextField(int x,int y,int len)

            Here:
                x   - X position where textbox will be drawn
                y   - Y position where textbox will be drawn
                len - length of the array (string)
                str - Specifies the text the default text
                                        of text)

    The methods are

        void setText(char *str)
            Stores str to the text field. Note that this wont display 
                the text. You will have to explicithly call draw method.

        void draw()
            Draws the text field with the string in the variable str.

        void hide()
            Hides the text field

        char *getText()
            get the text from the text field. Press enter to exit from 
            this.

    The Keys of the text field includes:

        Escape                   : Clears the text field.
        Left & Right Cursor keys : To move left and right respectively.
        Home                     : To move to home.
        End                         : To move to end.
        Delete                     : Deletes the character at current
                                    cursor position.
        Back Space                 : Deletes the character before the current
                                    cursor position.
        Insert                     : To insert character into the field.


*/

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>

#define ESC 27
#define ENTER 13
#define RIGHT 77
#define LEFT 75
#define HOME 71
#define END 79
#define INSERT 82
#define DEL 83
#define BKSP 8
#define TXW 8
#define TXH 8

/*
   To Check the status of Insert
       1 if ON
       0 if OFF
*/

int ins_state()
{
    unsigned char far *stat=(char far*)0x417;
    char status;
    status=*stat;
    if((status&128)==128)
        return 1;
    else
        return 0;
}

class TextField
{
    private:
        char str[21];
        int startx,starty,size;
        void draw(char *strin);
    public:
        TextField(int x1,int y1,int s,char *str1)
        {
            startx=x1+2;
            starty=y1+2;
            strcpy(str,str1);
            size=s;
        }
        TextField(int x1,int y1,int s)
        {
            strcpy(str,\"\");
            startx=x1+2;
            starty=y1+2;
            size=s;
        }
        void setText(char *str1);
        void draw();
        char *getText();
        void hide();
        void cursor(int x,int y,int ins,char ch);
};

void TextField::hide()
{
    setcolor(getbkcolor());
    setfillstyle(SOLID_FILL,getbkcolor());
    bar(startx-3,starty-3,startx+size*TXW+9,starty+TXH+3);
}

void TextField::setText(char *str1)
{
    strcpy(str,str1);
}

void TextField::draw()
{
    setcolor(RED);
    setfillstyle(SOLID_FILL,WHITE);
    bar(startx-2,starty-2,startx+size*TXW+8,starty+TXH+2);
    outtextxy(startx,starty,str);
}

void TextField::cursor(int x,int y,int ins,char ch)
{
    y+=2;
    flushall();
    char str[2];
    str[0]=ch;
    str[1]=\'\\0\';
    while(!kbhit())
    {
        setcolor(RED);
        if (ins)
            outtextxy(x,y,\"Ü\");
        else
            outtextxy(x,y,\"_\");
        delay(500);
        setcolor(WHITE);
        if (ins)
            outtextxy(x,y,\"Ü\");
        else
            outtextxy(x,y,\"_\");
        setcolor(RED);
        if (ch!=0)
            outtextxy(x,y-2,str);
        delay(500);
    }
    setcolor(WHITE);
    if (ins)
        outtextxy(x,y,\"Ü\");
    else
        outtextxy(x,y,\"_\");
    setcolor(RED);
    if (ch!=0)
        outtextxy(x,y-2,str);
}
char* TextField::getText()
{
    draw();
    int cur_x,xend,ins,len;
    char cur_ch,ch;
    int stx;
    cur_x=xend=textwidth(str);
    ins=ins_state();
    cur_ch=str[cur_x/TXW];
    cursor(cur_x+startx,starty,ins,cur_ch);
    ch=getch();
    setcolor(RED);
    while (ch!=ENTER)
    {
        if ((ch!=0)&&((isalnum(ch))||(ch==\'.\')||(ch==\' \')))
        {
            ins=ins_state();
            if (!ins)
            {
                if (cur_x==xend)
                {
                    if (xend/TXW<size)
                    {
                        strncat(str,&ch,1);
                        xend+=TXW;
                        cur_x+=TXW;
                    }
                }
                else
                {
                    str[cur_x/TXW]=ch;
                    cur_x+=TXW;
                }
            }
            else if (xend/TXW<size)
            {
                len=strlen(str);
                if (cur_x==xend)
                    strncat(str,&ch,1);
                else
                {
                    str[len+1]=\'\\0\';
                    for (int i=len;i>=cur_x/TXW;i--)
                        str[i]=str[i-1];
                    str[cur_x/TXW]=ch;
                }
                xend+=TXW;
            }
        }
        else if (ch==ESC)
        {
            setText(\"\");
            cur_x=xend=0;
        }
        else if ((ch==BKSP)&&(cur_x>0))
        {
            len=strlen(str);
            for (int i=(cur_x-1)/TXW;i<len;i++)
                str[i]=str[i+1];
            str[len]=\'\\0\';
            xend-=TXW;
            cur_x-=TXW;
        }
        else if (ch==0)
        {
            ch=getch();
            if ((ch==LEFT)&&(cur_x>0))
                cur_x-=TXW;
            else if ((ch==RIGHT)&&(cur_x<xend))
                cur_x+=TXW;
            else if (ch==HOME)
                cur_x=0;
            else if (ch==END)
                cur_x=xend;
            else if ((ch==DEL)&&(cur_x<xend))
            {
                len=strlen(str);
                for (int i=(cur_x+1)/TXW;i<len;i++)
                    str[i]=str[i+1];
                str[len]=\'\\0\';
                xend-=TXW;
            }
            ins=ins_state();
        }
        if (cur_x==xend)
            cur_ch=0;
        else
            cur_ch=str[cur_x/TXW];
        draw();
        stx=startx;
        cursor(cur_x+stx,starty,ins,cur_ch);
        ch=getch();
    }
    return str;
}

//Example

void main()
{
    int gd=DETECT,gm=0;
    initgraph(&gd,&gm,\"\");
    char *st=\"Hello\";
    TextField t1(100,100,10,st);
    t1.draw();
    char *str=t1.getText();
    closegraph();
    cout<<str;
    getch();
}

    Related Post:
  1. Program that list of letters should be an alphabetical list of upper case letters followed by an alphabetical list of lower case letters

  2. Program to illustrate the functions returning pointers

  3. Program to fill a Circle using Scan-Line Circle Fill Algorithm using Polar Coordinates

  4. CREATING A CLASS ACCOUNTS FROM WHICH ARE DERIVED TWO CLASSES CURRENT AND SAVINGS AND THEN ASK THE USER FOR THE TYPE OF ACCOUNT HE WANTS TO CREATE

  5. Program to coumputes and displays the factorial of the given number ( using Recursive Algorithm )

  6. Program to implement a Translator that reads an Infix Expression translates it into a Postfix Expression and evaluate the Postfix Expression

  7. Program to illustrate the use of the break statement

  8. Program to illustrate the difference among public, protected and private inheritance

  9. Program of LL Parser

  10. Program to illustrate the implementation of Double Ended linked list as a Stack

  11. Program to draw different kinds of Thick Lines

  12. Program to draw a circle using Bresenhams Circle Algorithm

  13. Program to draw a spiral rotating clockwise at the center of the screen

  14. To parse a string using First and Follow algorithm and LL-1 parser

  15. Program of link list using class pointers

  16. Program to illustrate the implementation of X-Direction Shear Transformation

  17. Program to capitalize the first charcter of every word in a string

  18. Program to illustrate the Quick Sort

  19. Program to write macro that obtains the largest of three numbers

  20. Program to illusrate data conversion user defined data types using constructor

 
 
Didn't find what you were looking for? Find more on Program of text box