-- Ego's version


local region = {};

function region.new(cframe, size)
	local self = setmetatable({}, {__index = region});
	
	self.CFrame = cframe;
	self.Planes = {
		{n = cframe:vectorToWorldSpace(Vector3.FromNormalId(Enum.NormalId.Top)), d = -size.y / 2};
		{n = cframe:vectorToWorldSpace(Vector3.FromNormalId(Enum.NormalId.Bottom)), d = -size.y / 2};
		{n = cframe:vectorToWorldSpace(Vector3.FromNormalId(Enum.NormalId.Left)), d = -size.x / 2};
		{n = cframe:vectorToWorldSpace(Vector3.FromNormalId(Enum.NormalId.Right)), d = -size.x / 2};
		{n = cframe:vectorToWorldSpace(Vector3.FromNormalId(Enum.NormalId.Front)), d = -size.z / 2};
		{n = cframe:vectorToWorldSpace(Vector3.FromNormalId(Enum.NormalId.Back)), d = -size.z / 2};
	};
	
	return self;
end;

function getCorners(part)
	local size, corners = part.Size / 2, {};
	for x = -1, 1, 2 do
		for y = -1, 1, 2 do
			for z = -1, 1, 2 do
				table.insert(corners, (part.CFrame * CFrame.new(size * Vector3.new(x, y, z))).p);
			end;
		end;
	end;
	return corners;
end;

function region:CastPart(part)
	local corners = getCorners(part);
	for _, corner in next, corners do
		local position = corner - self.CFrame.p;
		local isInRegion = true;
		for _, plane in next, self.Planes do
			if (position:Dot(plane.n) + plane.d) >= 0 then
				isInRegion = false;
				break;
			end;
		end;
		if isInRegion then
			return true;
		end;
	end;
	return false;
end;

return region;
