MCPcopy Create free account
hub / github.com/LUX-Core/lux / pointDistance

Method pointDistance

src/qt/qcustomplot.cpp:21577–21623  ·  view source on GitHub ↗

! \internal Calculates the minimum distance in pixels the graph's representation has from the given \a pixelPoint. This is used to determine whether the graph was clicked or not, e.g. in \ref selectTest. The closest data point to \a pixelPoint is returned in \a closestData. Note that if the graph has a line representation, the returned distance may be smaller than the distance to the

Source from the content-addressed store, hash-verified

21575 is \ref QCPScatterStyle::ssNone (i.e. there is no visual representation of the graph), returns -1.0.
21576*/
21577double QCPGraph::pointDistance(const QPointF &pixelPoint, QCPGraphDataContainer::const_iterator &closestData) const
21578{
21579 closestData = mDataContainer->constEnd();
21580 if (mDataContainer->isEmpty())
21581 return -1.0;
21582 if (mLineStyle == lsNone && mScatterStyle.isNone())
21583 return -1.0;
21584
21585 // calculate minimum distances to graph data points and find closestData iterator:
21586 double minDistSqr = std::numeric_limits<double>::max();
21587 // determine which key range comes into question, taking selection tolerance around pos into account:
21588 double posKeyMin, posKeyMax, dummy;
21589 pixelsToCoords(pixelPoint-QPointF(mParentPlot->selectionTolerance(), mParentPlot->selectionTolerance()), posKeyMin, dummy);
21590 pixelsToCoords(pixelPoint+QPointF(mParentPlot->selectionTolerance(), mParentPlot->selectionTolerance()), posKeyMax, dummy);
21591 if (posKeyMin > posKeyMax)
21592 qSwap(posKeyMin, posKeyMax);
21593 // iterate over found data points and then choose the one with the shortest distance to pos:
21594 QCPGraphDataContainer::const_iterator begin = mDataContainer->findBegin(posKeyMin, true);
21595 QCPGraphDataContainer::const_iterator end = mDataContainer->findEnd(posKeyMax, true);
21596 for (QCPGraphDataContainer::const_iterator it=begin; it!=end; ++it)
21597 {
21598 const double currentDistSqr = QCPVector2D(coordsToPixels(it->key, it->value)-pixelPoint).lengthSquared();
21599 if (currentDistSqr < minDistSqr)
21600 {
21601 minDistSqr = currentDistSqr;
21602 closestData = it;
21603 }
21604 }
21605
21606 // calculate distance to graph line if there is one (if so, will probably be smaller than distance to closest data point):
21607 if (mLineStyle != lsNone)
21608 {
21609 // line displayed, calculate distance to line segments:
21610 QVector<QPointF> lineData;
21611 getLines(&lineData, QCPDataRange(0, dataCount()));
21612 QCPVector2D p(pixelPoint);
21613 const int step = mLineStyle==lsImpulse ? 2 : 1; // impulse plot differs from other line styles in that the lineData points are only pairwise connected
21614 for (int i=0; i<lineData.size()-1; i+=step)
21615 {
21616 const double currentDistSqr = p.distanceSquaredToLine(lineData.at(i), lineData.at(i+1));
21617 if (currentDistSqr < minDistSqr)
21618 minDistSqr = currentDistSqr;
21619 }
21620 }
21621
21622 return qSqrt(minDistSqr);
21623}
21624
21625/*! \internal
21626

Callers

nothing calls this directly

Calls 10

maxFunction · 0.85
constEndMethod · 0.80
distanceSquaredToLineMethod · 0.80
constBeginMethod · 0.80
isEmptyMethod · 0.45
findBeginMethod · 0.45
findEndMethod · 0.45
sizeMethod · 0.45
atMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected