aboutsummaryrefslogtreecommitdiff
path: root/pkg/xtools/icfit/icgnearest.gx
blob: d31659409142848b41df4825eae2feed4be3fa93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<mach.h>
include	<pkg/gtools.h>
include "icfit.h"

# ICG_NEAREST -- Find the nearest point to the cursor and return the index.
# The nearest point to the cursor in NDC coordinates is determined.
# The cursor is moved to the nearest point selected.

int procedure icg_nearest$t (ic, gp, gt, cv, x, y, npts, wx, wy)

pointer	ic					# ICFIT pointer
pointer	gp					# GIO pointer
pointer	gt					# GTOOLS pointer
pointer	cv					# CURFIT pointer
PIXEL	x[npts], y[npts]			# Data points
int	npts					# Number of points
real	wx, wy					# Cursor position

int	pt
pointer	sp, xout, yout

int	icg_n$t(), gt_geti()

begin
	call smark (sp)
	call salloc (xout, npts, TY_PIXEL)
	call salloc (yout, npts, TY_PIXEL)
	call icg_axes$t (ic, gt, cv, 1, x, y, Mem$t[xout], npts)
	call icg_axes$t (ic, gt, cv, 2, x, y, Mem$t[yout], npts)
	if (gt_geti (gt, GTTRANSPOSE) == NO)
	    pt = icg_n$t (gp, Mem$t[xout], Mem$t[yout], npts, wx, wy)
	else
	    pt = icg_n$t (gp, Mem$t[yout], Mem$t[xout], npts, wy, wx)
	call sfree (sp)
	
	return (pt)
end

int procedure icg_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	x0, y0, r2, r2min

begin
	# Transform world cursor coordinates to NDC.

	call gctran (gp, wx, wy, wx, wy, 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 - wx) ** 2 + (y0 - wy) ** 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]))

	return (j)
end