6 const double PI = 3.1415926535;
8 double angleBetweenVectors (
const QPointF &v1,
11 double v1Mag = qSqrt (v1.x() * v1.x() + v1.y() * v1.y());
12 double v2Mag = qSqrt (v2.x() * v2.x() + v2.y() * v2.y());
15 if ((v1Mag > 0) || (v2Mag > 0)) {
17 double cosArg = (v1.x() * v2.x() + v1.y() * v2.y()) / (v1Mag * v2Mag);
18 cosArg = qMin (qMax (cosArg, -1.0), 1.0);
19 angle = qAcos (cosArg);
25 double angleFromVectorToVector (
const QPointF &vFrom,
28 double angleFrom = qAtan2 (vFrom.y(), vFrom.x());
29 double angleTo = qAtan2 (vTo.y() , vTo.x());
33 double angleSeparation = angleTo - angleFrom;
35 while (angleSeparation < -1.0 * PI) {
36 angleSeparation += 2.0 * PI;
38 while (angleSeparation > PI) {
39 angleSeparation -= 2.0 * PI;
42 return angleSeparation;
45 QRgb pixelRGB(
const QImage &image,
int x,
int y)
47 switch (image.depth())
50 return pixelRGB1(image, x, y);
52 return pixelRGB8(image, x, y);
54 return pixelRGB32(image, x, y);
58 QRgb pixelRGB1(
const QImage &image1Bit,
int x,
int y)
61 if (image1Bit.format () == QImage::Format_MonoLSB) {
62 bit = *(image1Bit.scanLine (y) + (x >> 3)) & (1 << (x & 7));
64 bit = *(image1Bit.scanLine (y) + (x >> 3)) & (1 << (7 - (x & 7)));
67 unsigned int tableIndex = ((bit == 0) ? 0 : 1);
68 return image1Bit.color(tableIndex);
71 QRgb pixelRGB8(
const QImage &image8Bit,
int x,
int y)
73 unsigned int tableIndex = *(image8Bit.scanLine(y) + x);
74 return image8Bit.color(tableIndex);
77 QRgb pixelRGB32(
const QImage &image32Bit,
int x,
int y)
79 unsigned int* p = (
unsigned int *) image32Bit.scanLine(y) + x;
83 void projectPointOntoLine(
double xToProject,
91 double *projectedDistanceOutsideLine,
92 double *distanceToLine)
95 if (qAbs (yStart - yStop) > qAbs (xStart - xStop)) {
98 double slope = (xStop - xStart) / (yStart - yStop);
99 double yintercept = yToProject - slope * xToProject;
102 s = (slope * xStart + yintercept - yStart) /
103 (yStop - yStart + slope * (xStart - xStop));
108 double slope = (yStop - yStart) / (xStart - xStop);
109 double xintercept = xToProject - slope * yToProject;
112 s = (slope * yStart + xintercept - xStart) /
113 (xStop - xStart + slope * (yStart - yStop));
117 *xProjection = (1.0 - s) * xStart + s * xStop;
118 *yProjection = (1.0 - s) * yStart + s * yStop;
122 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStart) * (*xProjection - xStart) +
123 (*yProjection - yStart) * (*yProjection - yStart));
124 *distanceToLine = qSqrt ((xToProject - xStart) * (xToProject - xStart) +
125 (yToProject - yStart) * (yToProject - yStart));
128 *xProjection = xStart;
129 *yProjection = yStart;
133 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStop) * (*xProjection - xStop) +
134 (*yProjection - yStop) * (*yProjection - yStop));
135 *distanceToLine = qSqrt ((xToProject - xStop) * (xToProject - xStop) +
136 (yToProject - yStop) * (yToProject - yStop));
139 *xProjection = xStop;
140 *yProjection = yStop;
144 *distanceToLine = qSqrt ((xToProject - *xProjection) * (xToProject - *xProjection) +
145 (yToProject - *yProjection) * (yToProject - *yProjection));
148 *projectedDistanceOutsideLine = 0.0;
153 void setPixelRGB(QImage &image,
int x,
int y, QRgb q)
155 switch (image.depth())
158 setPixelRGB1(image, x, y, q);
161 setPixelRGB8(image, x, y, q);
164 setPixelRGB32(image, x, y, q);
169 void setPixelRGB1(QImage &image1Bit,
int x,
int y, QRgb q)
171 for (
int index = 0; index < image1Bit.colorCount(); index++) {
172 if (q == image1Bit.color(index))
174 if (image1Bit.format () == QImage::Format_MonoLSB)
176 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (x & 7));
178 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (x & 7);
182 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (7 - (x & 7)));
184 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (7 - (x & 7));
191 void setPixelRGB8(QImage &image8Bit,
int x,
int y, QRgb q)
193 for (
int index = 0; index < image8Bit.colorCount(); index++) {
194 if (q == image8Bit.color(index))
196 *(image8Bit.scanLine(y) + x) = index;
202 void setPixelRGB32(QImage &image32Bit,
int x,
int y, QRgb q)
204 int* p = (
int *)image32Bit.scanLine(y) + x;