C++ & Opengl
#include <iostream>
using namespace std;
int main(){
int height=0;
cout <<"Enter the height: ";
cin >> height;
for (int row =2; row <=height; row++){
for(int column =1; column<=height+1; column++){
if(column > height - row +1){
cout<< "#";
}
else{
cout << " ";
}
}
cout << endl;
}
system("pause");
}
- Details
- Written by Alece
- Published: 20 June 2014
#include <stdio.h>
#include <iostream>
using namespace std;
int myarray[5][5] = {};
int main(){
int posx=0,posy=2;
for(int x = 0; x<5; x++){
for(int y = 0; y < 5; y++){
myarray[x][y] = 0;
}
}
for(int i = 1; i<=25; i++){
myarray[posx][posy] = i;
if(posx == 0)
{
if (posy+1<=4)
{posx = 4; posy +=1;}
else
posx=posx+1;
}
else
{
if (posy+1 > 4)
{ posx = posx-1; posy = 0;}
else if (myarray[posx-1][posy+1] != 0)
posx = posx +1;
else { posx = posx-1; posy = posy+1;}
}
}
for(int x = 0; x<5; x++){
for(int y = 0; y < 5; y++){
cout << myarray[x][y]<<" ";
}
cout <<endl;
}
system("pause");
}
Output:
- Details
- Written by Alece
- Published: 20 June 2014
Calculate the value of pi from the infinite series
pi = 4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+...
------------------------------------------------------------------
#include <iostream>
using namespace std;
double pi;
int main()
{
int num;
cout << "Enter integer:";
cin >> num;
for (int x=1; x<=num; x++)
{
if(x==1){
pi =4;
cout << x <<"->"<< pi << endl;
}
else if(x%2==0)
{
pi = pi - (4.0/(2.0*x-1.0));
cout << x<< "->" << pi << endl;
}
else{
pi = pi+(4.0/(2.0*x-1.0));
cout << x <<"->" << pi << endl;
}
}
system("pause");
}
- Details
- Written by Alece
- Published: 23 October 2013
#include <iostream>
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#include <OpenGl/glu.h>
usingnamespacestd;
charchessboard[100][100]={};
intqueens;
floatW; floatH;
intsavingposition[100][2]={ };
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
//glEnable(GL_DEPTH_TEST);
}
bool checking (int row, int column){
for(int i = 0; i< queens; i++){
for(int j = 0; j < queens; j++) {
if((j+i== column + row|| column - row == j-i || i== row || j == column) && chessboard[i][j]=='Q')
returnfalse;
}}
returntrue;
}
void removeflag(int row)
{
for(int i=0; i < queens; i++)
{
chessboard[row][i] = '0';
}
}
void backtracking (void)
{
for(int row = 0; row < queens; row++)
{
for(int column = 0; column <queens; column++){
if( checking (row,column) && chessboard[row][column] != '-'){
chessboard[row][column] = 'Q'; removeflag(row+1);
savingposition[row][0] = row;
savingposition[row][1] = column;
row++; column = -1;
if ( row == queens ) break;
}
else if (column == queens-1 && chessboard[row][column] != 'Q')
{
row--;
column = savingposition[row][1];
//cout << row << " " << column << endl;
chessboard[row][column] = '-';
column = 0;
}
}
}
}
void displaytable(void)
{
for(int i = 0; i< queens; i++){
for(int j = 0; j < queens; j++) {
if (chessboard[i][j] != 'Q')
cout << '.';
else
cout << chessboard[i][j];
}
cout << endl;
}
}
void displayboard()
{
glTranslatef(25, H-25, 0.0);
glPushMatrix();
for(int i = 0; i<queens; i++){
glPushMatrix();
for(int j = 0; j < queens; j++) {
if ((i+j)%2 == 0) glColor3f(1.0, 1.0, 1.0); else glColor3f(0.0, 0.0, 0.0);
glPushMatrix(); glScalef(50.0, 50.0, 0.0);
glutSolidCube(1);
if(chessboard[i][j] == 'Q'){ glPushMatrix();
glColor3f(1, 0, 0);
glutSolidSphere(0.4, 50, 50);glPopMatrix();}
glPopMatrix();
glTranslatef(50.0, 0.0, 0.0);
}
glPopMatrix();
glTranslatef(0.0, -50.0, 0.0);
}
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
displayboard();
glPopMatrix();
displaytable();
glutSwapBuffers();
}
void reshape(int w, int h)
{glViewport(0,0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, W, 0, H);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();//important: last line must be glLoadIdentity() for coloring with openGL
//glTranslatef (.0, .0, -5.); //do not translate at all if glLoadIdentity() is used
}
void mouse (int button, int state, int x, int y) //mouse function...
{
int row, column;
switch(button) {
caseGLUT_LEFT_BUTTON:
if(state == GLUT_DOWN)
{ row = (y/50);
column = (x/50);
if (checking(row, column))
chessboard[row][column] = 'Q';}
cout << "X -->" << x<< "Y-->" << y << endl;
glutPostRedisplay();
break;
caseGLUT_RIGHT_BUTTON:
if(state == GLUT_DOWN)
{ row = (y/50);
column = (x/50);
chessboard[row][column] = ' ';}
cout << "X -->" << x<< "Y-->" << y << endl;
//displaytable();
glutPostRedisplay();
break;}
}
int main(int argc, char **argv)
{
int input;
cout << "WELCOME TO N-QUEEN PROGRAM!!!"<<endl;
cout << "1. Show The Solution by Computer!!"<<endl;
cout << "2. Try to solve the puzzle"<<endl;
cout << "3. Exit!!!"<<endl;
cout << "Select the NUMBER from 1-3::";
cin >> input;
switch (input)
{
case 1:
cout << "Please enter the number of the queens 1 to 100:"<<endl;
cin >> queens;
backtracking();break;
case 2:
cout << "What size of N queens do you want to play ?:"<< endl;
cin >> queens;
cout << "Left Cick on the box to select!!\n Right click to deselect ";
break;
case 3:
cout << "THANK YOU FOR PLAYING"; break;
default:
cout << "WRONG INPUT";
break;
}
W = 50*queens;
H = 50*queens;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (W, H);
glutInitWindowPosition(0,0);
glutCreateWindow("Chessboard");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutReshapeFunc(reshape);
glutMainLoop();
return0;
}
- Details
- Written by Alece
- Published: 10 July 2013
#include <stdio.h>
#include <float.h>
#include <math.h>
#include <iostream>
#include <GL/glut.h>
using namespace std;
int angleX, angleY, angleZ=0;
float spin;
static GLuint texture[6];
GLuint LoadTextureRAW( const char * filename)
{
GLuint texture;
- Details
- Written by Alece
- Published: 25 June 2013
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <time.h>
#include <iostream>
using namespace std;
#include <GL/glut.h>
GLuint makeaTree;
float x,y,z;
void makeCylinder(float height, float base){
GLUquadric *obj = gluNewQuadric();
//gluQuadricDrawStyle(obj, GLU_LINE);
glColor3f(0.64f, 0.16, 0.16f);glPushMatrix();
glRotatef(-90, 1.0,0.0,0.0);
gluCylinder(obj, base,base-(0.2*base), height, 20,20);
glPopMatrix();
glutSwapBuffers();
}
- Details
- Published: 25 June 2013