]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/bezier.cpp
* gtk2_bitmap.cpp: fixed constructor bug
[vlc] / modules / gui / skins / src / bezier.cpp
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 $
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Emmanuel Puig    <karibu@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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,
23  * USA.
24  *****************************************************************************/
25
26 //--- SKIN ------------------------------------------------------------------
27 #include "bezier.h"
28
29
30
31 //---------------------------------------------------------------------------
32 // Inline methods : supposed to accelerate the code
33 //---------------------------------------------------------------------------
34 inline double Bezier::melange( int i, int n, double t )
35 {
36     return Power( t, i ) * Power( 1 - t, (n - i) ) * ft[n] / ft[i] / ft[n - i];
37 }
38 //---------------------------------------------------------------------------
39 inline double Bezier::bezier_pty( double t )
40 {
41     double res = 0;
42     for( int i = 0; i <= maxpt; i++ )
43     {
44         res += pty[i] * melange( i, maxpt, t );
45     }
46     return res;
47 }
48 //---------------------------------------------------------------------------
49 inline double Bezier::bezier_ptx( double t )
50 {
51     double res = 0;
52     for( int i = 0; i <= maxpt; i++ )
53     {
54         res += ptx[i] * melange( i, maxpt, t );
55     }
56     return res;
57 }
58 //---------------------------------------------------------------------------
59
60
61 //---------------------------------------------------------------------------
62 // BEZIER
63 // The bezier class generate bezier curves
64 //---------------------------------------------------------------------------
65 Bezier::Bezier( double *x, double *y, int n, int flag )
66 {
67     int i;
68
69     // x and y pointer are arrays of the coordinates of the points
70     // n is the number of points
71
72     // Allocation of ressources for arrays of points
73     ptx = new double[n];
74     pty = new double[n];
75
76     // ft is factoriels
77     // Here we create an array an precalculate them
78     ft  = new double[n];
79     ft[0] = 1;
80     for( i = 0; i < n; i++ )
81     {
82         ptx[i] = x[i];    // assign values of coordinates
83         pty[i] = y[i];
84         if( i > 0 )
85             ft[i] = i * ft[i - 1];
86     }
87     maxpt = n - 1;
88
89     // FLAG values :
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
93
94     // Initialization
95     Flag         = flag;
96     Max          = 0;                 // Init number of pixels
97     double Range = MAX_BEZIER_POINT;  // max number of pixel
98     int last_i   = 0;
99     int cx, cy, oldx, oldy;
100     Left = new int[MAX_BEZIER_POINT + 1];
101     Top  = new int[MAX_BEZIER_POINT + 1];
102
103     // Calculate first point
104     double per = 0;
105     double j;
106     GetPoint( per, oldx, oldy );
107     Left[0] = oldx;
108     Top[0]  = oldy;
109
110     // Search for number of different points
111     for( j = 1; j <= Range; j++ )
112     {
113         per = j / Range;
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 ) )
118         {
119             Max++;
120             Left[Max] = cx;
121             Top[Max]  = cy;
122             oldx = cx;
123             oldy = cy;
124
125             // Accelerator
126             if( i - last_i > 2 )
127             {
128                 i += i - last_i - 1;
129             }
130             last_i = i;
131         }
132     }
133 }
134 //---------------------------------------------------------------------------
135 Bezier::~Bezier()
136 {
137     delete[] Left;
138     delete[] Top;
139     delete[] ptx;
140     delete[] pty;
141     delete[] ft;
142 }
143 //---------------------------------------------------------------------------
144 void Bezier::GetPoint( double i, int &x, int &y )
145 {
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 );
150 }
151 //---------------------------------------------------------------------------
152 int Bezier::GetNumOfDifferentPoints()
153 {
154     return Max;
155 }
156 //---------------------------------------------------------------------------
157 void Bezier::GetDifferentPoints( int *x, int *y, int OffX, int OffY )
158 {
159     for( int i = 0; i <= Max; i++ )
160     {
161         x[i] = Left[i] + OffX;
162         y[i] = Top[i]  + OffY;
163     }
164 }
165 //---------------------------------------------------------------------------
166 double Bezier::Power( double x, int n )
167 {
168     if( n > 0 )
169         return x * Power( x, n - 1);
170     else
171         return 1;
172 }
173 //---------------------------------------------------------------------------
174