#!/usr/local/bin/cz --
use b

Main:
	space(300,300)
	colour cols[] = { orange, red, green, yellow, blue, magenta, cyan, pink }
	int n_typs = array_size(cols)
	int n_cells, zoom
	num curve0, curve1, curve_pow
	if scanf("%d %d %lf %lf\n", &n_cells, &zoom, &curve0, &curve1) != 4:
		error("bad header")
	sf("%d %d %f %f", n_cells, zoom, curve0, curve1);
	int typs[n_cells]
	num xs[n_cells]
	num ys[n_cells]
	int i = 0
	Eachline(l):
		if i == n_cells:
			error("too many cells! expecting %d", n_cells)
		if sscanf(l, num_fmt num_fmt "%d", &xs[i], &ys[i], &typs[i]) != 3:
			error("bad line: %s", l)
		sf("%f %f %d", xs[i], ys[i], typs[i])
		xs[i] *= zoom ; ys[i] *= zoom
		++i
	if i != n_cells:
		error("not enough cells! expecting %d", n_cells)
	num large = 1e9

	if curve0 == curve1:
		num curve = curve0
		plot_cells()
	else:
		for(curve, curve0, curve1, (curve1-curve0) / 25):
			plot_cells()
			asleep(0.05)

def plot_cells()
	curve_pow = exp(-curve)
	for(x, -w_2, w_2):
		for(y, -h_2, h_2):
			num tot[n_typs]
			aryzero(tot)
			for(i, 0, n_cells):
				num dx = x-xs[i]
				num dy = y-ys[i]
				num d2 = dx*dx + dy*dy
				num d = sqrt(d2)
#				num di = 1.0/d
#				num di2 = 1.0/d2
				int typ = typs[i]
				if tot[typ] == large:
					tot[typ] = 0
				tot[typ] += pow(d, -curve_pow)
			num max_tot = 0
			int closest_typ
			for(i, 0, n_typs):
				num t = tot[i]
				if t > max_tot:
					closest_typ = i
					max_tot = t
			colour c = cols[closest_typ]
			col(c)
			point(x, y)
	for(i, 0, n_cells):
		black() ; circle_fill(xs[i], ys[i], 3)
		white() ; circle(xs[i], ys[i], 3)
	Paint()

def aryzero(ary):
	for(i, 0, array_size(ary)):
		ary[i] = 0

def num_fmt "%lf"
