MULDIV(3L)

NAME
muldiv - calculate (a*b)/c accurately

SYNOPSIS
#include <dmd.h>
short muldiv (a, b, c)
int a, b, c;

DESCRIPTION
The muldiv function is a macro that returns the 16-bit result (a*b)/c. (a*b) is calculated to 32 bits to minimize the precision lost. The muldiv function is convenient for calculating transformations.

EXAMPLE
The following subroutine implements the transform(3R) function. It converts a point from window coordinates to screen coordinates:

#include <dmd.h>
Point
transform(p)
Point p;
{
	Point Do, Dc, ret;
	Do = Drect.origin;
	Dc = Drect.corner;
	ret.x = muldiv(p.x, Dc.x-Do.x, XMAX) + Do.x;
	ret.y = muldiv(p.y, Dc.y-Do.y, YMAX) + Do.y;
	return(ret);
}

The following subroutine does the opposite of the transform(3R) function. It converts a point from screen coordinates to window coordinates.
#include <dmd.h>
Point
untransform(p)
Point p;
{
	Point Do, Dc, ret;
	Do = Drect.origin;
	Dc = Drect.corner;
	ret.x = muldiv(p.x-Do.x, XMAX, Dc.x-Do.x);
	ret.y = muldiv(p.y-Do.y, YMAX, Dc.y-Do.y);
	return(ret);
}

SEE ALSO
globals(3R), ptarith(3R), transform(3R/3L).