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 to computes the product of two matrices of size 4x4 using Strassens Algorithm (Improved Divide and Conquer Strategy)

  2. Menu driven telephone directory program

  3. Program that performs array operations like insert,delete, search, sort, merge and display

  4. Program to read a Linear System of Equations,then evaluate it by using Guass-Seidel Itrative Method and show the result

  5. Program to illustrate the functions returning pointers

  6. Program that defines template of vector class that provides modify and multiplication facility

  7. Program to read a Non-Linear equation in one variable, then evaluate it using Simple Itrative Method and display its kD accurate root

  8. Scanner of valide variable name in c

  9. Program to show the implementation of Liang-Barsky Line Clipping Algorithm

  10. Program to perform sorting of linklist

  11. Program to illustrate the binary operator(-) overloading by creating an object of that class

  12. Program of converting class type to basic variable of type string using conversion function

  13. Program to read a Non-Linear equation in one variable, then evaluate it using Modified False-Position Method and display its kD accurate root

  14. Program for election contest among five candidates

  15. Program of time conversion using conversion functions

  16. Program that performs addition operation of 2 polar objects by + operator overloading.

  17. Program of Boundary Fill (8 Connected Point).

  18. Program of stack to traverse in inorder, postorder and preorder

  19. Program of Deterministic Finite Automation (DFA) for identifier, real number with optional Integer and Fractional Part

  20. Program to estimate the Integral value of a given function using Gussian Quadrature Rule

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