view normalize.c @ 16:5ac8c7980aaf

Normalize vectors
author Josef "Jeff" Sipek <jeffpc@optonline.net>
date Fri, 02 Dec 2005 23:48:40 -0500
parents
children cc1adef5674e
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void normalize(float pt[3])
{
	float mag = sqrt(pt[0]*pt[0] + pt[1]*pt[1] + pt[2]*pt[2]);
	if (mag == 0) {
		printf("0 0 0\n");
		return;
	}
	printf("%f %f %f\n", pt[0] / mag, pt[1] / mag, pt[2] / mag);
}

int main(int argc, char **argv) 
{
	float pt[3];
	
	while(scanf("%*f %*f %*f %f %f %f", &pt[0], &pt[1], &pt[2])==3)
		normalize(pt);
	
	return 0;
}