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

Method pointDistance

qcustomplot/qcustomplot.cpp:21632–21678  ·  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

21630 is \ref QCPScatterStyle::ssNone (i.e. there is no visual representation of the graph), returns -1.0.
21631*/
21632double QCPGraph::pointDistance(const QPointF &pixelPoint, QCPGraphDataContainer::const_iterator &closestData) const
21633{
21634 closestData = mDataContainer->constEnd();
21635 if (mDataContainer->isEmpty())
21636 return -1.0;
21637 if (mLineStyle == lsNone && mScatterStyle.isNone())
21638 return -1.0;
21639
21640 // calculate minimum distances to graph data points and find closestData iterator:
21641 double minDistSqr = (std::numeric_limits<double>::max)();
21642 // determine which key range comes into question, taking selection tolerance around pos into account:
21643 double posKeyMin, posKeyMax, dummy;
21644 pixelsToCoords(pixelPoint-QPointF(mParentPlot->selectionTolerance(), mParentPlot->selectionTolerance()), posKeyMin, dummy);
21645 pixelsToCoords(pixelPoint+QPointF(mParentPlot->selectionTolerance(), mParentPlot->selectionTolerance()), posKeyMax, dummy);
21646 if (posKeyMin > posKeyMax)
21647 qSwap(posKeyMin, posKeyMax);
21648 // iterate over found data points and then choose the one with the shortest distance to pos:
21649 QCPGraphDataContainer::const_iterator begin = mDataContainer->findBegin(posKeyMin, true);
21650 QCPGraphDataContainer::const_iterator end = mDataContainer->findEnd(posKeyMax, true);
21651 for (QCPGraphDataContainer::const_iterator it=begin; it!=end; ++it)
21652 {
21653 const double currentDistSqr = QCPVector2D(coordsToPixels(it->key, it->value)-pixelPoint).lengthSquared();
21654 if (currentDistSqr < minDistSqr)
21655 {
21656 minDistSqr = currentDistSqr;
21657 closestData = it;
21658 }
21659 }
21660
21661 // calculate distance to graph line if there is one (if so, will probably be smaller than distance to closest data point):
21662 if (mLineStyle != lsNone)
21663 {
21664 // line displayed, calculate distance to line segments:
21665 QVector<QPointF> lineData;
21666 getLines(&lineData, QCPDataRange(0, dataCount()));
21667 QCPVector2D p(pixelPoint);
21668 const int step = mLineStyle==lsImpulse ? 2 : 1; // impulse plot differs from other line styles in that the lineData points are only pairwise connected
21669 for (int i=0; i<lineData.size()-1; i+=step)
21670 {
21671 const double currentDistSqr = p.distanceSquaredToLine(lineData.at(i), lineData.at(i+1));
21672 if (currentDistSqr < minDistSqr)
21673 minDistSqr = currentDistSqr;
21674 }
21675 }
21676
21677 return qSqrt(minDistSqr);
21678}
21679
21680/*! \internal
21681

Callers

nothing calls this directly

Calls 8

constEndMethod · 0.80
distanceSquaredToLineMethod · 0.80
atMethod · 0.80
constBeginMethod · 0.80
isEmptyMethod · 0.45
findBeginMethod · 0.45
findEndMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected