home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
REND.LZH
/
READER
/
VECTOR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-22
|
3KB
|
160 lines
/*
ベクトル演算関数群
Copyright T.Kobayashi
*/
#ifndef INLINE_VECTOR
#include <stdio.h>
#include <math.h>
#include "reader.h"
#define static
#endif
/* ベクトルのコピー */
static inline void v_copy( a, b )
Vector a, b ;
{
int i ;
for( i = 0 ; i < 3 ; ++i )
a[i] = b[i] ;
}
/* ベクトルへの代入 */
static inline void ftov( Vector v, Float x, Float y, Float z )
{
v[0] = x ;
v[1] = y ;
v[2] = z ;
}
/* ベクトルの長さを求める */
static inline Float v_length( a )
Vector a ;
{
extern double sqrt(double);
return( sqrt( a[0]*a[0] + a[1]*a[1] + a[2]*a[2] ) );
}
/* 単位ベクトル化 */
static inline void v_unit( ret, a )
Vector ret, a ;
{
Float r ;
r = v_length( a );
if ( r == 0.0 )
{
ftov( ret, 0.0, 0.0, 0.0 );
return ;
}
ret[0] = a[0] / r ;
ret[1] = a[1] / r ;
ret[2] = a[2] / r ;
}
/* スカラー積 */
static inline Float s_pro( a1, a2 )
Vector a1, a2 ;
{
return( a1[0]*a2[0] + a1[1]*a2[1] + a1[2]*a2[2] );
}
/* ベクトル積 */
static inline void v_pro( ret, a1, a2 )
Vector ret, a1, a2 ;
{
ret[0] = a1[1] * a2[2] - a1[2] * a2[1] ;
ret[1] = a1[2] * a2[0] - a1[0] * a2[2] ;
ret[2] = a1[0] * a2[1] - a1[1] * a2[0] ;
}
/* 各成分ごとの積 */
static inline void v_mult( ret, a1, a2 )
Vector ret, a1, a2 ;
{
ret[0] = a1[0] * a2[0] ;
ret[1] = a1[1] * a2[1] ;
ret[2] = a1[2] * a2[2] ;
}
/* ベクトルの和 */
static inline void v_add( ret, a1, a2 )
Vector ret, a1, a2 ;
{
ret[0] = a1[0] + a2[0] ;
ret[1] = a1[1] + a2[1] ;
ret[2] = a1[2] + a2[2] ;
}
/* ベクトルの差 */
static inline void v_sub( ret, a1, a2 )
Vector ret, a1, a2 ;
{
ret[0] = a1[0] - a2[0] ;
ret[1] = a1[1] - a2[1] ;
ret[2] = a1[2] - a2[2] ;
}
/* ベクトルのスカラー倍 */
static inline void v_s_mult( Vector ret, Vector v, Float s )
{
ret[0] = v[0] * s ;
ret[1] = v[1] * s ;
ret[2] = v[2] * s ;
}
/*
* カラー
*/
/* コピー */
static inline void c_copy( c1, c2 )
Color c1, c2 ;
{
c1[0] = c2[0] ;
c1[1] = c2[1] ;
c1[2] = c2[2] ;
}
/* ベクトルからカラーへ変換 */
static inline void vtoc( c, v )
Color c ;
Vector v ;
{
c[0] = (long)( v[0] * (Float)COLOR_POINT );
c[1] = (long)( v[1] * (Float)COLOR_POINT );
c[2] = (long)( v[2] * (Float)COLOR_POINT );
}
/* 成分ごとの積 */
static inline void c_mult( ret, a1, a2 )
Color ret, a1, a2 ;
{
ret[0] = a1[0] * a2[0] / COLOR_POINT ;
ret[1] = a1[1] * a2[1] / COLOR_POINT ;
ret[2] = a1[2] * a2[2] / COLOR_POINT ;
}
/* 成分ごとの和 */
static inline void c_add( ret, a1, a2 )
Color ret, a1, a2 ;
{
ret[0] = a1[0] + a2[0] ;
ret[1] = a1[1] + a2[1] ;
ret[2] = a1[2] + a2[2] ;
}
/* 定数倍 */
static inline void c_s_mult( ret, v, s )
Color ret, v ;
long s ;
{
ret[0] = v[0] * s / COLOR_POINT ;
ret[1] = v[1] * s / COLOR_POINT ;
ret[2] = v[2] * s / COLOR_POINT ;
}