C++ gurus and learners, we program ourselves :P all work is CC licensed

Sunday, December 22

C++ geometry [for game projects and similar]

15:17 By

Hi.
 Let's just call it a graphics function blog thoe i just know 2 or 3 of them but they'll be enough for simple drawing.

 This will require gotoxy function which is mentioned in a blog from Umair Sajid. The recap is as follow:
________________________________________________________________
#include <iostream>
#include <windows.h>

void gotoxy ( int , int );       //prototype
{main function and stuff}
void gotoxy(int x,int y)       //header. with x and y as coordinates to where cursor goes
{
     HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
                                         //output handle to be defined for function
     COORD coordinates;
                                         //COORD is a data type of windows.h header to store 2D graphic position's                                                  coordinates.
     coordinates.X = x;
                                         //save x coordinate value into the COORD structure's data type
     coordinates.Y = y;
                                         //similarly for y axis coordinate
     SetConsoleCursorPosition( hOut , coordinates );
                                         //finaly the function which set's the position of the cursor that you
                                           gave
}
define it and then you are good to go for the graphics functions
=======================================================================
  1. Simple lines:
     To understand this, just look up to coordinates of a simple line:
They change in a simple manner:
x,y
x+1,y
x+2,y
x+3,y
x+4,y
x+5,y
Here is a more understandable form:
x,y
++x,y
++x,y
++x,y
++x,y
++x,y
so here is the functions:
________________________________________________________________________
1. Horizontal line:
void horizontal_line(int , int , int);
void horizontal_line(int x, int y, int size)
{
      for(int i=0;i<size;i++)
      {
            gotoxy(x,y);
            cout<<char(219);
            x++;
      }
}
2. Vertical line:
void vertical_line(int , int , int);
void vertical_line(int x, int y, int size)
{
      for(int i=0;i<size;i++)
      {
            gotoxy(x,y);
            cout<<char(219);

            y++;
      }
}
=======================================================================
 2. Simple squares:
void square(int , int , int , int);
void square( int x, int y, int width, int length )
{
       int t_x;
       for(int i=0;i<length;i++)
       {
              t_x=x;
              for(int j=0;j<width;j++)
             {
                     gotoxy(t_x,y);
                     cout<<char(219);
                     t_x++;
             }
              y++;
        }
}
=======================================================================
You can also use colours for making it better.
Errors in code are possible as i did'nt made it on compiler so don't think that it won't work. In case of any problem you can email me.
That's all. I hope this'll help ya with simple interfaces. I'll give other methods for circle, hollow square ( completely hollow ) when obviously i need to make them and use them :)
Asslam-o-Alalikum.

4 comments :