20#include "KoPointArray.h"
23#include <KoZoomHandler.h>
25void KoPointArray::translate(
double dx,
double dy )
36void KoPointArray::point( uint index,
double *x,
double *y )
const
38 KoPoint p = TQMemArray<KoPoint>::at( index );
45KoPoint KoPointArray::point( uint index )
const
47 return TQMemArray<KoPoint>::at( index );
50void KoPointArray::setPoint( uint index,
double x,
double y )
52 TQMemArray<KoPoint>::at( index ) = KoPoint( x, y );
57bool KoPointArray::putPoints(
int index,
int nPoints,
double firstx,
double firsty,
61 if ( index + nPoints > (
int)size() ) {
62 if ( !resize(index + nPoints) )
67 setPoint( index, firstx, firsty );
71 va_start( ap, firsty );
73 x = va_arg( ap,
double );
74 y = va_arg( ap,
double );
75 setPoint( i++, x, y );
81void split(
const double *p,
double *l,
double *r)
91 l[2] = (p[0]+ p[2])/2;
92 l[3] = (p[1]+ p[3])/2;
93 tmpx = (p[2]+ p[4])/2;
94 tmpy = (p[3]+ p[5])/2;
95 r[4] = (p[4]+ p[6])/2;
96 r[5] = (p[5]+ p[7])/2;
98 l[4] = (l[2]+ tmpx)/2;
99 l[5] = (l[3]+ tmpy)/2;
100 r[2] = (tmpx + r[4])/2;
101 r[3] = (tmpy + r[5])/2;
103 l[6] = (l[4]+ r[2])/2;
104 l[7] = (l[5]+ r[3])/2;
115int pnt_on_line(
const int* p,
const int* q,
const int* t )
150 if(q[0]==p[0] && q[1]==p[1] && q[0]==t[0] && q[1]==t[1]) {
154 if ( TQABS((q[1]-p[1])*(t[0]-p[0])-(t[1]-p[1])*(q[0]-p[0])) >=
155 (TQMAX(TQABS(q[0]-p[0]), TQABS(q[1]-p[1]))))
return 0;
157 if (((q[0]<p[0])&&(p[0]<t[0])) || ((q[1]<p[1])&&(p[1]<t[1])))
159 if (((t[0]<p[0])&&(p[0]<q[0])) || ((t[1]<p[1])&&(p[1]<q[1])))
161 if (((p[0]<q[0])&&(q[0]<t[0])) || ((p[1]<q[1])&&(q[1]<t[1])))
163 if (((t[0]<q[0])&&(q[0]<p[0])) || ((t[1]<q[1])&&(q[1]<p[1])))
170void polygonizeTQBezier(
double* acc,
int& accsize,
const double ctrl[],
173 if ( accsize > maxsize / 2 )
177 if ( accsize >= maxsize-4 )
180 acc[accsize++] = ctrl[0];
181 acc[accsize++] = ctrl[1];
182 acc[accsize++] = ctrl[6];
183 acc[accsize++] = ctrl[7];
193 int c0[2]; c0[0] = int(ctrl[0]); c0[1] = int(ctrl[1]);
194 int c1[2]; c1[0] = int(ctrl[2]); c1[1] = int(ctrl[3]);
195 int c2[2]; c2[0] = int(ctrl[4]); c2[1] = int(ctrl[5]);
196 int c3[2]; c3[0] = int(ctrl[6]); c3[1] = int(ctrl[7]);
199 if ( TQABS(c1[0]-c0[0]) <= 1 && TQABS(c1[1]-c0[1]) <= 1
200 && TQABS(c2[0]-c0[0]) <= 1 && TQABS(c2[1]-c0[1]) <= 1
201 && TQABS(c3[0]-c1[0]) <= 1 && TQABS(c3[1]-c0[1]) <= 1 )
206 acc[accsize++] = l[0];
207 acc[accsize++] = l[1];
211 if ( ( pnt_on_line( c0, c3, c1 ) == 2 && pnt_on_line( c0, c3, c2 ) == 2 )
212 || ( TQABS(c1[0]-c0[0]) <= 1 && TQABS(c1[1]-c0[1]) <= 1
213 && TQABS(c2[0]-c0[0]) <= 1 && TQABS(c2[1]-c0[1]) <= 1
214 && TQABS(c3[0]-c1[0]) <= 1 && TQABS(c3[1]-c0[1]) <= 1 ) )
219 acc[accsize++] = l[0];
220 acc[accsize++] = l[1];
225 polygonizeTQBezier( acc, accsize, l, maxsize );
226 polygonizeTQBezier( acc, accsize, r, maxsize );
230KoRect KoPointArray::boundingRect()
const
233 return KoRect( 0, 0, 0, 0 );
234 KoPoint *pd = data();
235 double minx, maxx, miny, maxy;
236 minx = maxx = pd->x();
237 miny = maxy = pd->y();
239 for (
int i=1; i<(int)size(); i++ ) {
240 if ( pd->x() < minx )
242 else if ( pd->x() > maxx )
244 if ( pd->y() < miny )
246 else if ( pd->y() > maxy )
250 return KoRect( KoPoint(minx,miny), KoPoint(maxx,maxy) );
254KoPointArray KoPointArray::cubicBezier()
const
257#if defined(TQT_CHECK_RANGE)
258 tqWarning(
"TQPointArray::bezier: The array must have 4 control points" );
263 KoRect r = boundingRect();
264 int m = (int)(4+2*TQMAX(r.width(),r.height()));
265 double *p =
new double[m];
268 for (i=0; i<4; i++) {
269 ctrl[i*2] = at(i).x();
270 ctrl[i*2+1] = at(i).y();
273 polygonizeTQBezier( p, len, ctrl, m );
274 KoPointArray pa((len/2)+1);
276 for (i=0; j<len; i++) {
277 double x = tqRound(p[j++]);
278 double y = tqRound(p[j++]);
279 pa[i] = KoPoint(x,y);
282 pa[(int)pa.size()-1] = at(3);
289TQPointArray KoPointArray::zoomPointArray(
const KoZoomHandler* zoomHandler )
const
291 TQPointArray tmpPoints(size());
292 for ( uint i= 0; i<size();i++ ) {
294 tmpPoints.putPoints( i, 1, zoomHandler->zoomItX(p.x()),zoomHandler->zoomItY(p.y()) );
299TQPointArray KoPointArray::zoomPointArray(
const KoZoomHandler* zoomHandler,
int penWidth )
const
303 KoSize ext = boundingRect().size();
304 int pw = zoomHandler->zoomItX( penWidth ) / 2;
306 fx = (double)( zoomHandler->zoomItX(ext.width()) - 2 * pw ) / ext.width();
307 fy = (double)( zoomHandler->zoomItY(ext.height()) - 2 * pw ) / ext.height();
309 unsigned int index = 0;
310 TQPointArray tmpPoints;
311 KoPointArray::ConstIterator it;
312 for ( it = begin(); it != end(); ++it, ++index ) {
313 int tmpX = tqRound((*it).x() * fx + pw);
314 int tmpY = tqRound((*it).y() * fy + pw);
316 tmpPoints.putPoints( index, 1, tmpX, tmpY );