1 /*****************************************************************************
2 * bezier.cpp: Functions to handle Bezier curves
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: bezier.cpp,v 1.1 2003/03/18 02:21:47 ipkiss Exp $
7 * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8 * Emmanuel Puig <karibu@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111,
24 *****************************************************************************/
26 //--- SKIN ------------------------------------------------------------------
31 //---------------------------------------------------------------------------
32 // Inline methods : supposed to accelerate the code
33 //---------------------------------------------------------------------------
34 inline double Bezier::melange( int i, int n, double t )
36 return Power( t, i ) * Power( 1 - t, (n - i) ) * ft[n] / ft[i] / ft[n - i];
38 //---------------------------------------------------------------------------
39 inline double Bezier::bezier_pty( double t )
42 for( int i = 0; i <= maxpt; i++ )
44 res += pty[i] * melange( i, maxpt, t );
48 //---------------------------------------------------------------------------
49 inline double Bezier::bezier_ptx( double t )
52 for( int i = 0; i <= maxpt; i++ )
54 res += ptx[i] * melange( i, maxpt, t );
58 //---------------------------------------------------------------------------
61 //---------------------------------------------------------------------------
63 // The bezier class generate bezier curves
64 //---------------------------------------------------------------------------
65 Bezier::Bezier( double *x, double *y, int n, int flag )
69 // x and y pointer are arrays of the coordinates of the points
70 // n is the number of points
72 // Allocation of ressources for arrays of points
77 // Here we create an array an precalculate them
80 for( i = 0; i < n; i++ )
82 ptx[i] = x[i]; // assign values of coordinates
85 ft[i] = i * ft[i - 1];
90 // - BEZIER_PTS_ALL : when x and y are differents
91 // - BEZIER_PTS_Y : when only y is different
92 // - BEZIER_PTS_X : when only x is different
96 Max = 0; // Init number of pixels
97 double Range = MAX_BEZIER_POINT; // max number of pixel
99 int cx, cy, oldx, oldy;
100 Left = new int[MAX_BEZIER_POINT + 1];
101 Top = new int[MAX_BEZIER_POINT + 1];
103 // Calculate first point
106 GetPoint( per, oldx, oldy );
110 // Search for number of different points
111 for( j = 1; j <= Range; j++ )
114 GetPoint( per, cx, cy );
115 if( ( Flag == BEZIER_PTS_ALL && ( cy != oldy || cx != oldx ) ) ||
116 ( Flag == BEZIER_PTS_Y && cy != oldy ) ||
117 ( Flag == BEZIER_PTS_X && cx != oldx ) )
134 //---------------------------------------------------------------------------
143 //---------------------------------------------------------------------------
144 void Bezier::GetPoint( double i, int &x, int &y )
146 // Get the coordinates of the point at i precent of
147 // the curve (i must be between 0 and 1)
148 x = (int)(float)bezier_ptx( i );
149 y = (int)(float)bezier_pty( i );
151 //---------------------------------------------------------------------------
152 int Bezier::GetNumOfDifferentPoints()
156 //---------------------------------------------------------------------------
157 void Bezier::GetDifferentPoints( int *x, int *y, int OffX, int OffY )
159 for( int i = 0; i <= Max; i++ )
161 x[i] = Left[i] + OffX;
162 y[i] = Top[i] + OffY;
165 //---------------------------------------------------------------------------
166 double Bezier::Power( double x, int n )
169 return x * Power( x, n - 1);
173 //---------------------------------------------------------------------------