void insertRect(TY** ppY,const TRect& rect){ TY* pY=*ppY; if (rect.y1 <= pY->y0){ //之前 insertAtFront(ppY,rect); }else if (rect.y0 >= pY->y1){ //之后 insertRectNext(pY,rect); }else if (getIsCover(pY,rect)){ //包含 return; }else{ //相交 TRect nRect(rect); if (nRect.y1 > pY->y1){ //长出一截的部分交给下一个Y TRect backRect(nRect.x0,pY->y1,nRect.x1,nRect.y1); nRect.y1=pY->y1; insertRectNext(pY,backRect); }else if (nRect.y1< pY->y1){ //短一截,分裂Y clipY(pY,nRect.y1); } //nRect.y1==pY->y1 if (nRect.y0 < pY->y0){ TRect frontRect(nRect.x0,nRect.y0,nRect.x1,pY->y0); nRect.y0=pY->y0; YAddAX(pY,nRect.x0,nRect.x1); insertAtFront(ppY,frontRect); }else if (nRect.y0< pY->y0){ clipY(pY,nRect.y0); YAddAX(pY->nextY,nRect.x0,nRect.x1); }else{ //nRect.y0== pY->y0 YAddAX(pY,nRect.x0,nRect.x1); } } }
void GridRemoval::removeLine (const QPointF &posMin, const QPointF &posMax, QImage &image) { double w = image.width() - 1; // Inclusive width = exclusive width - 1 double h = image.height() - 1; // Inclusive height = exclusive height - 1 QPointF pos1 = posMin; QPointF pos2 = posMax; // Throw away all lines that are entirely above or below or left or right to the screen, since // they cannot intersect the screen bool onLeft = (pos1.x() < 0 && pos2.x () < 0); bool onTop = (pos1.y() < 0 && pos2.y () < 0); bool onRight = (pos1.x() > w && pos2.x () > w); bool onBottom = (pos1.y() > h && pos2.y () > h); if (!onLeft && !onTop && !onRight && !onBottom) { // Clip to within the four sides if (pos1.x() < 0) { pos1 = clipX (pos1, 0, pos2); } if (pos2.x() < 0) { pos2 = clipX (pos2, 0, pos1); } if (pos1.y() < 0) { pos1 = clipY (pos1, 0, pos2); } if (pos2.y() < 0) { pos2 = clipY (pos2, 0, pos1); } if (pos1.x() > w) { pos1 = clipX (pos1, w, pos2); } if (pos2.x() > w) { pos2 = clipX (pos2, w, pos1); } if (pos1.y() > h) { pos1 = clipY (pos1, h, pos2); } if (pos2.y() > h) { pos2 = clipY (pos2, h, pos1); } // Is line more horizontal or vertical? double deltaX = qAbs (pos1.x() - pos2.x()); double deltaY = qAbs (pos1.y() - pos2.y()); if (deltaX > deltaY) { // More horizontal int xMin = qMin (pos1.x(), pos2.x()); int xMax = qMax (pos1.x(), pos2.x()); int yAtXMin = (pos1.x() < pos2.x() ? pos1.y() : pos2.y()); int yAtXMax = (pos1.x() < pos2.x() ? pos2.y() : pos1.y()); for (int x = xMin; x <= xMax; x++) { double s = (double) (x - xMin) / (double) (xMax - xMin); double yLine = (1.0 - s) * yAtXMin + s * yAtXMax; for (int yOffset = -1; yOffset <= 1; yOffset++) { int y = (int) (0.5 + yLine + yOffset); image.setPixel (x, y, QColor(Qt::white).rgb()); } } } else { // More vertical int yMin = qMin (pos1.y(), pos2.y()); int yMax = qMax (pos1.y(), pos2.y()); int xAtYMin = (pos1.y() < pos2.y() ? pos1.x() : pos2.x()); int xAtYMax = (pos1.y() < pos2.y() ? pos2.x() : pos1.x()); for (int y = yMin; y <= yMax; y++) { double s = (double) (y - yMin) / (double) (yMax - yMin); double xLine = (1.0 - s) * xAtYMin + s * xAtYMax; for (int xOffset = -1; xOffset <= 1; xOffset++) { int x = (int) (0.5 + xLine + xOffset); image.setPixel (x, y, QColor(Qt::white).rgb()); } } } } }