Pattern AI - for C#
by Sam Fereday
JavaScript
var pathRow = [];
var pathCol = [];
var kMaxPathLength = 0;
// Page 32
function InitializePatternArrays()
{
var i = 0;
for(i; i < kMaxPathLength; i++)
{
pathRow[i] = -1;
pathCol[i] = -1;
}
}
// Page 32
//// x1, y1, x2, y2
function BuildPathSegment(x1, y1, x2, y2)
{
var nextCol = x1;
var nextRow = y1;
var endCol = x2;
var endRow = y2;
var i = 0;
var deltaCol = endCol - x1;
var deltaRow = endRow - y1;
var stepCol = 0;
var stepRow = 0;
var currentStep = 0;
var fraction = 0;
for(i; i < kMaxPathLength; i++)
{
if( (pathRow[i] === -1) && (pathCol[i === -1]) ){
currentStep = i;
break;
}
if (deltaRow < 0) {
stepRow = -1;
} else {
stepRow = 1;
}
if(deltaCol < 0) {
stepCol = -1;
} else {
stepCol = 1;
}
deltaRow = Math.abs(deltaRow * 2);
deltaCol = Math.abs(deltaCol * 2);
pathRow[currentStep] = nextRow;
pathCol[currentStep] = nextCol;
currentStep += 1;
if(currentStep >= kMaxPathLength)
return;
if(deltaCol > deltaRow)
{
fraction = deltaRow * 2 - deltaCol;
while(nextCol != endCol)
{
if(fraction >= 0)
{
nextRow += stepRow;
fraction = fraction - deltaCol;
nextCol = nextCol + stepCol;
fraction = fraction + deltaRow;
pathRow[currentStep] = nextRow;
pathCol[currentStep] = nextCol;
currentStep += 1;
if(currentStep >= kMaxPathLength)
return;
}
}
} else {
fraction = deltaCol * 2 - deltaRow;
while(nextRow != endRow)
{
if(fraction >= 0) {
nextCol = nextCol + stepCol;
fraction = fraction - deltaRow;
}
nextRow = nextRow + stepRow;
fraction = fraction + deltaCol;
pathRow[currentStep] = nextRow;
pathCol[currentStep] = nextCol;
currentStep += 1;
...