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 unsigned int tableIndex = ((bit == 0) ? 0 : 1);
74 return image1Bit.color(tableIndex);
77 QRgb pixelRGB8(
const QImage &image8Bit,
int x,
int y)
79 unsigned int tableIndex = *(image8Bit.scanLine(y) + x);
80 return image8Bit.color(tableIndex);
83 QRgb pixelRGB32(
const QImage &image32Bit,
int x,
int y)
85 unsigned int* p = (
unsigned int *) image32Bit.scanLine(y) + x;
89 void projectPointOntoLine(
double xToProject,
97 double *projectedDistanceOutsideLine,
98 double *distanceToLine)
101 if (qAbs (yStart - yStop) > qAbs (xStart - xStop)) {
104 double slope = (xStop - xStart) / (yStart - yStop);
105 double yintercept = yToProject - slope * xToProject;
108 s = (slope * xStart + yintercept - yStart) /
109 (yStop - yStart + slope * (xStart - xStop));
114 double slope = (yStop - yStart) / (xStart - xStop);
115 double xintercept = xToProject - slope * yToProject;
118 s = (slope * yStart + xintercept - xStart) /
119 (xStop - xStart + slope * (yStart - yStop));
123 *xProjection = (1.0 - s) * xStart + s * xStop;
124 *yProjection = (1.0 - s) * yStart + s * yStop;
128 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStart) * (*xProjection - xStart) +
129 (*yProjection - yStart) * (*yProjection - yStart));
130 *distanceToLine = qSqrt ((xToProject - xStart) * (xToProject - xStart) +
131 (yToProject - yStart) * (yToProject - yStart));
134 *xProjection = xStart;
135 *yProjection = yStart;
139 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStop) * (*xProjection - xStop) +
140 (*yProjection - yStop) * (*yProjection - yStop));
141 *distanceToLine = qSqrt ((xToProject - xStop) * (xToProject - xStop) +
142 (yToProject - yStop) * (yToProject - yStop));
145 *xProjection = xStop;
146 *yProjection = yStop;
150 *distanceToLine = qSqrt ((xToProject - *xProjection) * (xToProject - *xProjection) +
151 (yToProject - *yProjection) * (yToProject - *yProjection));
154 *projectedDistanceOutsideLine = 0.0;
159 void setPixelRGB(QImage &image,
int x,
int y, QRgb q)
161 switch (image.depth())
164 setPixelRGB1(image, x, y, q);
167 setPixelRGB8(image, x, y, q);
170 setPixelRGB32(image, x, y, q);
175 void setPixelRGB1(QImage &image1Bit,
int x,
int y, QRgb q)
177 for (
int index = 0; index < image1Bit.colorCount(); index++) {
178 if (q == image1Bit.color(index))
180 if (image1Bit.format () == QImage::Format_MonoLSB)
182 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (x & 7));
184 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (x & 7);
188 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (7 - (x & 7)));
190 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (7 - (x & 7));
197 void setPixelRGB8(QImage &image8Bit,
int x,
int y, QRgb q)
199 for (
int index = 0; index < image8Bit.colorCount(); index++) {
200 if (q == image8Bit.color(index))
202 *(image8Bit.scanLine(y) + x) = index;
208 void setPixelRGB32(QImage &image32Bit,
int x,
int y, QRgb q)
210 int* p = (
int *)image32Bit.scanLine(y) + x;