function testDistanceSearch() { $testSpine = new WMSpine(); $testSpine->addPoint(new WMPoint(50, 50)); $testSpine->addPoint(new WMPoint(150, 50)); $testSpine->addPoint(new WMPoint(150, 150)); $this->assertEquals(200, $testSpine->totalDistance()); $this->assertEquals(3, $testSpine->pointCount()); $index = $testSpine->findIndexNearDistance(110); $this->assertEquals(1, $index); $index = $testSpine->findIndexNearDistance(90); $this->assertEquals(0, $index); $testSpine->addPoint(new WMPoint(0, 150)); $testSpine->addPoint(new WMPoint(0, 0)); $this->assertEquals(500, $testSpine->totalDistance()); $index = $testSpine->findIndexNearDistance(250); $this->assertEquals(2, $index); $index = $testSpine->findIndexNearDistance(600); $this->assertEquals(4, $index); $index = $testSpine->findIndexNearDistance(100); $this->assertEquals(1, $index); $index = $testSpine->findIndexNearDistance(100); $this->assertEquals(1, $index); $index = $testSpine->findIndexNearDistance(-100); $this->assertEquals(0, $index); }
/** split - split the Spine into two new spines, with splitIndex in the first one * used by the link-drawing code to make one curve into two arrows * */ function split($splitIndex) { $spine1 = new WMSpine(); $spine2 = new WMSpine(); $endCursor = $this->pointCount() - 1; $totalDistance = $this->totalDistance(); for ($i = 0; $i < $splitIndex; $i++) { $spine1->addRawEntry($this->points[$i]); } // work backwards from the end, finishing with the same point // Recalculate the distance (element 1) from the other end as we go for ($i = $endCursor; $i > $splitIndex; $i--) { $newEntry = $this->points[$i]; $newDistance = $totalDistance - $newEntry[1]; // wm_debug(" $totalDistance => $newDistance \n"); $newEntry[1] = $newDistance; $spine2->addRawEntry($newEntry); } return array($spine1, $spine2); }