Sunday, December 22
C++ geometry [for game projects and similar]
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
=======================================================================
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
=======================================================================
- Simple lines:
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.
Saturday, December 14
Use of ascii characters to print table in console
A post on using ascii chart for printing a table in console
#include <iostream> using namespace std; int main() { char topLeftCorner = (char)201, bottomLeft = (char)200, topRight= (char)187; char bottomRight = (char)188, horizontal = (char)205, vertical = (char)186; cout<<topLeftCorner; for(int i=0; i<10;i++) cout<<horizontal; cout<<topRight<<endl; cout<<vertical; for(int i=0;i<10;i++)cout<<' '; cout<<vertical; cout<<endl<<bottomLeft; for(int i=0; i<10;i++) cout<<horizontal; cout<<bottomRight; //or you can use ascii character 219 for all of the purposes, it is very easy to use return 0; }
Wednesday, December 11
sorting of character arrays based on first character only
A code for Sir Saeed
/** * code by http://CppQA.blogspot.com * sorting of character arrays based on first character only * C++ */ #include <iostream> #include <string.h> using namespace std; int main() { char countries[5][15] = {"Pakistan","India","Afghanistan","Iran","China"}; for(int i=0;i<5;i++) { for(int j=0;j<5-1;j++) { if(countries[i][0] < countries[j][0]) {//swaping char temp[15]; strcpy(temp,countries[i]); strcpy(countries[i],countries[j]); strcpy(countries[j],temp); } } } //print for(int i=0;i<5;i++)cout<<countries[i]<<endl; return 0; }
Sunday, December 8
Filing in C++
Its very irritating to input again and again and best way to avoid it is FILING and it is pretty simple :)
Code:
Note:
Your .txt file should be in the same location as that of your .cpp file.
For More Info:
For more details and possible values of parameters visit the link below
http://www.cplusplus.com/doc/tutorial/files/
Include filing in your programs and ENJOY
Regards
Code:
#include<iostream.h> #include<conio.h> #include<fstream.h> void main(){ char name[15]; //---------------- READING FROM FILE --------------- ifstream insertion("contries.txt"); cout<<"\t\tReading data from file contries.txt\n\n"<<endl; while(insertion>>name) { cout<<name<<endl; } insertion.close(); //--------------- WRITING IN FILE ------------------ //to clear the data of "data.txt" so that new content overwrites the file // if u want to append new data to previous file skip the following two lines ofstream empty("data.txt"); empty.close(); //writing new data in "data.txt" cout<<"\t\tWriting following data in file data.txt\n\n"<<endl; int copyng[]={1995,1996,1997,1998,1999,2000,0,8,7,6}; ofstream burr("data.txt",ios::app); int n=sizeof(copyng)/sizeof(copyng[0]); //to find out the size of array in terms of # of elements for(int i=0;i<n;i++) { cout<<copyng[i]<<endl; burr<<" "<<copyng[i]; } burr.close(); cout<<"\n\t\tPress any key to exit"<<endl; getche(); }
Note:
Your .txt file should be in the same location as that of your .cpp file.
For More Info:
For more details and possible values of parameters visit the link below
http://www.cplusplus.com/doc/tutorial/files/
Include filing in your programs and ENJOY
Regards