00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <iostream>
00026 #include <float.h>
00027
00028 #include <qvmath.h>
00029 #include <qvdefines.h>
00030
00031
00032 #include <QVPolyline>
00033 #include <QVPolylineF>
00034
00035 QVPolyline::QVPolyline(const int estimatedSize): QList<QPoint>(),
00036 closed(false), direction(false)
00037 {
00038 qDebug() << "QVPolyline()";
00039
00040 if (estimatedSize > 0)
00041 #ifdef QT_MIN_VERSION_4_7
00042 QList<QPoint>::reserve(estimatedSize);
00043 #else
00044 Q_WARNING("cannot reserve a size for Qt versions older than 4.7");
00045 #endif // QT_MIN_VERSION_4_7
00046
00047 qDebug() << "QVPolyline() <~ return";
00048 };
00049
00050 QVPolyline::QVPolyline(const QVPolyline &polyline): QList<QPoint>(polyline),
00051 closed(polyline.closed), direction(polyline.direction)
00052 {
00053 qDebug() << "QVPolyline(const QVPolyline &)";
00054 qDebug() << "QVPolyline(const QVPolyline &) <~ return";
00055 };
00056
00057 QVPolyline::QVPolyline(const QVPolylineF &polyline): QList<QPoint>(),
00058 closed(polyline.closed), direction(polyline.direction)
00059 {
00060 foreach(QPointF pointf, polyline)
00061 {
00062 append( QPoint(pointf.toPoint()) );
00063 }
00064 qDebug() << "QVPolylineF(const QVPolylineF &)";
00065 qDebug() << "QVPolylineF(const QVPolylineF &) <~ return";
00066 };
00067
00068 QVPolyline QVPolyline::ellipse(uInt n, float x, float y, float maxRadio, float minRadio, float ang)
00069 {
00070 QVPolyline ellipse;
00071 float w = 2*PI/(n-1);
00072 float maxArg = (maxRadio+minRadio)/2;
00073 float minArg = (maxRadio-minRadio)/2;
00074
00075 for (uInt t = 0; t < n; t++)
00076 ellipse.append(QPoint ( (uInt) (x + maxArg*cos(ang+w*t) + minArg*cos(ang-w*t)),
00077 (uInt) (y + maxArg*sin(ang+w*t) + minArg*sin(ang-w*t))
00078 ));
00079 return ellipse;
00080 }
00081
00082 QVPolyline QVPolyline::rectangle(int x1, int y1, int x2, int y2)
00083 {
00084 QVPolyline rectangle;
00085 rectangle.append(QPoint( x1, y1 ));
00086 rectangle.append(QPoint( x1, y2 ));
00087 rectangle.append(QPoint( x2, y2 ));
00088 rectangle.append(QPoint( x2, y1 ));
00089 rectangle.append(QPoint( x1, y1 ));
00090 return rectangle;
00091 }
00092
00093 QVPolyline QVPolyline::line(int x1, int y1, int x2, int y2)
00094 {
00095 QVPolyline line;
00096
00097 line.append(QPoint( x1, y1 ));
00098
00099 if (x1 == x2 && y1 == y2)
00100 return line;
00101
00102 int i,dx,dy,sdx,sdy,dxabs,dyabs,x,y,px,py;
00103
00104 dx=x2-x1;
00105 dy=y2-y1;
00106 dxabs=abs(dx);
00107 dyabs=abs(dy);
00108 sdx=SIGN(dx);
00109 sdy=SIGN(dy);
00110 x=dyabs>>1;
00111 y=dxabs>>1;
00112 px=x1;
00113 py=y1;
00114
00115 if (dxabs>=dyabs)
00116 for(i=0;i<dxabs;i++)
00117 {
00118 y+=dyabs;
00119 if (y>=dxabs)
00120 {
00121 y-=dxabs;
00122 py+=sdy;
00123 }
00124 px+=sdx;
00125 line.append(QPoint( px, py ));
00126 }
00127 else
00128 for(i=0;i<dyabs;i++)
00129 {
00130 x+=dxabs;
00131 if (x>=dyabs)
00132 {
00133 x-=dyabs;
00134 px+=sdx;
00135 }
00136 py+=sdy;
00137 line.append(QPoint( px, py ));
00138 }
00139
00140 return line;
00141 }
00142
00143 QVPolyline::operator QVPolylineF() const
00144 {
00145 QVPolylineF polyline;
00146 foreach(QPoint point, *this)
00147 {
00148 polyline.append(QPointF(point));
00149 }
00150 return polyline;
00151 }
00152
00153 std::ostream& operator << ( std::ostream &os, const QVPolyline &polyline)
00154 {
00155 const int size = polyline.size();
00156
00157 os << "QVPolyline (" << size << ")" ;
00158
00159 os << " [" ;
00160 for (int i=0; i < size; i++)
00161 {
00162 os << " (" << polyline[i].x() << "," << polyline[i].y() << ")";
00163 if(i!=size-1)
00164 os << ",";
00165 else
00166 os << " ";
00167 }
00168 os << "]" ;
00169 return os;
00170 }