I have a graph program where the X and Y values are continuously updated on the screen when the cursor moves over the graph. I use two different ways to get the X and Y value, by getting the mouse cursor position (when moving) and by getting the value of the graph cursor position (when clicking).
I've noticed that there's about 0.2% difference in value when using the graph cursor (in free form mode) and when using the mouse cursor. Not that 0.2% is a lot, but it looks a bit strange when your value changes when you click on the same point your mouse was hovering over.
IMO the graph cursor is (should be) more precise than the pixel method, so I would like to make the calculation exactly equal to the graph cursor method. The question remains: what is that method???
The different methods are described below.
case EVENT_LEFT_CLICK:
GetGraphCursor (panel, control, 1, &XValue, &YValue);
SetCtrlVal(panel, GRAPHPANEL_CURSOR1X, XValue);
SetCtrlVal(panel, GRAPHPANEL_CURSOR1Y, YValue);
case EVENT_MOUSE_MOVE:
GetCtrlAttribute (GraphPanel, GRAPHPANEL_GRAPH, ATTR_TOP, &GraphArea.top);
GetCtrlAttribute (GraphPanel, GRAPHPANEL_GRAPH, ATTR_PLOT_AREA_TOP, &PlotArea.top);
PlotArea.top += GraphArea.top;
GetCtrlAttribute (GraphPanel, GRAPHPANEL_GRAPH, ATTR_LEFT, &GraphArea.left);
GetCtrlAttribute (GraphPanel, GRAPHPANEL_GRAPH, ATTR_PLOT_AREA_LEFT, &PlotArea.left);
PlotArea.left += GraphArea.left;
GetCtrlAttribute (GraphPanel, GRAPHPANEL_GRAPH, ATTR_PLOT_AREA_HEIGHT, &PlotArea.height);
GetCtrlAttribute (GraphPanel, GRAPHPANEL_GRAPH, ATTR_PLOT_AREA_WIDTH, &PlotArea.width);
if (!RectContainsPoint (PlotArea, MakePoint (eventData2, eventData1))) break;
GetAxisScalingMode (panel, control, VAL_XAXIS, 0, &XAxisMin, &XAxisMax);
GetAxisScalingMode (panel, control, VAL_LEFT_YAXIS, 0, &YAxisMin, &YAxisMax);
eventData1 -= PlotArea.top;
eventData2 -= PlotArea.left;
XValue = XAxisMin + (XAxisMax - XAxisMin) / (double)PlotArea.width * (double)eventData2;
YValue = YAxisMin + (YAxisMax - YAxisMin) / (double)PlotArea.height * (double)(PlotArea.height - eventData1);
SetCtrlVal(panel, GRAPHPANEL_CURSOR1X, XValue);
SetCtrlVal(panel, GRAPHPANEL_CURSOR1Y, YValue);