还记得上代数课求两个连续集合是否有交集的场景么?
例如:
- 连续集合A (2, 8)
- 连续集合B (6, +∞)
求连续集合A和连续集合B是否有交集
1 2 3 4 5
| _______________________ ______________|_________ | | | ------o-------------o--------o------------> 2 6 8
|
我们学习时,可以通过画数轴,划范围,看两个区间是否重合区域的图像法求解。那如何通过程序,让计算机能判断这样的结果呢?
构思
数轴其实是由无数的点组成。每个连续集合是由一个起始点(特例-∞)和一个结束点组成(特例+∞)。因此要完成这个工作,底层至少需要两个类:
Point
- 属性 value,代表数值。需要考虑特例-∞和特例+∞
- 属性 includeSelf,代表是否包含自身,其实就是实心点还是虚心点的区别
Range
- 属性 start,代表起始点
- 属性 end,代表结束点
- 功能 hasIntersection(Range $range),判断当前对象与传入的对象是否含有交集
具体实现代码
Point 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
class Point {
const POSITIVE_INFINITY = "+";
const NEGATIVE_INFINITY = "-";
public $value;
public $includeSelf = true;
private function __construct() {}
public static function builderPoint(float $value, bool $includeSelf) { $point = new Point(); $point->value = $value; $point->includeSelf = $includeSelf; return $point; }
public static function builderPositiveInfinityPoint() { $point = new Point(); $point->value = Point::POSITIVE_INFINITY; $point->includeSelf = false; return $point; }
public static function builderNegativeInfinityPoint() { $point = new Point(); $point->value = Point::NEGATIVE_INFINITY; $point->includeSelf = false; return $point; }
public function isNegativeInfinityPoint() { return $this->value === self::NEGATIVE_INFINITY; }
public function isPositiveInfinityPoint() { return $this->value === self::POSITIVE_INFINITY; } }
|
Range 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
class Range {
public $start;
public $end;
private function __construct() {}
public static function builderRange(Point $start, Point $end) { $range = new Range(); $range->start = $start; $range->end = $end;
if ($range->isValidRange()) { return $range; } else { throw new RangeInvalidException("{$range}不满足数学区间定义"); } }
public function isValidRange() { if ($this->start->isNegativeInfinityPoint() && $this->end->isNegativeInfinityPoint()) { return false; } else if ($this->start->isPositiveInfinityPoint() && $this->end->isPositiveInfinityPoint()) { return false; } else if ($this->start->isPositiveInfinityPoint()) { return false; } else if ($this->end->isNegativeInfinityPoint()) { return false; } else if (!$this->start->isNegativeInfinityPoint() && !$this->end->isPositiveInfinityPoint()) { if ($this->start->value > $this->end->value) { return false; } else if ($this->start->value == $this->end->value) { if ($this->start->includeSelf && $this->end->includeSelf) { return true; } else { return false;
} } } return true; }
function __toString() { $result = "";
if ($this->start->isNegativeInfinityPoint()) { $result .= "(-∞, "; } else If ($this->start->isPositiveInfinityPoint()) { $result .= "(+∞, "; } else { if ($this->start->includeSelf) { $result .= "[" . $this->start->value . ", "; } else { $result .= "(" . $this->start->value . ", "; } }
if ($this->end->isNegativeInfinityPoint()) { $result .= "-∞)"; } else If ($this->end->isPositiveInfinityPoint()) { $result .= "+∞)"; } else { if ($this->end->includeSelf) { $result .= $this->end->value . "]"; } else { $result .= $this->end->value . ")"; } }
return $result; }
public function hasIntersection(Range $other) { if ($this->start->isNegativeInfinityPoint() && $this->end->isPositiveInfinityPoint()) { return true; } else if ($this->start->isNegativeInfinityPoint()) { if ($other->start->isNegativeInfinityPoint()) { return true; } else if ($other->start->value == $this->end->value) { if ($other->start->includeSelf && $this->end->includeSelf) { return true; } else { return false; } } else if ($other->start->value > $this->end->value) { return false; } else { return true; } } else if ($this->end->isPositiveInfinityPoint()) { if ($other->end->isPositiveInfinityPoint()) { return true; } else if ($other->end->value == $this->start->value) { if ($other->end->includeSelf && $this->start->includeSelf) { return true; } else { return false; } } else if ($other->end->value < $this->start->value) { return false; } else { return true; } } else { if ($other->start->isNegativeInfinityPoint() && $other->end->isPositiveInfinityPoint()) { return true; } else if ($other->start->isNegativeInfinityPoint()) { if ($other->end->value == $this->start->value) { if ($other->end->includeSelf && $this->start->includeSelf) { return true; } else { return false; } } else if($other->end->value < $this->start->value) { return false; } else { return true; } } else if ($other->end->isPositiveInfinityPoint()) { if ($other->start->value == $this->end->value) { if ($other->start->includeSelf && $this->end->includeSelf) { return true; } else { return false; } } else if ($other->start->value > $this->end->value) { return false; } else { return true; } } else {
if ($other->end->value == $this->start->value) { if ($other->end->includeSelf && $this->start->includeSelf) { return true; } else { return false; } } else if ($other->end->value < $this->start->value) { return false; }
if ($other->start->value == $this->end->value) { if ($other->start->includeSelf && $this->end->includeSelf) { return true; } else { return false; } } elseif ($other->start->value > $this->end->value) { return false; }
return true; } } }
public function isContainPoint(Point $point) { if ($this->start->isNegativeInfinityPoint() && $this->end->isPositiveInfinityPoint()) { return true; } else if ($this->start->isNegativeInfinityPoint()) { if ($point->value == $this->end->value) { if ($point->includeSelf && $this->end->includeSelf) { return true; } else { return false; } } else if ($point->value > $this->end->value) { return false; } else { return true; } } else if ($this->end->isPositiveInfinityPoint()) { if ($point->value == $this->start->value) { if ($point->includeSelf && $this->start->includeSelf) { return true; } else { return false; } } else if ($point->value < $this->start->value) { return false; } else { return true; } } else { if ($point->value == $this->start->value) { if ($point->includeSelf && $this->start->includeSelf) { return true; } else { return false; } } else if ($point->value == $this->end->value) { if ($point->includeSelf && $this->end->includeSelf) { return true; } else { return false; } } else if ($point->value > $this->start->value && $point->value < $this->end->value) { return true; } else { return false; } } } }
|
RangeInvalidException 类
1 2 3 4 5 6 7 8 9
|
class RangeInvalidException extends \Exception { }
|
测试代码
测试含有重叠场景
1 2 3 4 5 6 7
| $rangeA = Range::builderRange(Point::builderPoint(2, false), Point::builderPoint(8, false)); dump("rangeA:" . $rangeA);
$rangeB = Range::builderRange(Point::builderPoint(6, false), Point::builderPositiveInfinityPoint()); dump("rangeB:" . $rangeB);
dd("rangeA hasIntersection " . ($rangeA->hasIntersection($rangeB) ? "Yes" : "No"));
|
运行结果:
1 2 3
| "rangeA:(2, 8)" "rangeB:(6, +∞)" "rangeA hasIntersection Yes"
|
测试没有重叠的场景
1 2 3 4 5 6 7
| $rangeA = Range::builderRange(Point::builderPoint(2, false), Point::builderPoint(8, false)); dump("rangeA:" . $rangeA);
$rangeB = Range::builderRange(Point::builderPoint(10, false), Point::builderPositiveInfinityPoint()); dump("rangeB:" . $rangeB);
dd("rangeA hasIntersection " . ($rangeA->hasIntersection($rangeB) ? "Yes" : "No"));
|
运行结果:
1 2 3
| "rangeA:(2, 8)" "rangeB:(10, +∞)" "rangeA hasIntersection No"
|