1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
|
/****************************************************************************
** $Id: qpointarray.cpp,v 2.27.2.1 1999/09/01 10:49:34 aavit Exp $
**
** Implementation of QPointArray class
**
** Created : 940213
**
** Copyright (C) 1992-1999 Troll Tech AS. All rights reserved.
**
** This file is part of the Qt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Troll Tech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** Licensees holding valid Qt Professional Edition licenses may use this
** file in accordance with the Qt Professional Edition License Agreement
** provided with the Qt Professional Edition.
**
** See http://www.troll.no/pricing.html or email sales@troll.no for
** information about the Professional Edition licensing, or see
** http://www.troll.no/qpl/ for QPL licensing information.
**
*****************************************************************************/
#include "qpointarray.h"
#include "qrect.h"
#include "qbitarray.h"
#include "qdatastream.h"
#include "qwmatrix.h"
#include <stdarg.h>
const double Q_PI = 3.14159265358979323846; // pi
/*!
\class QPointArray qpointarray.h
\brief The QPointArray class provides an array of points.
\ingroup drawing
\inherit QArray
The QPointArray is an array of QPoint objects. In addition to the functions
provided by QArray, QPointArray provides some handy methods:
For convenient reading and writing of the point data: setPoints(),
putPoints(), point(), and setPoint().
For geometry operations: boundingRect() and translate(). As for the latter,
note that QWMatrix provides a map() function for more general transformation
of QPointArrays.
QPointArray is used by the QPainter to draw
\link QPainter::drawLineSegments() line segments\endlink,
\link QPainter::drawPolyline() polylines\endlink,
\link QPainter::drawPolygon() polygons\endlink and
\link QPainter::drawQuadBezier() Bezier curves\endlink.
Note that since this class is a QArray, it is
\link shclass.html explicitly shared\endlink
and works with shallow copies by default.
*/
/*****************************************************************************
QPointArray member functions
*****************************************************************************/
/*!
\fn QPointArray::QPointArray()
Constructs a null point array.
\sa isNull()
*/
/*!
\fn QPointArray::QPointArray( int size )
Constructs a point array with room for \e size points.
Makes a null array if \e size == 0.
\sa resize(), isNull()
*/
/*!
\fn QPointArray::QPointArray( const QPointArray &a )
Constructs a
\link shclass.html shallow copy\endlink of the point array \e a.
\sa copy()
*/
/*!
Constructs a point array from the rectangle \e r.
If \e closed is FALSE, then the point array will contain the
following four points (in the listed order):
<ol>
<li> r.topLeft()
<li> r.topRight()
<li> r.bottomRight()
<li> r.bottomLeft()
</ol>
If \e closed is TRUE, then a fifth point is set to r.topLeft() to
close the point array.
*/
QPointArray::QPointArray( const QRect &r, bool closed )
{
setPoints( 4, r.left(), r.top(),
r.right(), r.top(),
r.right(), r.bottom(),
r.left(), r.bottom() );
if ( closed ) {
resize( 5 );
setPoint( 4, r.left(), r.top() );
}
}
/*!
Constructs a point array with \e nPoints points, taken from the
\e points array.
Equivalent to setPoints(nPoints,points).
*/
QPointArray::QPointArray( int nPoints, const QCOORD *points )
{
setPoints( nPoints, points );
}
/*!
\fn QPointArray &QPointArray::operator=( const QPointArray &a )
Assigns a
\link shclass.html shallow copy\endlink of \e a to this point array
and returns a reference to this point array.
Equivalent to assign( a ).
\sa copy()
*/
/*!
\fn QPointArray QPointArray::copy() const
Creates a
\link shclass.html deep copy\endlink of the array.
*/
/*!
Translates all points in the array \e (dx,dy).
*/
void QPointArray::translate( int dx, int dy )
{
register QPoint *p = data();
register int i = size();
QPoint pt( dx, dy );
while ( i-- ) {
*p += pt;
p++;
}
}
/*!
Returns the point at position \e index in the array in \e *x and \e *y.
*/
void QPointArray::point( uint index, int *x, int *y ) const
{
QPoint p = QArray<QPoint>::at( index );
*x = (int)p.x();
*y = (int)p.y();
}
/*!
Returns the point at position \e index in the array.
*/
QPoint QPointArray::point( uint index ) const
{
return QArray<QPoint>::at( index );
}
/*!
Sets the point at position \e index in the array to \e (x,y).
*/
void QPointArray::setPoint( uint index, int x, int y )
{
QArray<QPoint>::at( index ) = QPoint( x, y );
}
/*!
Resizes the array to \e nPoints and sets the points in the array to
the values taken from \e points.
Returns TRUE if successful, or FALSE if the array could not be resized.
Example:
\code
static QCOORD points[] = { 1,2, 3,4 };
QPointArray a;
a.setPoints( 2, points );
\endcode
The example code creates an array with two points (1,2) and (3,4).
\sa resize(), putPoints()
*/
bool QPointArray::setPoints( int nPoints, const QCOORD *points )
{
if ( !resize(nPoints) )
return FALSE;
int i = 0;
while ( nPoints-- ) { // make array of points
setPoint( i++, *points, *(points+1) );
points++;
points++;
}
return TRUE;
}
/*!
\fn void QPointArray::setPoint( uint i, const QPoint &p )
Equivalent to setPoint( i, p.x(), p.y() ).
*/
/*!
Resizes the array to \e nPoints and sets the points in the array to
the values taken from the variable argument list.
Returns TRUE if successful, or FALSE if the array could not be resized.
Example:
\code
QPointArray a;
a.setPoints( 2, 1,2, 3,4 );
\endcode
The example code creates an array with two points (1,2) and (3,4).
\sa resize(), putPoints()
*/
bool QPointArray::setPoints( int nPoints, int firstx, int firsty,
... )
{
va_list ap;
if ( !resize(nPoints) )
return FALSE;
setPoint( 0, firstx, firsty ); // set first point
int i = 1, x, y;
nPoints--;
va_start( ap, firsty );
while ( nPoints-- ) {
x = va_arg( ap, int );
y = va_arg( ap, int );
setPoint( i++, x, y );
}
va_end( ap );
return TRUE;
}
/*!
Copies \e nPoints points from the \e points array into this point array.
Will resize this point array if <code>index+nPoints</code> exceeds
the size of the array.
Returns TRUE if successful, or FALSE if the array could not be resized.
Example:
\code
QPointArray a( 1 );
a[0] = QPoint( 1, 2 );
static QCOORD points[] = { 3,4, 5,6 };
a.putPoints( 1, 2, points );
\endcode
The example code creates an array with three points: (1,2), (3,4)
and (5,6).
This function differs from setPoints() in that it does not resize the
array unless the array size is exceeded.
\sa resize(), setPoints()
*/
bool QPointArray::putPoints( int index, int nPoints, const QCOORD *points )
{
if ( index + nPoints > (int)size() ) { // extend array
if ( !resize( index + nPoints ) )
return FALSE;
}
int i = index;
while ( nPoints-- ) { // make array of points
setPoint( i++, *points, *(points+1) );
points++;
points++;
}
return TRUE;
}
/*!
Copies \e nPoints points from the variable argument list into this point
array. Will resize this point array if <code>index+nPoints</code> exceeds
the size of the array.
Returns TRUE if successful, or FALSE if the array could not be resized.
Example:
\code
QPointArray a( 1 );
a[0] = QPoint( 1, 2 );
a.putPoints( 1, 2, 3,4, 5,6 );
\endcode
The example code creates an array with two points (1,2), (3,4) and (5,6).
This function differs from setPoints() because it does not resize the
array unless the array size is exceeded.
\sa resize(), setPoints()
*/
bool QPointArray::putPoints( int index, int nPoints, int firstx, int firsty,
... )
{
va_list ap;
if ( index + nPoints > (int)size() ) { // extend array
if ( !resize(index + nPoints) )
return FALSE;
}
if ( nPoints <= 0 )
return TRUE;
setPoint( index, firstx, firsty ); // set first point
int i = index + 1, x, y;
nPoints--;
va_start( ap, firsty );
while ( nPoints-- ) {
x = va_arg( ap, int );
y = va_arg( ap, int );
setPoint( i++, x, y );
}
va_end( ap );
return TRUE;
}
/*!
Returns the bounding rectangle of the points in the array, or
QRect(0,0,0,0) if the array is empty.
*/
QRect QPointArray::boundingRect() const
{
if ( isEmpty() )
return QRect( 0, 0, 0, 0 ); // null rectangle
register QPoint *pd = data();
int minx, maxx, miny, maxy;
minx = maxx = pd->x();
miny = maxy = pd->y();
pd++;
for ( int i=1; i<(int)size(); i++ ) { // find min+max x and y
if ( pd->x() < minx )
minx = pd->x();
else if ( pd->x() > maxx )
maxx = pd->x();
if ( pd->y() < miny )
miny = pd->y();
else if ( pd->y() > maxy )
maxy = pd->y();
pd++;
}
return QRect( QPoint(minx,miny), QPoint(maxx,maxy) );
}
static inline int fix_angle( int a )
{
if ( a > 16*360 )
a %= 16*360;
else if ( a < -16*360 )
a = -((-a) % 16*360);
return a;
}
/*!
Sets the points of the array to those describing an arc of an
ellipse with size \a w by \a h and position (\a x, \a y ), starting
from angle \a1, spanning \a a2. The resulting array has sufficient
resolution for pixel accuracy (see the overloaded function which
takes an additional QWMatrix parameter).
Angles are specified in 16ths of a degree,
i.e. a full circle equals 5760 (16*360). Positive values mean
counter-clockwise while negative values mean clockwise direction.
Zero degrees is at the 3'o clock position.
*/
void QPointArray::makeArc( int x, int y, int w, int h, int a1, int a2 )
{
QWMatrix unit;
makeArc(x,y,w,h,a1,a2,unit);
#if QT_OLD_MAKEELLIPSE // ### WWA says discard this.
a1 = fix_angle( a1 );
if ( a1 < 0 )
a1 += 16*360;
a2 = fix_angle( a2 );
int a3 = a2 > 0 ? a2 : -a2; // abs angle
makeEllipse( x, y, w, h );
int npts = a3*size()/(16*360); // # points in arc array
QPointArray a(npts);
int i = a1*size()/(16*360);
int j = 0;
if ( a2 > 0 ) {
while ( npts-- ) {
if ( i >= (int)size() ) // wrap index
i = 0;
a.QArray<QPoint>::at( j++ ) = QArray<QPoint>::at( i++ );
}
} else {
while ( npts-- ) {
if ( i < 0 ) // wrap index
i = (int)size()-1;
a.QArray<QPoint>::at( j++ ) = QArray<QPoint>::at( i-- );
}
}
*this = a;
return;
#endif
}
// Based upon:
// parelarc.c from Graphics Gems III
// VanAken / Simar, "A Parametric Elliptical Arc Algorithm"
//
static void
qtr_elips(QPointArray& a, int& offset, double dxP, double dyP, double dxQ, double dyQ, double dxK, double dyK, int m)
{
#define PIV2 102944 /* fixed point PI/2 */
#define TWOPI 411775 /* fixed point 2*PI */
#define HALF 32768 /* fixed point 1/2 */
int xP, yP, xQ, yQ, xK, yK;
xP = int(dxP * 65536.0); yP = int(dyP * 65536.0);
xQ = int(dxQ * 65536.0); yQ = int(dyQ * 65536.0);
xK = int(dxK * 65536.0); yK = int(dyK * 65536.0);
int i;
int vx, ux, vy, uy, xJ, yJ;
vx = xK - xQ; /* displacements from center */
ux = xK - xP;
vy = yK - yQ;
uy = yK - yP;
xJ = xP - vx + HALF; /* center of ellipse J */
yJ = yP - vy + HALF;
int r;
ux -= (r = ux >> (2*m + 3)); /* cancel 2nd-order error */
ux -= (r >>= (2*m + 4)); /* cancel 4th-order error */
ux -= r >> (2*m + 3); /* cancel 6th-order error */
ux += vx >> (m + 1); /* cancel 1st-order error */
uy -= (r = uy >> (2*m + 3)); /* cancel 2nd-order error */
uy -= (r >>= (2*m + 4)); /* cancel 4th-order error */
uy -= r >> (2*m + 3); /* cancel 6th-order error */
uy += vy >> (m + 1); /* cancel 1st-order error */
int n = offset;
for (i = (PIV2 << m) >> 16; i >= 0; --i) {
a[n++] = QPoint((xJ + vx) >> 16, (yJ + vy) >> 16);
ux -= vx >> m;
vx += ux >> m;
uy -= vy >> m;
vy += uy >> m;
}
offset = n;
#undef PIV2
#undef TWOPI
#undef HALF
}
/*!
Sets the points of the array to those describing an arc of an
ellipse with size \a w by \a h and position (\a x, \a y ), starting
from angle \a1, spanning \a a2, transformed by the matrix \a xf.
The resulting array has sufficient resolution for pixel accuracy.
Angles are specified in 16ths of a degree,
i.e. a full circle equals 5760 (16*360). Positive values mean
counter-clockwise while negative values mean clockwise direction.
Zero degrees is at the 3'o clock position.
*/
void QPointArray::makeArc( int x, int y, int w, int h,
int a1, int a2,
const QWMatrix& xf )
{
bool rev = a2 < 0;
if ( rev ) {
a1 += a2;
a2 = -a2;
}
a1 = fix_angle( a1 );
if ( a1 < 0 )
a1 += 16*360;
a2 = fix_angle( a2 );
bool arc = a1 != 0 || a2 != 360*16 || rev;
double xP, yP, xQ, yQ, xK, yK;
xf.map(x+w, y+h/2.0, &xP, &yP);
xf.map(x+w/2.0, y, &xQ, &yQ);
xf.map(x+w, y, &xK, &yK);
int m = 2;
int max;
int q = int(QMAX(QABS(xP-xQ),QABS(yP-yQ)));
if ( arc )
q *= 2;
do {
m++;
max = 4*(1 + int((Q_PI/2)*(1<<m)));
} while (max < q && m < 16); // 16 limits memory usage on HUGE arcs
resize(max);
int n = 0;
double inc = 1.0/(1<<m);
int nquad[4];
nquad[0]=0;
qtr_elips(*this, n, xP, yP, xQ, yQ, xK, yK, m);
nquad[1] = n;
xP = xQ; yP = yQ;
xf.map(x, y+h/2.0, &xQ, &yQ);
xf.map(x, y, &xK, &yK);
qtr_elips(*this, n, xP, yP, xQ, yQ, xK, yK, m);
nquad[2] = n;
xP = xQ; yP = yQ;
xf.map(x+w/2.0, y+h, &xQ, &yQ);
xf.map(x, y+h, &xK, &yK);
qtr_elips(*this, n, xP, yP, xQ, yQ, xK, yK, m);
nquad[3] = n;
xP = xQ; yP = yQ;
xf.map(x+w, y+h/2.0, &xQ, &yQ);
xf.map(x+w, y+h, &xK, &yK);
qtr_elips(*this, n, xP, yP, xQ, yQ, xK, yK, m);
if ( arc ) {
// We could merge the sub-ellipse extraction into the above so
// that we didn't generate points we don't need, but this is
// clearer, and optimizes for the common case.
double da1 = double(a1)*Q_PI / (360*8);
double da2 = double(a2)*Q_PI / (360*8);
int t = 0;
while ( da1 > Q_PI/2 ) {
da1 -= Q_PI/2;
t++;
}
int i = nquad[t]+int(da1/inc+0.5);
int k = int(da2/inc+0.5);
if ( rev ) {
QPointArray r(k);
int j = 0;
while (k--)
r[j++] = at((i+k)%n);
*this = r;
} else {
int j = 0;
while (j < k) {
setPoint(j,at((k+j)%n));
j++;
}
resize(j);
}
} else {
resize(n);
}
}
/*!
Sets the points of the array to those describing an ellipse with
size \a w by \a h and position (\a x, \a y ).
The returned array has sufficient
resolution for use as pixels (see the overloaded function which
takes an additional QWMatrix parameter).
*/
void QPointArray::makeEllipse( int xx, int yy, int w, int h )
{ // midpoint, 1/4 ellipse
QWMatrix unit;
makeArc(xx,yy,w,h,0,360*16,unit);
return;
#if QT_OLD_MAKEELLIPSE // ### WWA says discard this.
if ( w <= 0 || h <= 0 ) {
if ( w == 0 || h == 0 ) {
resize( 0 );
return;
}
if ( w < 0 ) { // negative width
w = -w;
xx -= w;
}
if ( h < 0 ) { // negative height
h = -h;
yy -= h;
}
}
int s = (w+h+2)/2; // max size of x,y array
int *px = new int[s]; // 1/4th of ellipse
int *py = new int[s];
int x, y, i=0;
double d1, d2;
double a2=(w/2)*(w/2), b2=(h/2)*(h/2);
x = 0;
y = int(h/2);
d1 = b2 - a2*(h/2) + 0.25*a2;
px[i] = x;
py[i] = y;
i++;
while ( a2*(y-0.5) > b2*(x+0.5) ) { // region 1
if ( d1 < 0 ) {
d1 = d1 + b2*(3.0+2*x);
x++;
} else {
d1 = d1 + b2*(3.0+2*x) + 2.0*a2*(1-y);
x++;
y--;
}
px[i] = x;
py[i] = y;
i++;
}
d2 = b2*(x+0.5)*(x+0.5) + a2*(y-1)*(y-1) - a2*b2;
while ( y > 0 ) { // region 2
if ( d2 < 0 ) {
d2 = d2 + 2.0*b2*(x+1) + a2*(3-2*y);
x++;
y--;
} else {
d2 = d2 + a2*(3-2*y);
y--;
}
px[i] = x;
py[i] = y;
i++;
}
s = i;
resize( 4*s ); // make full point array
xx += w/2;
yy += h/2;
for ( i=0; i<s; i++ ) { // mirror
x = px[i];
y = py[i];
setPoint( s-i-1, xx+x, yy-y );
setPoint( s+i, xx-x, yy-y );
setPoint( 3*s-i-1, xx-x, yy+y );
setPoint( 3*s+i, xx+x, yy+y );
}
delete[] px;
delete[] py;
#endif
}
// Work functions for QPointArray::quadBezier()
static
void split(const double *p, double *l, double *r)
{
double tmpx;
double tmpy;
l[0] = p[0];
l[1] = p[1];
r[6] = p[6];
r[7] = p[7];
l[2] = (p[0]+ p[2])/2;
l[3] = (p[1]+ p[3])/2;
tmpx = (p[2]+ p[4])/2;
tmpy = (p[3]+ p[5])/2;
r[4] = (p[4]+ p[6])/2;
r[5] = (p[5]+ p[7])/2;
l[4] = (l[2]+ tmpx)/2;
l[5] = (l[3]+ tmpy)/2;
r[2] = (tmpx + r[4])/2;
r[3] = (tmpy + r[5])/2;
l[6] = (l[4]+ r[2])/2;
l[7] = (l[5]+ r[3])/2;
r[0] = l[6];
r[1] = l[7];
}
// Based on:
//
// A Fast 2D Point-On-Line Test
// by Alan Paeth
// from "Graphics Gems", Academic Press, 1990
static
int pnt_on_line( const double* p, const double* q, const double* t )
{
/*
* given a line through P:(px,py) Q:(qx,qy) and T:(tx,ty)
* return 0 if T is not on the line through <--P--Q-->
* 1 if T is on the open ray ending at P: <--P
* 2 if T is on the closed interior along: P--Q
* 3 if T is on the open ray beginning at Q: Q-->
*
* Example: consider the line P = (3,2), Q = (17,7). A plot
* of the test points T(x,y) (with 0 mapped onto '.') yields:
*
* 8| . . . . . . . . . . . . . . . . . 3 3
* Y 7| . . . . . . . . . . . . . . 2 2 Q 3 3 Q = 2
* 6| . . . . . . . . . . . 2 2 2 2 2 . . .
* a 5| . . . . . . . . 2 2 2 2 2 2 . . . . .
* x 4| . . . . . 2 2 2 2 2 2 . . . . . . . .
* i 3| . . . 2 2 2 2 2 . . . . . . . . . . .
* s 2| 1 1 P 2 2 . . . . . . . . . . . . . . P = 2
* 1| 1 1 . . . . . . . . . . . . . . . . .
* +--------------------------------------
* 1 2 3 4 5 X-axis 10 15 19
*
* Point-Line distance is normalized with the Infinity Norm
* avoiding square-root code and tightening the test vs the
* Manhattan Norm. All math is done on the field of integers.
* The latter replaces the initial ">= MAX(...)" test with
* "> (ABS(qx-px) + ABS(qy-py))" loosening both inequality
* and norm, yielding a broader target line for selection.
* The tightest test is employed here for best discrimination
* in merging collinear (to grid coordinates) vertex chains
* into a larger, spanning vectors within the Lemming editor.
*/
if ( QABS((q[1]-p[1])*(t[0]-p[0])-(t[1]-p[1])*(q[0]-p[0])) >=
(QMAX(QABS(q[0]-p[0]), QABS(q[1]-p[1])))) return 0;
if (((q[0]<p[0])&&(p[0]<t[0])) || ((q[1]<p[1])&&(p[1]<t[1])))
return 1 ;
if (((t[0]<p[0])&&(p[0]<q[0])) || ((t[1]<p[1])&&(p[1]<q[1])))
return 1 ;
if (((p[0]<q[0])&&(q[0]<t[0])) || ((p[1]<q[1])&&(q[1]<t[1])))
return 3 ;
if (((t[0]<q[0])&&(q[0]<p[0])) || ((t[1]<q[1])&&(q[1]<p[1])))
return 3 ;
return 2 ;
}
static
void polygonizeQBezier( double* acc, int& accsize, const double ctrl[],
int maxsize )
{
if ( accsize > maxsize / 2 )
{
// This never happens in practice.
if ( accsize >= maxsize-4 )
return;
// Running out of space - approximate by a line.
acc[accsize++] = ctrl[0];
acc[accsize++] = ctrl[1];
acc[accsize++] = ctrl[6];
acc[accsize++] = ctrl[7];
return;
}
//intersects:
double l[8];
double r[8];
split( ctrl, l, r);
if ( pnt_on_line( &ctrl[0], &ctrl[6], &ctrl[2] ) == 2
&& pnt_on_line( &ctrl[0], &ctrl[6], &ctrl[4] ) == 2 )
{
// Approximate by 2 lines.
acc[accsize++] = l[0];
acc[accsize++] = l[1];
acc[accsize++] = l[6];
acc[accsize++] = l[7];
acc[accsize++] = r[6];
acc[accsize++] = r[7];
return;
}
// Too big and too curved - recusively subdivide.
polygonizeQBezier( acc, accsize, l, maxsize );
polygonizeQBezier( acc, accsize, r, maxsize );
}
/*!
Returns the Bezier points for the four control points in this array.
*/
QPointArray QPointArray::quadBezier() const
{
#ifdef USE_SIMPLE_QBEZIER_CODE
if ( size() != 4 ) {
#if defined(CHECK_RANGE)
qWarning( "QPointArray::bezier: The array must have 4 control points" );
#endif
QPointArray p;
return p;
}
int v;
const int n = 3; // n + 1 control points
float xvec[4];
float yvec[4];
for ( v=0; v<=n; v++ ) { // store all x,y in xvec,yvec
int x, y;
point( v, &x, &y );
xvec[v] = (float)x;
yvec[v] = (float)y;
}
QRect r = boundingRect();
int m = QMAX(r.width(),r.height())/2;
m = QMIN(m,30); // m = number of result points
if ( m < 2 ) // at least two points
m = 2;
QPointArray p( m ); // p = Bezier point array
register QPointData *pd = p.data();
float x0 = xvec[0], y0 = yvec[0];
float dt = 1.0F/m;
float cx = 3.0F * (xvec[1] - x0);
float bx = 3.0F * (xvec[2] - xvec[1]) - cx;
float ax = xvec[3] - (x0 + cx + bx);
float cy = 3.0F * (yvec[1] - y0);
float by = 3.0F * (yvec[2] - yvec[1]) - cy;
float ay = yvec[3] - (y0 + cy + by);
float t = dt;
pd->rx() = (QCOORD)xvec[0];
pd->ry() = (QCOORD)yvec[0];
pd++;
m -= 2;
while ( m-- ) {
pd->rx() = (QCOORD)qRound( ((ax * t + bx) * t + cx) * t + x0 );
pd->ry() = (QCOORD)qRound( ((ay * t + by) * t + cy) * t + y0 );
pd++;
t += dt;
}
pd->rx() = (QCOORD)xvec[3];
pd->ry() = (QCOORD)yvec[3];
return p;
#else
if ( size() != 4 ) {
#if defined(CHECK_RANGE)
qWarning( "QPointArray::bezier: The array must have 4 control points" );
#endif
QPointArray pa;
return pa;
} else {
QRect r = boundingRect();
int m = 4+2*QMAX(r.width(),r.height());
double *p = new double[m];
double *ctrl = new double[8];
int i;
for (i=0; i<4; i++) {
ctrl[i*2] = at(i).x();
ctrl[i*2+1] = at(i).y();
}
int len=0;
polygonizeQBezier( p, len, ctrl, m );
QPointArray pa(len/2);
int j=0;
for (i=0; j<len; i++) {
// Don't round - it looks terrible
int x = int(p[j++]);
int y = int(p[j++]);
pa[i] = QPoint(x,y);
}
delete[] p;
delete[] ctrl;
return pa;
}
#endif
}
/*****************************************************************************
QPointArray stream functions
*****************************************************************************/
/*!
\relates QPointArray
Writes a point array to the stream and returns a reference to the stream.
The serialization format is:
<ol>
<li> The array size (UINT32)
<li> The array points (QPoint)
</ol>
*/
QDataStream &operator<<( QDataStream &s, const QPointArray &a )
{
register uint i;
uint len = a.size();
s << len; // write size of array
for ( i=0; i<len; i++ ) // write each point
s << a.point( i );
return s;
}
/*!
\relates QPointArray
Reads a point array from the stream and returns a reference to the stream.
*/
QDataStream &operator>>( QDataStream &s, QPointArray &a )
{
register uint i;
uint len;
s >> len; // read size of array
if ( !a.resize( len ) ) // no memory
return s;
QPoint p;
for ( i=0; i<len; i++ ) { // read each point
s >> p;
a.setPoint( i, p );
}
return s;
}
struct QShortPoint { // Binary compatible with XPoint
short x, y;
};
uint QPointArray::splen = 0;
void* QPointArray::sp = 0; // Really a QShortPoint*
/*!
\internal
Converts the point coords to short (16bit) size, compatible with
X11's XPoint structure. The pointer returned points to a static
array, so its contents will be overwritten the next time this
function is called.
*/
void* QPointArray::shortPoints( int index, int nPoints ) const
{
if ( isNull() || !nPoints )
return 0;
QPoint* p = data();
p += index;
uint i = nPoints < 0 ? size() : nPoints;
if ( splen < i ) {
if ( sp )
delete[] ((QShortPoint*)sp);
sp = new QShortPoint[i];
splen = i;
}
QShortPoint* ps = (QShortPoint*)sp;
while ( i-- ) {
ps->x = (short)p->x();
ps->y = (short)p->y();
p++;
ps++;
}
return sp;
}
/*!
\internal
Deallocates the internal buffer used by shortPoints().
*/
void QPointArray::cleanBuffers()
{
if ( sp )
delete[] ((QShortPoint*)sp);
sp = 0;
splen = 0;
}
|