int TrailBlazeGapped(DmtxRegion reg, DmtxBresLine line, int streamDir) { bool onEdge; int distSq, distSqMax; int travel = 0; int outward = 0; int xDiff, yDiff; int steps; int stepDir = 0; int[] dirMap = { 0, 1, 2, 7, 8, 3, 6, 5, 4 }; bool err; DmtxPixelLoc beforeStep, afterStep; DmtxPointFlow flow, flowNext; DmtxPixelLoc loc0; int xStep, yStep; loc0 = line.Loc; flow = GetPointFlow(reg.FlowBegin.Plane, loc0, DmtxConstants.DmtxNeighborNone); distSqMax = (line.XDelta * line.XDelta) + (line.YDelta * line.YDelta); steps = 0; onEdge = true; beforeStep = loc0; int beforeCacheIndex = DecodeGetCache(loc0.X, loc0.Y); if (beforeCacheIndex == -1) return 0; else _cache[beforeCacheIndex] = 0; do { if (onEdge == true) { flowNext = FindStrongestNeighbor(flow, streamDir); if (flowNext.Mag == DmtxConstants.DmtxUndefined) break; err = (new DmtxBresLine(line)).GetStep(flowNext.Loc, ref travel, ref outward); if (flowNext.Mag < 50 || outward < 0 || (outward == 0 && travel < 0)) { onEdge = false; } else { line.Step(travel, outward); flow = flowNext; } } if (!onEdge) { line.Step(1, 0); flow = GetPointFlow(reg.FlowBegin.Plane, line.Loc, DmtxConstants.DmtxNeighborNone); if (flow.Mag > 50) onEdge = true; } afterStep = line.Loc; int afterCacheIndex = DecodeGetCache(afterStep.X, afterStep.Y); if (afterCacheIndex == -1) break; /* Determine step direction using pure magic */ xStep = afterStep.X - beforeStep.X; yStep = afterStep.Y - beforeStep.Y; if (Math.Abs(xStep) > 1 || Math.Abs(yStep) > 1) { throw new Exception("Invalid step directions!"); } stepDir = dirMap[3 * yStep + xStep + 4]; if (stepDir == 8) { throw new Exception("Invalid step direction!"); } if (streamDir < 0) { this._cache[beforeCacheIndex] |= (byte)(0x40 | stepDir); this._cache[afterCacheIndex] = (byte)(((stepDir + 4) % 8) << 3); } else { this._cache[beforeCacheIndex] |= (byte)(0x40 | (stepDir << 3)); this._cache[afterCacheIndex] = (byte)((stepDir + 4) % 8); } /* Guaranteed to have taken one step since top of loop */ xDiff = line.Loc.X - loc0.X; yDiff = line.Loc.Y - loc0.Y; distSq = (xDiff * xDiff) + (yDiff * yDiff); beforeStep = line.Loc; beforeCacheIndex = afterCacheIndex; steps++; } while (distSq < distSqMax); return steps; }