XXIIVV

Point2d toolkit in ANSI C.

typedef struct {
	int x, y;
} Point2d;

Point2d *
setpt2d(Point2d *p, int x, int y)
{
	p->x = x;
	p->y = y;
	return p;
}

Point2d
Pt2d(int x, int y)
{
	Point2d p;
	setpt2d(&p, x, y);
	return p;
}

Point2d
mid2d(Point2d a, Point2d b, int seg, int segs)
{
	return Pt2d(
		a.x + ((b.x - a.x) / (double)segs) * seg,
		a.y + ((b.y - a.y) / (double)segs) * seg);
}

Point2d *
mag2d(Point2d *a, int step)
{
	return setpt2d(a,
		abs(a->x / step) * step,
		abs(a->y / step) * step);
}

int
equ2d(Point2d *a, Point2d *b)
{
	return a->x == b->x && a->y == b->y;
}

int
distance2d(Point2d p0, Point2d p1)
{
	double a = p0.x - p1.x, b = p0.x - p1.x;
	return sqrt(a * a + b * b);
}

int
collinear2d(Point2d a, Point2d b, Point2d c)
{
	return (b.y - a.y) * (c.x - b.x) - (c.y - b.y) * (b.x - a.x);
}
— Submit an edit to point2d.c.txt(54 lines)