Showing posts with label Implementation. Show all posts
Showing posts with label Implementation. Show all posts

Transform.cpp: implementation of the Transform class C++

// Transform.cpp: implementation of the Transform class.


#include "Transform.h"
#include <iostream>

//Please implement the following functions:

// Helper rotation function.
mat3 Transform::rotate(const float degrees, const vec3& axis) {
  mat3 alongMat = mat3(
 axis.x*axis.x, axis.x*axis.y, axis.x*axis.z,
 axis.x*axis.y, axis.y*axis.y, axis.y*axis.z,
 axis.x*axis.z, axis.y*axis.z, axis.z*axis.z
  );
  mat3 perpMat = mat3(
 0, axis.z, -axis.y,
 -axis.z, 0 , axis.x,
 axis.y, -axis.x, 0
  );
  return cos(glm::radians(degrees))*mat3(1.0) + (1-cos(glm::radians(degrees)))*alongMat + sin(glm::radians(degrees))*perpMat;
}

// Transforms the camera left around the "crystal ball" interface
void Transform::left(float degrees, vec3& eye, vec3& up) {
eye = eye * rotate(degrees, up);
up = up * rotate(degrees, up);
}

// Transforms the camera up around the "crystal ball" interface
void Transform::up(float degrees, vec3& eye, vec3& up) {
vec3 pivot = glm::normalize(glm::cross(up, eye));
eye = eye * rotate(degrees, pivot);
up = up * rotate(degrees, pivot);
}

// Your implementation of the glm::lookAt matrix
mat4 Transform::lookAt(vec3 eye, vec3 up) {
vec3 a = eye/*-vec3(4.0,5.1,3.2)*/;
vec3 b = up;
// eye = glm::normalize(eye);
vec3 w = a/glm::length(a);
vec3 u = glm::cross(b,w)/glm::length(glm::cross(b,w));
vec3 v = glm::cross(w,u);
mat4 M = mat4(
vec4(u.x,v.x,w.x,0),
vec4(u.y,v.y,w.y,0),
vec4(u.z,v.z,w.z,0),
vec4(
-u.x*eye.x-u.y*eye.y-u.z*eye.z,
-v.x*eye.x-v.y*eye.y-v.z*eye.z,
-w.x*eye.x-w.y*eye.y-w.z*eye.z,
1
)
);

  return M;
}

Transform::Transform()
{

}

Transform::~Transform()
{

}

C++ Sudoku Dancing Links Implementation

C++ Sudoku Dancing Links Implementation Program By Naim Hashmi


//Naim Hashmi, 2014
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

class Sudoku{
  private: // Dancing Links toroidal structures
    class header;
    class node{
    public:
      int row;
      node * up, * down, * left, * right;
      header * head;
    };
    class header : public node{
    public:
      int size;
      bool visible;
      header * left, * right;
    };
    int u, u2, u3, rowsNumber, columnsNumber, solutionsNumber, maxSolutionsNumber;
    header * root;
    vector<header> columns;
    vector<vector<node> > rows;
    vector<int> solutions;
   
public:
  Sudoku(int dimension) :
  u(dimension),
  u2(u*u),
  u3(u2*u2),
  rowsNumber(u3*u2),
  rows(rowsNumber, vector<node>(4, node())),
  columnsNumber(u3*4+1),
  columns(columnsNumber, header()),
  solutions(u3, 0)
  { // create the 729x324 matrix
    root = &(columns[u3*4]);
    for (int i = 0; i < columnsNumber; i++) {
      columns[i].up = &(columns[i]);
      columns[i].down = &(columns[i]);
      columns[i].left = &(columns[(i+columnsNumber-1)%columnsNumber]);
      columns[i].right = &(columns[(i+1)%columnsNumber]);
      columns[i].visible = true;
      columns[i].size = 0;
    }
    for (int i = 0; i < rowsNumber; i++) {
      int r = i/(u3), c = i%u3/u2, p = i%u2;
      for (int j = 0; j < 4; j++) {
int columnIndex;
// http://www.stolaf.edu/people/hansonr/sudoku/exactcover.htm
switch (j){
 case 0: // cell constraints
columnIndex = u3*j + r*u2+c;
break;
 case 1: //row constraints
columnIndex = u3*j + r*u2+p;
break;
 case 2: //column constraints
     columnIndex = u3*j + c*u2+p;
     break;
 case 3: //block constraints
     columnIndex = u3*j + (r/u*u+c/u)*u2+p;
     break;
}
rows[i][j].row = i;
rows[i][j].left = &(rows[i][(j+3)%4]);
rows[i][j].right = &(rows[i][(j+1)%4]);
rows[i][j].down = rows[i][j].head = &(columns[columnIndex]);
rows[i][j].up = columns[columnIndex].up;

columns[columnIndex].up->down = columns[columnIndex].up = &(rows[i][j]);
columns[columnIndex].size++;
      }
    }
  };
 
  void print(int * s){
    for (int i=0; i<u3; i++){
      if (i!=0 && i%(u*u2)==0){
cout << "\n";
for (int j=0; j<u; j++){
 for (int k=0; k<u*2+2*((j==0 || j==u-1) ? 1 : 2); k++) cout << "-";
 if (j!=u-1) cout << "+";
}
      }
      if      (i%u2==0) cout << "\n";
      else if (i%u==0) cout << "  \u007C  ";
     
      cout << s[i]  << " ";
    }
    cout << "\n";
  }
 
  int solver(int * grid, int max=1, bool fill=true){
    int i = 0;
    solutionsNumber = 0;
    maxSolutionsNumber = max;
    for (int j = 0; j<u3; j++){
      if (grid[j]) {
int rowIndex = j*u2+grid[j]-1;
if (rows[rowIndex][0].head->visible==false ||
 rows[rowIndex][1].head->visible==false ||
 rows[rowIndex][2].head->visible==false ||
 rows[rowIndex][3].head->visible==false) {
 return -1; // unvalid puzzle
 break;
 }
 for (int k = 0; k<4; k++)
   cover(rows[rowIndex][k].head);
 solutions[i++] = rowIndex;
      }
    }
    backtrack(i);
    if (solutionsNumber==0) return 0;
    if (fill)
      for (int j=0; j<u3; j++)
grid[solutions[j]/u2] = solutions[j]%u2+1;
   
    for (int j=i-1; j>=0; j--)
      for (int k=3; k>=0; k--)
uncover(rows[solutions[j]][k].head);
      return solutionsNumber;
  }
 
  int generator (int * grid, int numbers=-1, int limit=1000, int randomness = 100){
    for (int i=0; i<u3; i++)
      grid[i] = 0;
    solver(grid);
   
    srand((unsigned)time(0));
    for (int i=0; i<randomness; i++){
      int box = rand()%3, begin = rand()%3, end = rand()%3;
      if (rand()%2==0){
for (int j=0; j<u2; j++){
 int beginIndex = box*u2*u+begin*u2+j,
     endIndex = box*u2*u+end*u2+j;
 int t = grid[endIndex];
 grid[endIndex] = grid[beginIndex];
 grid[beginIndex] = t;
}
      }else{
for (int j=0; j<u2; j++){
 int beginIndex = box*u+j*u2+begin,
     endIndex = box*u+j*u2+end;
 int t = grid[endIndex];
 grid[endIndex] = grid[beginIndex];
 grid[beginIndex] = t;
}
      }  
    }
   
    if (numbers==-1) numbers = u3-u3/3;
    for (int i=0; i<numbers; i++){
      int t, r, c=0;
      do{
if (c!=0) grid[r] = t;
r = rand()%u3;
t = grid[r];
grid[r] = 0;
if (++c>limit) break;
      }while(solver(grid, 2, false)!=1);
    }
  }
 
private:
  void cover(header *column){ // cover a column and its nodes' rows
  column->visible = false;
  column->right->left = column->left;
  column->left->right = column->right;
  for (node *i = column->down; i!=column; i=i->down){
    for (node *j = i->right; j!=i; j=j->right) {
      j->down->up = j->up;
      j->up->down = j->down;
      j->head->size--;
    }
  }
  }
 
  void uncover(header *column){ // uncover a column and its nodes' rows
  column->visible = true;
  for (node *i = column->up; i!=column; i=i->up){
    for (node *j = i->left; j!=i; j=j->left) {
      j->head->size++;
      j->up->down = j;
      j->down->up = j;
    }
  }
    column->left->right = column;
  column->right->left = column;
  }
 
  void backtrack(int i){
    if (root->right == root) {
      solutionsNumber++;
      return;
    }
    header * column, * choosen = root->right;
  for (column = root->right; column!=root; column=column->right) {
    if (column->size < choosen->size)
      choosen = column;
    if (choosen->size==0) return;
  }
    cover(choosen);
    for (node *row = choosen->down; row!=choosen && solutionsNumber<maxSolutionsNumber; row=row->down) {
      if (solutionsNumber==0)
solutions[i] = row->row;
      for (node *j = row->right; j!=row; j=j->right)
cover(j->head);
      backtrack(i + 1);
      for (node *j = row->left; j!=row; j=j->left)
uncover(j->head);
    }
    uncover(choosen);
   
  }
 
};


int main(){
  int empty[81] = {
    0, 0, 0, 0,  0, 0,  0, 0, 0,
    0, 0, 0, 0,  0, 0,  0, 0, 0,
    0, 0, 0, 0,  0, 0,  0, 0, 0,
    0, 0, 0, 0,  0, 0,  0, 0, 0,
   
    0, 0, 0, 0,  0, 0,  0, 0, 0,
    0, 0, 0, 0,  0, 0,  0, 0, 0,
   
    0, 0, 0,  0, 0, 0,  0, 0, 0,
    0, 0, 0,  0, 0, 0,  0, 0, 0,
    0, 0, 0,  0, 0, 0,  0, 0, 0,
  };
  int minusthree[81] = {
    0, 0, 0,  0, 0, 0,  0, 0, 0,
    0, 2, 3,  0, 0, 0,  7, 8, 0,
    1, 0, 0,  4, 0, 6,  0, 0, 9,
   
    9, 0, 0,  0, 5, 0,  0, 0, 4,
    2, 0, 0,  0, 0, 0,  0, 0, 8,
    0, 1, 0,  0, 0, 0,  0, 3, 0,
   
    0, 0, 8,  0, 0, 0,  3, 0, 0,
    0, 0, 0,  2, 0, 9,  0, 0, 0,
    0, 0, 0,  0, 1, 0,  0, 0, 0
  };
  int heart[81] = {
    8, 7, 2,  0, 0, 9,  0, 0, 0,
    0, 0, 0,  0, 3, 0,  4, 0, 0,
    0, 0, 0,  0, 0, 1,  2, 0, 8,
   
    0, 0, 9,  0, 0, 0,  0, 8, 0,
    3, 0, 7,  0, 0, 0,  6, 0, 5,
    0, 2, 0,  0, 0, 0,  1, 0, 0,
   
    1, 0, 8,  2, 0, 0,  0, 0, 0,
    0, 0, 5,  0, 9, 0,  0, 0, 0,
    0, 0, 0,  1, 0, 0,  9, 5, 3
  };
 
  clock_t begin, end;
 
  begin = clock();
  Sudoku sudoku(3);
 
  cout << "Sudoku.solver\n";
  sudoku.print(heart);
  begin = clock();
  cout << "Working.." << "\n";
  int solutions = sudoku.solver(heart, 2);
  end = clock();
  sudoku.print(heart);
  cout << solutions
       << " solutions. Solved in " << ((double)(end - begin)*1000)/CLOCKS_PER_SEC << "\n";
 
 
  cout << "\n\nSudoku.generator\n";
  int mysudoku[81];
  begin = clock();
  cout << "Working.." << "\n";
  sudoku.generator(mysudoku);
  end = clock();
  sudoku.print(mysudoku);
  cout << "Generated in " << ((double)(end - begin)*1000)/CLOCKS_PER_SEC << "\n";
 
  begin = clock();
  cout << "Working.." << "\n";
  sudoku.solver(mysudoku);
  end = clock();
  sudoku.print(mysudoku);
  cout << solutions
       << " solutions. Solved in " << ((double)(end - begin)*1000)/CLOCKS_PER_SEC << "\n";
}

Circular Buffer Generic Implementation in C#

Circular Buffer Generic Implementation in C# By Naim Hashmi


using System;

/*  Circular Buffer Generic Implementation
    *  By Naim Hashmi
    *
    *   Fast and Lightweight, Generic Circular Buffer Implementation, supports for bidirectional rotations
    *   Free to use or modify! Just keep me on the comments!
    */


/// <summary>
/// Used to store a Circular Buffer of objects with a particular size, that rotates when an item is added to the collection.
/// </summary>
class CircularBuffer<T>
{
    /// <summary>
    /// Creates a new Circular Buffer with the specified Capacity
    /// </summary>
    /// <param name="capacity">Total elements that can be stored inside the buffer before it starts discarding items</param>
    public CircularBuffer(int capacity)
    {
        plainBuffer = new T[capacity];
        _startIndex = 0;
    }

    private T[] plainBuffer;
    private int _startIndex; // Stores the start of the Circular Buffer

    /// <summary>
    /// Stores the current Capacity of this Buffer
    /// </summary>
    public int Capacity
    {
        get { return plainBuffer.Length; }
    }

    /// <summary>
    /// Returns the item that is stored on the specified Index inside the Circular Buffer
    /// </summary>
    /// <param name="index">Index of the Item to be returned</param>
    /// <returns>The object value stored on the specified index</returns>
    public T ElementAt(int index)
    {
        if ((index >= plainBuffer.Length) || (index < 0))
            throw new IndexOutOfRangeException();

        index += _startIndex;

        if (index >= plainBuffer.Length)
            index -= plainBuffer.Length;

        return plainBuffer[index];
    }

    /// <summary>
    /// Returns an array with the full content of the actual Circular Buffer
    /// </summary>
    /// <returns></returns>
    public T[] ToArray()
    {
        int i;
        T[] output = new T[plainBuffer.Length];

        for (i = 0; i < plainBuffer.Length; i++)
        {
            output[i] = ElementAt(i);
        }

        return output;
    }

    /// <summary>
    /// Inserts a new item inside the Circular Buffer and rotates the entire structure by one step forwards
    /// </summary>
    /// <param name="newItem">New item to be inserted into the Circular Buffer</param>
    public void Insert(T newItem)
    {
        if (_startIndex == 0)
            _startIndex = plainBuffer.Length - 1;
        else
            _startIndex--;

        plainBuffer[_startIndex] = newItem;
    }

    /// <summary>
    /// Inserts a new item inside the Circular Buffer and rotates the entire structure by one step backwards
    /// </summary>
    /// <param name="newItem">New item to be inserted into the Circular Buffer</param>
    public void InsertBackwards(T newItem)
    {
        plainBuffer[_startIndex] = newItem;

        if (_startIndex == plainBuffer.Length - 1)
            _startIndex = 0;
        else
            _startIndex++;
    }
}

Pages