Home | API | File List | Examples | Download

lib3ds_math.c

00001 /*
00002     Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com>
00003     All rights reserved.
00004     
00005     This program is free  software: you can redistribute it and/or modify 
00006     it under the terms of the GNU Lesser General Public License as published 
00007     by the Free Software Foundation, either version 2.1 of the License, or 
00008     (at your option) any later version.
00009 
00010     Thisprogram  is  distributed in the hope that it will be useful, 
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
00013     GNU Lesser General Public License for more details.
00014     
00015     You should  have received a copy of the GNU Lesser General Public License
00016     along with  this program; If not, see <http://www.gnu.org/licenses/>. 
00017 */
00018 #include "lib3ds_impl.h"
00019 
00020 
00021 float
00022 lib3ds_math_ease(float fp, float fc, float fn, float ease_from, float ease_to) {
00023     double s, step;
00024     double tofrom;
00025     double a;
00026 
00027     s = step = (float)(fc - fp) / (fn - fp);
00028     tofrom = ease_to + ease_from;
00029     if (tofrom != 0.0) {
00030         if (tofrom > 1.0) {
00031             ease_to = (float)(ease_to / tofrom);
00032             ease_from = (float)(ease_from / tofrom);
00033         }
00034         a = 1.0 / (2.0 - (ease_to + ease_from));
00035 
00036         if (step < ease_from) s = a / ease_from * step * step;
00037         else {
00038             if ((1.0 - ease_to) <= step) {
00039                 step = 1.0 - step;
00040                 s = 1.0 - a / ease_to * step * step;
00041             } else {
00042                 s = ((2.0 * step) - ease_from) * a;
00043             }
00044         }
00045     }
00046     return((float)s);
00047 }
00048 
00049 
00050 void
00051 lib3ds_math_cubic_interp(float *v, float *a, float *p, float *q, float *b, int n, float t) {
00052     float x, y, z, w;
00053     int i;
00054 
00055     x = 2 * t * t * t - 3 * t * t + 1;
00056     y = -2 * t * t * t + 3 * t * t;
00057     z = t * t * t - 2 * t * t + t;
00058     w = t * t * t - t * t;
00059     for (i = 0; i < n; ++i) {
00060         v[i] = x * a[i] + y * b[i] + z * p[i] + w * q[i];
00061     }
00062 }