MCPcopy Create free account
hub / github.com/CieNTi/serial_port_plotter / getRectClippedStraightLine

Method getRectClippedStraightLine

qcustomplot/qcustomplot.cpp:28152–28229  ·  view source on GitHub ↗

! \internal Returns the section of the straight line defined by \a base and direction vector \a vec, that is visible in the specified \a rect. This is a helper function for \ref draw. */

Source from the content-addressed store, hash-verified

28150 This is a helper function for \ref draw.
28151*/
28152QLineF QCPItemStraightLine::getRectClippedStraightLine(const QCPVector2D &base, const QCPVector2D &vec, const QRect &rect) const
28153{
28154 double bx, by;
28155 double gamma;
28156 QLineF result;
28157 if (vec.x() == 0 && vec.y() == 0)
28158 return result;
28159 if (qFuzzyIsNull(vec.x())) // line is vertical
28160 {
28161 // check top of rect:
28162 bx = rect.left();
28163 by = rect.top();
28164 gamma = base.x()-bx + (by-base.y())*vec.x()/vec.y();
28165 if (gamma >= 0 && gamma <= rect.width())
28166 result.setLine(bx+gamma, rect.top(), bx+gamma, rect.bottom()); // no need to check bottom because we know line is vertical
28167 } else if (qFuzzyIsNull(vec.y())) // line is horizontal
28168 {
28169 // check left of rect:
28170 bx = rect.left();
28171 by = rect.top();
28172 gamma = base.y()-by + (bx-base.x())*vec.y()/vec.x();
28173 if (gamma >= 0 && gamma <= rect.height())
28174 result.setLine(rect.left(), by+gamma, rect.right(), by+gamma); // no need to check right because we know line is horizontal
28175 } else // line is skewed
28176 {
28177 QList<QCPVector2D> pointVectors;
28178 // check top of rect:
28179 bx = rect.left();
28180 by = rect.top();
28181 gamma = base.x()-bx + (by-base.y())*vec.x()/vec.y();
28182 if (gamma >= 0 && gamma <= rect.width())
28183 pointVectors.append(QCPVector2D(bx+gamma, by));
28184 // check bottom of rect:
28185 bx = rect.left();
28186 by = rect.bottom();
28187 gamma = base.x()-bx + (by-base.y())*vec.x()/vec.y();
28188 if (gamma >= 0 && gamma <= rect.width())
28189 pointVectors.append(QCPVector2D(bx+gamma, by));
28190 // check left of rect:
28191 bx = rect.left();
28192 by = rect.top();
28193 gamma = base.y()-by + (bx-base.x())*vec.y()/vec.x();
28194 if (gamma >= 0 && gamma <= rect.height())
28195 pointVectors.append(QCPVector2D(bx, by+gamma));
28196 // check right of rect:
28197 bx = rect.right();
28198 by = rect.top();
28199 gamma = base.y()-by + (bx-base.x())*vec.y()/vec.x();
28200 if (gamma >= 0 && gamma <= rect.height())
28201 pointVectors.append(QCPVector2D(bx, by+gamma));
28202
28203 // evaluate points:
28204 if (pointVectors.size() == 2)
28205 {
28206 result.setPoints(pointVectors.at(0).toPointF(), pointVectors.at(1).toPointF());
28207 } else if (pointVectors.size() > 2)
28208 {
28209 // line probably goes through corner of rect, and we got two points there. single out the point pair with greatest distance:

Callers

nothing calls this directly

Calls 3

appendMethod · 0.80
atMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected