From fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 8 Jul 2015 20:46:52 -0400 Subject: Initial commit --- pkg/xtools/inlfit/ingnearest.gx | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 pkg/xtools/inlfit/ingnearest.gx (limited to 'pkg/xtools/inlfit/ingnearest.gx') diff --git a/pkg/xtools/inlfit/ingnearest.gx b/pkg/xtools/inlfit/ingnearest.gx new file mode 100644 index 00000000..1d208678 --- /dev/null +++ b/pkg/xtools/inlfit/ingnearest.gx @@ -0,0 +1,81 @@ +include +include + + +# ING_NEAREST -- Find the nearest point to the cursor and return the index. +# The cursor is moved to the nearest point selected. + +int procedure ing_nearest$t (in, gp, gt, nl, x, y, npts, nvars, wx, wy) + +pointer in # INLFIT pointer +pointer gp # GIO pointer +pointer gt # GTOOLS pointer +pointer nl # NLFIT pointer +PIXEL x[ARB] # Independent variables (npts * nvars) +PIXEL y[npts] # Dependent variables +int npts # Number of points +int nvars # Number of variables +real wx, wy # Cursor position + +int pt +pointer sp, xout, yout + +int ing_n$t(), gt_geti() + +begin + # Allocate memory for axes data + call smark (sp) + call salloc (xout, npts, TY_PIXEL) + call salloc (yout, npts, TY_PIXEL) + + # Set axes data + call ing_axes$t (in, gt, nl, 1, x, y, Mem$t[xout], npts, nvars) + call ing_axes$t (in, gt, nl, 2, x, y, Mem$t[yout], npts, nvars) + + # Check for transposed axes + if (gt_geti (gt, GTTRANSPOSE) == NO) + pt = ing_n$t (gp, Mem$t[xout], Mem$t[yout], npts, wx, wy) + else + pt = ing_n$t (gp, Mem$t[yout], Mem$t[xout], npts, wy, wx) + call sfree (sp) + + # Return index + return (pt) +end + + +# ING_N -- Find position and move the cursor. + +int procedure ing_n$t (gp, x, y, npts, wx, wy) + +pointer gp # GIO pointer +PIXEL x[npts], y[npts] # Data points +int npts # Number of points +real wx, wy # Cursor position + +int i, j +real xc, yc, x0, y0, r2, r2min + +begin + # Transform world cursor coordinates to NDC. + call gctran (gp, wx, wy, xc, yc, 1, 0) + + # Search for nearest point. + r2min = MAX_REAL + do i = 1, npts { + call gctran (gp, real (x[i]), real (y[i]), x0, y0, 1, 0) + r2 = (x0 - xc) ** 2 + (y0 - yc) ** 2 + if (r2 < r2min) { + r2min = r2 + j = i + } + } + + # Move the cursor to the selected point and return the index. + if (j != 0) { + call gscur (gp, real (x[j]), real (y[j])) + wx = x[j] + wy = y[j] + } + return (j) +end -- cgit