//adds many boxes together using AND - operator for the data public static BooleanBox Combine(BooleanBox[] boxes) { if (boxes.Length == 0) { return(null); } if (boxes.Length == 1) { return(boxes[0]); } Vec2I origo = boxes[0].origo; Vec2I end = boxes[0].end; for (int i = 1; i < boxes.Length; i++) { origo = Vec2I.SelectMin(origo, boxes[i].origo); end = Vec2I.SelectMax(end, boxes[i].end); } BooleanBox result = new BooleanBox(origo, end - origo + Vec2I.one); for (int i = 0; i < boxes.Length; i++) { for (int y = 0; y < result.size.y; y++) { for (int x = 0; x < result.size.x; x++) { if (boxes[i].GetValue(origo + new Vec2I(x, y))) { result.data[x, y] = true; } } } } return(result); }
//creates a new boolean box from two, returns false if boxes don't intersect, uses boolean operator to decide the resulting data public static bool Intersection(BooleanBox a, BooleanBox b, out BooleanBox result, Operation operation) { result = null; //no intersection if (a.end.AnyLowerOrEqual(b.origo) || b.origo.AnyHigherOrEqual(a.end) || b.end.AnyLowerOrEqual(a.origo) || a.origo.AnyHigherOrEqual(b.end)) { return(false); } //check if other box encapsulates other or create a cross-section if (a.Contains(b.origo) && a.Contains(b.end)) { result = new BooleanBox(b.origo, b.size); } else if (b.Contains(a.origo) && b.Contains(a.end)) { result = new BooleanBox(a.origo, a.size); } else { Vec2I start = Vec2I.SelectMax(a.origo, b.origo); Vec2I end = Vec2I.SelectMin(a.end, b.end); result = new BooleanBox(start, end - start); } //offsets Vec2I ao = result.origo - a.origo; Vec2I bo = result.origo - b.origo; //boolean method switch (operation) { default: case Operation.AllFalse: break; case Operation.AllTrue: for (int y = 0; y < result.size.y; y++) { for (int x = 0; x < result.size.x; x++) { result.data[x, y] = true; } } break; case Operation.And: for (int y = 0; y < result.size.y; y++) { for (int x = 0; x < result.size.x; x++) { result.data[x, y] = a.data[ao.x + x, ao.y + y] && b.data[bo.x + x, bo.y + y]; } } break; case Operation.Or: for (int y = 0; y < result.size.y; y++) { for (int x = 0; x < result.size.x; x++) { result.data[x, y] = a.data[ao.x + x, ao.y + y] || b.data[bo.x + x, bo.y + y]; } } break; case Operation.Xor: for (int y = 0; y < result.size.y; y++) { for (int x = 0; x < result.size.x; x++) { result.data[x, y] = a.data[ao.x + x, ao.y + y] != b.data[bo.x + x, bo.y + y]; } } break; case Operation.Nor: for (int y = 0; y < result.size.y; y++) { for (int x = 0; x < result.size.x; x++) { result.data[x, y] = !a.data[ao.x + x, ao.y + y] && !b.data[bo.x + x, bo.y + y]; } } break; case Operation.CloneA: for (int y = 0; y < result.size.y; y++) { for (int x = 0; x < result.size.x; x++) { result.data[x, y] = a.data[ao.x + x, ao.y + y]; } } break; case Operation.CloneB: for (int y = 0; y < result.size.y; y++) { for (int x = 0; x < result.size.x; x++) { result.data[x, y] = b.data[bo.x + x, bo.y + y]; } } break; } return(true); }