12 const double PI = 3.1415926535;
14 double angleBetweenVectors (
const QPointF &v1,
17 double v1Mag = qSqrt (v1.x() * v1.x() + v1.y() * v1.y());
18 double v2Mag = qSqrt (v2.x() * v2.x() + v2.y() * v2.y());
21 if ((v1Mag > 0) || (v2Mag > 0)) {
23 double cosArg = (v1.x() * v2.x() + v1.y() * v2.y()) / (v1Mag * v2Mag);
24 cosArg = qMin (qMax (cosArg, -1.0), 1.0);
25 angle = qAcos (cosArg);
31 double angleFromVectorToVector (
const QPointF &vFrom,
34 double angleFrom = qAtan2 (vFrom.y(), vFrom.x());
35 double angleTo = qAtan2 (vTo.y() , vTo.x());
39 double angleSeparation = angleTo - angleFrom;
41 while (angleSeparation < -1.0 * PI) {
42 angleSeparation += 2.0 * PI;
44 while (angleSeparation > PI) {
45 angleSeparation -= 2.0 * PI;
48 return angleSeparation;
51 QRgb pixelRGB(
const QImage &image,
int x,
int y)
53 switch (image.depth())
56 return pixelRGB1(image, x, y);
58 return pixelRGB8(image, x, y);
60 return pixelRGB32(image, x, y);
64 QRgb pixelRGB1(
const QImage &image1Bit,
int x,
int y)
67 if (image1Bit.format () == QImage::Format_MonoLSB) {
68 bit = *(image1Bit.scanLine (y) + (x >> 3)) & (1 << (x & 7));
70 bit = *(image1Bit.scanLine (y) + (x >> 3)) & (1 << (7 - (x & 7)));
73 int tableIndex = ((bit == 0) ? 0 : 1);
74 return image1Bit.color(tableIndex);
77 QRgb pixelRGB8(
const QImage &image8Bit,
int x,
int y)
79 int tableIndex = *(image8Bit.scanLine(y) + x);
80 return image8Bit.color(tableIndex);
83 QRgb pixelRGB32(
const QImage &image32Bit,
int x,
int y)
89 const QRgb *p = reinterpret_cast<const QRgb *> (image32Bit.scanLine(y)) + x;
93 void projectPointOntoLine(
double xToProject,
101 double *projectedDistanceOutsideLine,
102 double *distanceToLine)
105 if (qAbs (yStart - yStop) > qAbs (xStart - xStop)) {
108 double slope = (xStop - xStart) / (yStart - yStop);
109 double yintercept = yToProject - slope * xToProject;
112 s = (slope * xStart + yintercept - yStart) /
113 (yStop - yStart + slope * (xStart - xStop));
118 double slope = (yStop - yStart) / (xStart - xStop);
119 double xintercept = xToProject - slope * yToProject;
122 s = (slope * yStart + xintercept - xStart) /
123 (xStop - xStart + slope * (yStart - yStop));
127 *xProjection = (1.0 - s) * xStart + s * xStop;
128 *yProjection = (1.0 - s) * yStart + s * yStop;
132 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStart) * (*xProjection - xStart) +
133 (*yProjection - yStart) * (*yProjection - yStart));
134 *distanceToLine = qSqrt ((xToProject - xStart) * (xToProject - xStart) +
135 (yToProject - yStart) * (yToProject - yStart));
138 *xProjection = xStart;
139 *yProjection = yStart;
143 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStop) * (*xProjection - xStop) +
144 (*yProjection - yStop) * (*yProjection - yStop));
145 *distanceToLine = qSqrt ((xToProject - xStop) * (xToProject - xStop) +
146 (yToProject - yStop) * (yToProject - yStop));
149 *xProjection = xStop;
150 *yProjection = yStop;
154 *distanceToLine = qSqrt ((xToProject - *xProjection) * (xToProject - *xProjection) +
155 (yToProject - *yProjection) * (yToProject - *yProjection));
158 *projectedDistanceOutsideLine = 0.0;
163 void setPixelRGB(QImage &image,
int x,
int y, QRgb q)
165 switch (image.depth())
168 setPixelRGB1(image, x, y, q);
171 setPixelRGB8(image, x, y, q);
174 setPixelRGB32(image, x, y, q);
179 void setPixelRGB1(QImage &image1Bit,
int x,
int y, QRgb q)
181 for (
int index = 0; index < image1Bit.colorCount(); index++) {
182 if (q == image1Bit.color(index))
184 if (image1Bit.format () == QImage::Format_MonoLSB)
186 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (x & 7));
188 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (x & 7);
192 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (7 - (x & 7)));
194 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (7 - (x & 7));
201 void setPixelRGB8(QImage &image8Bit,
int x,
int y, QRgb q)
203 for (
int index = 0; index < image8Bit.colorCount(); index++) {
204 if (q == image8Bit.color(index))
206 *(image8Bit.scanLine(y) + x) = static_cast<uchar> (index);
212 void setPixelRGB32(QImage &image32Bit,
int x,
int y, QRgb q)
214 int* p = (
int *)image32Bit.scanLine(y) + x;