[BACK]Return to imath.c CVS log [TXT][DIR] Up to [jp.NetBSD.org] / othersrc / mgl / mgl2 / ext

File: [jp.NetBSD.org] / othersrc / mgl / mgl2 / ext / imath.c (download)

Revision 1.1, Sun Apr 1 08:13:49 2001 UTC (17 years, 6 months ago) by suz
Branch: MAIN
CVS Tags: HEAD



mglext --- mgl 拡張機能

このライブラリは、kaffe を MGL に移植する際、必要だった機能のうち
切り出せるものをまとめたものです

/*
 * MGLINE -- MGL simple drawer 
 * Copyright (C) 2001
 *      Koji Suzuki (suz@at.sakura.ne.jp)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY KOJI SUZUKI ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: imath.c,v 1.1 2001/04/01 08:13:49 suz Exp $
 */

#include <stdlib.h>
#include <stdio.h>
#include "imath.h"

#define ABS(a) (((a)<0) ? -(a) : (a))

imath_t imath_sin(imath_t x) {
	imath_t x2,xx;
	imath_t y;
	int i;

	x2 = ((x * x)/IMATH_SCALE);
	xx = ((x * x2)/IMATH_SCALE)/(3*2*1);
	y = x;
	for (i=3; xx; i += 4) {
		y -= xx;
		xx = (xx * x2/IMATH_SCALE)/((i+1)*(i+2));
		y += xx;
		xx = (xx * x2/IMATH_SCALE)/((i+3)*(i+4));
	}
	return y;
}

imath_t imath_cos(imath_t x) {
	imath_t x2,xx;
	imath_t y;
	int i;
	x2 = ((x * x)/IMATH_SCALE);
	xx = x2/(2*1);

	y = IMATH_SCALE;
	for (i=2; xx; i+=4) {
		y -= xx;
		xx = (xx * x2/IMATH_SCALE)/((i+1)*(i+2));
		y += xx;
		xx = (xx * x2/IMATH_SCALE)/((i+3)*(i+4));
	}
	return y;
}

imath_t imath_atan(imath_t x) {
	imath_t x2,xx;
	imath_t y;
	int i;

	y = x;
	x2 = (x * x)/IMATH_SCALE;
	xx = (x * x2)/IMATH_SCALE;

	if (x2 >= IMATH_SCALE) {
	   	return (imath_t)(IMATH_PI_SCALED/4); 
	}

	for (i=3; xx ; i+= 4) {
		y -= xx/i; 
		xx = (xx * x2)/IMATH_SCALE;
		y += xx/(i+2); 
		xx = (xx * x2)/IMATH_SCALE;
	}
	return y;
}

int imath_xy_to_len(int dx,int dy) {
	imath_t adx,ady;
	imath_t y,z;
	int dig;

	adx = (imath_t)dx * IMATH_SCALE;
	ady = (imath_t)dy * IMATH_SCALE;

	adx = ABS(adx);
	ady = ABS(ady);

	if (adx > ady) {
	   y = imath_atan(ady * IMATH_SCALE / adx);
	   z = adx / imath_cos(y);
	} else {
	   y = imath_atan(adx * IMATH_SCALE / ady);
	   y = (imath_t)(IMATH_PI_SCALED/2) - y; 
	   z = ady / imath_sin(y);
	}
	return (int)z;
}

int imath_sqrt(int x) {
   return imath_xy_to_len(x,x);
}


int imath_xy_to_dig(int dx,int dy) {
	imath_t adx,ady;
	imath_t y;
	int dig;

	adx = (imath_t)dx * IMATH_SCALE;
	ady = (imath_t)dy * IMATH_SCALE;

	adx = ABS(adx);
	ady = ABS(ady);

	if (adx > ady) {
	   y = imath_atan(ady * IMATH_SCALE / adx);
	} else {
	   y = imath_atan(adx * IMATH_SCALE / ady);
	   y = (imath_t)(IMATH_PI_SCALED/2) - y; 
	}
	if (dx < 0) {
	   y = (imath_t)(IMATH_PI_SCALED) - y; 
	}
	if (dy < 0) {
	   y = (imath_t)(IMATH_PI_SCALED*2) - y; 
	}
	dig = (int)(180 * y * IMATH_SCALE / (imath_t)(IMATH_PI_SCALED));
	return dig;
}