13 const double PI = 3.1415926535;
18 double v1Mag = qSqrt (v1.x() * v1.x() + v1.y() * v1.y());
19 double v2Mag = qSqrt (v2.x() * v2.x() + v2.y() * v2.y());
22 if ((v1Mag > 0) || (v2Mag > 0)) {
24 double cosArg = (v1.x() * v2.x() + v1.y() * v2.y()) / (v1Mag * v2Mag);
25 cosArg = qMin (qMax (cosArg, -1.0), 1.0);
26 angle = qAcos (cosArg);
35 double angleFrom = qAtan2 (vFrom.y(), vFrom.x());
36 double angleTo = qAtan2 (vTo.y() , vTo.x());
40 double angleSeparation = angleTo - angleFrom;
42 while (angleSeparation < -1.0 *
PI) {
43 angleSeparation += 2.0 *
PI;
45 while (angleSeparation >
PI) {
46 angleSeparation -= 2.0 *
PI;
49 return angleSeparation;
75 double xT = (xTL + xTR) / 2.0;
76 double yT = (yTL + yTR) / 2.0;
77 double xR = (xTR + xBR) / 2.0;
78 double yR = (yTR + yBR) / 2.0;
109 double m00 = xT * xT;
110 double m01 = yT * yT;
111 double m02 = 2.0 * xT * yT;
112 double m10 = xR * xR;
113 double m11 = yR * yR;
114 double m12 = 2.0 * xR * yR;
121 if (qAbs (yTR - yTL) * qAbs (xTR - xBR) < qAbs (yTR - yBR) * qAbs (xTR - xTL)) {
123 double mT = (yTR - yTL) / (xTR - xTL);
130 double mR = (yTR - yBR) / (xTR - xBR);
137 QTransform denominator (m00, m01, m02,
140 QTransform numeratorA (-1.0, m01, m02,
143 QTransform numeratorB (m00, -1.0, m02,
146 QTransform numeratorC (m00, m01, -1.0,
149 double A = numeratorA.determinant () / denominator.determinant ();
150 double B = numeratorB.determinant () / denominator.determinant ();
151 double C = numeratorC.determinant () / denominator.determinant ();
156 double numerator = 4.0 * F * C * C - 4.0 * A * B * F;
157 double denominatorMinus = 2.0 * (A * B - C * C) * (A + B + qSqrt ((B - A) * (B - A) + 4 * C * C));
158 double denominatorPlus = 2.0 * (A * B - C * C) * (A + B - qSqrt ((B - A) * (B - A) + 4 * C * C));
159 aAligned = qSqrt (numerator / denominatorMinus);
160 bAligned = qSqrt (numerator / denominatorPlus);
162 if (qAbs (2.0 * C) > 10000.0 * qAbs (A - B)) {
165 angleRadians = 0.5 * qAtan (2 * C / (A - B));
171 switch (image.depth())
185 if (image1Bit.format () == QImage::Format_MonoLSB) {
186 bit = *(image1Bit.scanLine (y) + (x >> 3)) & (1 << (x & 7));
188 bit = *(image1Bit.scanLine (y) + (x >> 3)) & (1 << (7 - (x & 7)));
191 int tableIndex = ((bit == 0) ? 0 : 1);
192 return image1Bit.color(tableIndex);
197 int tableIndex = *(image8Bit.scanLine(y) + x);
198 return image8Bit.color(tableIndex);
207 const QRgb *p =
reinterpret_cast<const QRgb *
> (image32Bit.scanLine(y)) + x;
219 double *projectedDistanceOutsideLine,
220 double *distanceToLine)
223 if (qAbs (yStart - yStop) > qAbs (xStart - xStop)) {
226 double slope = (xStop - xStart) / (yStart - yStop);
227 double yintercept = yToProject - slope * xToProject;
230 s = (slope * xStart + yintercept - yStart) /
231 (yStop - yStart + slope * (xStart - xStop));
236 double slope = (yStop - yStart) / (xStart - xStop);
237 double xintercept = xToProject - slope * yToProject;
240 s = (slope * yStart + xintercept - xStart) /
241 (xStop - xStart + slope * (yStart - yStop));
245 *xProjection = (1.0 - s) * xStart + s * xStop;
246 *yProjection = (1.0 - s) * yStart + s * yStop;
250 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStart) * (*xProjection - xStart) +
251 (*yProjection - yStart) * (*yProjection - yStart));
252 *distanceToLine = qSqrt ((xToProject - xStart) * (xToProject - xStart) +
253 (yToProject - yStart) * (yToProject - yStart));
256 *xProjection = xStart;
257 *yProjection = yStart;
261 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStop) * (*xProjection - xStop) +
262 (*yProjection - yStop) * (*yProjection - yStop));
263 *distanceToLine = qSqrt ((xToProject - xStop) * (xToProject - xStop) +
264 (yToProject - yStop) * (yToProject - yStop));
267 *xProjection = xStop;
268 *yProjection = yStop;
272 *distanceToLine = qSqrt ((xToProject - *xProjection) * (xToProject - *xProjection) +
273 (yToProject - *yProjection) * (yToProject - *yProjection));
276 *projectedDistanceOutsideLine = 0.0;
283 switch (image.depth())
299 for (
int index = 0; index < image1Bit.colorCount(); index++) {
300 if (q == image1Bit.color(index))
302 if (image1Bit.format () == QImage::Format_MonoLSB)
304 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (x & 7));
306 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (x & 7);
310 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (7 - (x & 7)));
312 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (7 - (x & 7));
321 for (
int index = 0; index < image8Bit.colorCount(); index++) {
322 if (q == image8Bit.color(index))
324 *(image8Bit.scanLine(y) + x) = static_cast<uchar> (index);
332 int* p = (
int *)image32Bit.scanLine(y) + x;
QRgb pixelRGB8(const QImage &image8Bit, int x, int y)
Get pixel method for 8 bit depth.
void setPixelRGB32(QImage &image32Bit, int x, int y, QRgb q)
Set pixel method for 32 bit depth.
void projectPointOntoLine(double xToProject, double yToProject, double xStart, double yStart, double xStop, double yStop, double *xProjection, double *yProjection, double *projectedDistanceOutsideLine, double *distanceToLine)
Find the projection of a point onto a line segment such that the line through the point and its proje...
void setPixelRGB(QImage &image, int x, int y, QRgb q)
Set pixel method for any bit depth.
QRgb pixelRGB(const QImage &image, int x, int y)
Get pixel method for any bit depth.
void ellipseFromParallelogram(double xTL, double yTL, double xTR, double yTR, double xBR, double yBR, double &angleRadians, double &aAligned, double &bAligned)
Calculate ellipse parameters that is incribed in a parallelogram centered at the origin, given three successive corners of that parallelogram.
QRgb pixelRGB32(const QImage &image32Bit, int x, int y)
Get pixel method for 32 bit depth.
void setPixelRGB8(QImage &image8Bit, int x, int y, QRgb q)
Set pixel method for 8 bit depth.
double angleBetweenVectors(const QPointF &v1, const QPointF &v2)
Angle between two vectors. Direction is unimportant, so result is between 0 to pi radians...
QRgb pixelRGB1(const QImage &image1Bit, int x, int y)
Get pixel method for one bit depth.
double angleFromVectorToVector(const QPointF &vFrom, const QPointF &vTo)
Angle between two vectors. Direction is positive when rotation is about +z vector, so result is betwen -pi to pi radians.
void setPixelRGB1(QImage &image1Bit, int x, int y, QRgb q)
Set pixel method for one bit depth.