//Returns intersection interval if it exist, NULL if there is no. bool Interval::intersect(interval_t result, interval_t interval1, interval_t interval2) { double x1 = INTERVAL_X1(interval1), x2 = INTERVAL_X2(interval1), y1 = INTERVAL_X1(interval2), y2 = INTERVAL_X2(interval2); if(x1 > y2) { result->null(); return false; } if(x1 < y1) { if(x2 < y1) { result->null(); return false; } if(x2 > y2) newInterval(result, y1, y2); else newInterval(result, y1, x2); } else if(x2 < y2) // [x1,x2] in [y1,y2] newInterval(result, x1, x2); else newInterval(result, x1, y2); return true; }
//Returns union interval if intersection interval is not NULL, else it returns NULL. void Interval::unifyIfIntersect(interval_t result, interval_t interval1, interval_t interval2) { double x1 = INTERVAL_X1(interval1), x2 = INTERVAL_X2(interval1), y1 = INTERVAL_X1(interval2), y2 = INTERVAL_X2(interval2); intersect(result, interval1, interval2); if(INTERVAL_IS_NULL(result)) return; if(x1 < y1) { if(x2 > y2) newInterval(result, x1, x2); else newInterval(result, x1, y2); } else if(x2 < y2) //if x1 in [y1,y2] newInterval(result, y1, y2); else //if x1 in [y1,y2] and x2 out [y1,y2] return newInterval(result, y1, x2); }
int main(int argc, char **argv) { Solution sol; // [1,5],[6,8]], [5,6] vector<Interval> intervals; Interval newInt(1, 5); Interval newInt2(6, 8); intervals.push_back(newInt); intervals.push_back(newInt2); Interval newInterval(5, 6); sol.insert(intervals, newInterval); return 0; }
vector<Interval> merge(vector<Interval> &intervals) { vector<Interval> res; if (intervals.size() < 2) return intervals; sort(intervals.begin(), intervals.end(), cmp); int i = 0; while (i < intervals.size()) { int start = intervals[i].start, end = intervals[i].end;// inside the loop while (i < intervals.size() && end >= intervals[i].start) { end = max(end, intervals[i].end); // must inside the while loop i++; } Interval newInterval(start, end); res.push_back(newInterval); } return res; }
void KisFillIntervalMap::cropInterval(KisFillInterval *interval) { Private::IteratorRange range; range = m_d->findFirstIntersectingInterval(*interval); Private::LineIntervalMap::iterator it = range.beginIt; while (interval->isValid() && it != range.endIt) { bool needsIncrement = true; if (it->start <= interval->start && it->end >= interval->start) { int savedIntervalStart = interval->start; interval->start = it->end + 1; /** * It might happen that we need to split a backward * interval into two pieces */ if (it->end > interval->end) { KisFillInterval newInterval(interval->end + 1, it->end, it->row); range.rowMapIt->insert(newInterval.start, newInterval); } it->end = savedIntervalStart - 1; /** * It might also happen that the backward interval is * fully eaten by the forward interval. This is possible * only in case the BW-interval was generated by the * strictly adjacent FW-interval, that is (it->start == * interval->start) */ if (!it->isValid()) { it = range.rowMapIt->erase(it); needsIncrement = false; } } else if (it->start <= interval->end && it->end >= interval->end) { int savedIntervalEnd = interval->end; interval->end = it->start - 1; it->start = savedIntervalEnd + 1; /** * The BW-interval is eaten by the FW-interval. See a * comment above */ if (!it->isValid()) { it = range.rowMapIt->erase(it); needsIncrement = false; } } else if (it->start > interval->end) { break; } #ifdef ENABLE_FILL_SANITY_CHECKS else if (it->start > interval->start && it->end < interval->end) { SANITY_ASSERT_MSG(0, "FATAL: The backward interval cannot fully reside inside the forward interval"); it->invalidate(); interval->invalidate(); } SANITY_ASSERT_MSG(it == range.endIt || it->isValid(), "FATAL: The backward interval cannot become invalid during the crop action"); #endif /* ENABLE_FILL_SANITY_CHECKS */ if (needsIncrement) { it++; } } }