Update ooo320-m1
[ooovba.git] / svtools / source / dialogs / mcvmath.hxx
blobaf3ae60da6848e7f1417922b1bce8d8e984e14f5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mcvmath.hxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _MCVMATH_HXX
32 #define _MCVMATH_HXX
34 #include <tools/solar.h>
36 class FixCpx;
37 class ColWheel;
39 // No of fractal bits
40 // allowed range 0..14, must be even
41 #define FIX_POST 14
43 // scale for ...Big() -Functions
44 #if (FIX_POST>=4)
45 #define FIX_P2 4
46 #define FIX_P3 (FIX_POST-FIX_P2)
47 #else
48 #define FIX_P2 0
49 #define FIX_P3 FIX_POST
50 #endif
52 #if (FIX_POST>=1)
53 #define FIX_ADD (1<<(FIX_POST-1))
54 #else
55 #define FIX_ADD 0
56 #endif
58 #if (FIX_P2>=1)
59 #define FIX_A2 (1<<(FIX_P2-1))
60 #else
61 #define FIX_A2 0
62 #endif
64 #if (FIX_P3>=1)
65 #define FIX_A3 (1<<(FIX_P3-1))
66 #else
67 #define FIX_A3 0
68 #endif
70 // -------
71 // - Fix -
72 // -------
74 class Fix
76 private:
77 friend class FixCpx;
78 friend class ColWheel;
80 // friend Fix ImpMultBig2( const Fix& a, const Fix& b );
82 public:
83 long x;
85 public:
86 Fix() { x=0; }
87 Fix( int i ) { x=(long(i)<<FIX_POST); }
88 Fix( short l ) { x=(long(l)<<FIX_POST); }
89 Fix( USHORT l ) { x=(long(l)<<FIX_POST); }
90 Fix( long l ) { x=(l<<FIX_POST); }
91 Fix( long Z, long N ) { x=(Z<<FIX_POST)/N; }
93 void SetInternVal( long nVal ) { x=nVal; }
94 long GetInternVal() const { return x; }
96 void operator+= ( const Fix& a ) { x+=a.x; }
97 void operator-= ( const Fix& a ) { x-=a.x; }
98 void operator*= ( const Fix& a ) { x=(x*a.x+FIX_ADD)>>FIX_POST; }
99 void operator/= ( const Fix& a ) { x=(x<<FIX_POST)/a.x; }
100 friend Fix operator- ( const Fix& a );
102 void MultBig( const Fix& a )
103 { x=((((a.x+FIX_A2)>>FIX_P2)*x+FIX_A3)>>FIX_P3); }
104 void DivBig( const Fix& a )
105 { x=((x<<FIX_P3)/a.x)<<FIX_P2; }
107 friend BOOL operator> ( const Fix& a, const Fix& b ) { return a.x > b.x; }
108 friend BOOL operator< ( const Fix& a, const Fix& b ) { return a.x < b.x; }
110 operator long() const { return (x+FIX_ADD) >> FIX_POST; }
111 operator double() const { return double(x)/(1<<FIX_POST); }
113 friend Fix operator+ ( const Fix& a, const Fix& b );
114 friend Fix operator- ( const Fix& a, const Fix& b );
115 friend Fix operator* ( const Fix& a, const Fix& b );
116 friend Fix operator/ ( const Fix& a, const Fix& b );
118 friend FixCpx operator-( const FixCpx& a );
121 // ----------
122 // - FixCpx -
123 // ----------
125 class FixCpx
127 // friend FixCpx ImpMultBig2( const FixCpx& ra, const FixCpx& rb );
129 public:
130 Fix r;
131 Fix i;
133 public:
134 FixCpx() : r(), i() {}
135 FixCpx( Fix a ) : r( a ), i() {}
136 FixCpx( Fix a, Fix b ) : r( a ), i( b ) {}
138 Fix& GetReal() { return r; }
139 Fix& GetImag() { return i; }
141 void operator*= ( const FixCpx& ra );
142 void MultBig( const FixCpx& ra, const FixCpx& rb );
144 friend FixCpx operator+ ( const FixCpx& a, const FixCpx& b );
145 friend FixCpx operator- ( const FixCpx& a, const FixCpx& b );
146 friend FixCpx operator* ( const FixCpx& a, const FixCpx& b );
147 friend FixCpx operator/ ( const FixCpx& a, const FixCpx& b );
148 friend FixCpx operator- ( const FixCpx& a );
151 inline Fix operator- ( const Fix& a )
153 Fix f;
154 f.x = -a.x;
155 return f;
158 inline Fix operator+ ( const Fix& a, const Fix& b )
160 long l = a.x+b.x;
161 return *((Fix*)&l);
164 inline Fix operator- ( const Fix& a, const Fix& b )
166 long l = a.x-b.x;
167 return *((Fix*)&l);
170 inline Fix operator* ( const Fix& a, const Fix& b )
172 long l=(a.x*b.x+FIX_ADD)>>FIX_POST;
173 return *((Fix*)&l);
176 inline Fix operator/ ( const Fix& a, const Fix& b )
178 long l=(a.x<<FIX_POST)/b.x;
179 return *((Fix*)&l);
182 inline FixCpx operator- ( const FixCpx& a )
184 FixCpx fc;
186 fc.r.x = -a.r.x;
187 fc.i.x = -a.i.x;
188 return fc;
191 inline FixCpx operator+ ( const FixCpx& a, const FixCpx& b )
193 return FixCpx( a.r+b.r, a.i+b.i );
196 inline FixCpx operator- ( const FixCpx& a, const FixCpx& b )
198 return FixCpx( a.r-b.r, a.i-b.i );
201 inline void FixCpx::operator*= ( const FixCpx& ra )
203 Fix rr = ra.r*r-ra.i*i;
204 i = ra.r*i+ra.i*r;
205 r = rr;
208 inline FixCpx operator* ( const FixCpx& a, const FixCpx& b )
210 return FixCpx( a.r*b.r-a.i*b.i, a.r*b.i+a.i*b.r );
213 inline FixCpx operator/ ( const FixCpx& a, const FixCpx& b )
215 return FixCpx( (a.r*b.r+a.i*b.i)/(b.r*b.r+b.i*b.i),
216 (b.r*a.r-a.r*b.i)/(b.r*b.r+b.i*b.i) );
219 // -----------------------------------------------------------------------
221 Fix ImpMultBig2( const Fix& a, const Fix& b );
222 FixCpx ImpMultBig2( const FixCpx& ra, const FixCpx& rb );
224 void ImpCartToPolar( const short x, const short y, Fix& rRad, USHORT& rPhi );
225 void ImpPolarToCart( const Fix& rR, const USHORT Phi, short& rX, short& rY );
227 USHORT ImpSqrt( ULONG nRadi );
228 USHORT ImpATan2( const short x, const short y );
229 FixCpx ImpExPI( USHORT nPhi );
231 #endif // _MCVMATH_HXX