! \internal Returns the section of the line defined by \a start and \a end, that is visible in the specified \a rect. This is a helper function for \ref draw. */
| 28367 | This is a helper function for \ref draw. |
| 28368 | */ |
| 28369 | QLineF QCPItemLine::getRectClippedLine(const QCPVector2D &start, const QCPVector2D &end, const QRect &rect) const |
| 28370 | { |
| 28371 | bool containsStart = rect.contains(start.x(), start.y()); |
| 28372 | bool containsEnd = rect.contains(end.x(), end.y()); |
| 28373 | if (containsStart && containsEnd) |
| 28374 | return QLineF(start.toPointF(), end.toPointF()); |
| 28375 | |
| 28376 | QCPVector2D base = start; |
| 28377 | QCPVector2D vec = end-start; |
| 28378 | double bx, by; |
| 28379 | double gamma, mu; |
| 28380 | QLineF result; |
| 28381 | QList<QCPVector2D> pointVectors; |
| 28382 | |
| 28383 | if (!qFuzzyIsNull(vec.y())) // line is not horizontal |
| 28384 | { |
| 28385 | // check top of rect: |
| 28386 | bx = rect.left(); |
| 28387 | by = rect.top(); |
| 28388 | mu = (by-base.y())/vec.y(); |
| 28389 | if (mu >= 0 && mu <= 1) |
| 28390 | { |
| 28391 | gamma = base.x()-bx + mu*vec.x(); |
| 28392 | if (gamma >= 0 && gamma <= rect.width()) |
| 28393 | pointVectors.append(QCPVector2D(bx+gamma, by)); |
| 28394 | } |
| 28395 | // check bottom of rect: |
| 28396 | bx = rect.left(); |
| 28397 | by = rect.bottom(); |
| 28398 | mu = (by-base.y())/vec.y(); |
| 28399 | if (mu >= 0 && mu <= 1) |
| 28400 | { |
| 28401 | gamma = base.x()-bx + mu*vec.x(); |
| 28402 | if (gamma >= 0 && gamma <= rect.width()) |
| 28403 | pointVectors.append(QCPVector2D(bx+gamma, by)); |
| 28404 | } |
| 28405 | } |
| 28406 | if (!qFuzzyIsNull(vec.x())) // line is not vertical |
| 28407 | { |
| 28408 | // check left of rect: |
| 28409 | bx = rect.left(); |
| 28410 | by = rect.top(); |
| 28411 | mu = (bx-base.x())/vec.x(); |
| 28412 | if (mu >= 0 && mu <= 1) |
| 28413 | { |
| 28414 | gamma = base.y()-by + mu*vec.y(); |
| 28415 | if (gamma >= 0 && gamma <= rect.height()) |
| 28416 | pointVectors.append(QCPVector2D(bx, by+gamma)); |
| 28417 | } |
| 28418 | // check right of rect: |
| 28419 | bx = rect.right(); |
| 28420 | by = rect.top(); |
| 28421 | mu = (bx-base.x())/vec.x(); |
| 28422 | if (mu >= 0 && mu <= 1) |
| 28423 | { |
| 28424 | gamma = base.y()-by + mu*vec.y(); |
| 28425 | if (gamma >= 0 && gamma <= rect.height()) |
| 28426 | pointVectors.append(QCPVector2D(bx, by+gamma)); |