42 ICOORDELT_IT it = &pt_list_;
44 it.add_to_end(new_pt);
50 ICOORDELT_IT it(&pt_list_);
52 if (pt_list_.empty()) {
62 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
64 starts[pt_count] = it.data();
65 ends[pt_count] = starts[pt_count];
68 ends[i - 1] = ends[i];
69 ends[kNumEndPoints - 1] = it.data();
83 int* distances =
new int[pt_count];
84 double best_uq = -1.0;
86 for (
int i = 0; i < end_count; ++i) {
88 for (
int j = 0; j < end_count; ++j) {
92 double dist = ComputeErrors(*start, *end, distances);
93 if (dist < best_uq || best_uq < 0.0) {
103 return best_uq > 0.0 ? sqrt(best_uq) : best_uq;
111 double error =
Fit(&start, &end);
112 if (end.
x() != start.
x()) {
113 *m =
static_cast<float>(end.
y() - start.
y()) / (end.
x() - start.
x());
114 *c = start.
y() - *m * start.
x();
125 if (m > 1.0 || m < -1.0) {
128 int dx =
static_cast<int>(floor(
MAX_INT16 / m));
130 if (start.
y() > 0) dx = - dx;
134 if (-1 <= dx && dx <= 1) {
135 return ICOORD(start.
x(), start.
y() + 1);
137 int y = start.
y() +
static_cast<int>(floor(dx * m + 0.5));
139 return ICOORD(start.
x() + dx, y);
143 int dy =
static_cast<int>(floor(
MAX_INT16 * m));
145 if (start.
x() > 0) dy = - dy;
149 if (-1 <= dy && dy <= 1) {
150 return ICOORD(start.
x() + 1, start.
y());
152 int x = start.
x() +
static_cast<int>(floor(dy / m + 0.5));
154 return ICOORD(x, start.
y() + dy);
160 ICOORDELT_IT it(&pt_list_);
162 if (pt_list_.empty()) {
170 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
172 pts[pt_count] = it.data();
176 pts[kNumEndPoints + i - 1] = pts[kNumEndPoints + i];
177 pts[kNumEndPoints * 2 - 1] = it.data();
182 pts[pt_count] =
NULL;
185 int* distances =
new int[pt_count];
186 double best_uq = -1.0;
190 if (start ==
NULL)
continue;
193 double dist = ComputeErrors(*start, end, distances);
194 if (dist < best_uq || best_uq < 0.0) {
196 *c = start->
y() - start->
x() * m;
201 return best_uq > 0.0 ? sqrt(best_uq) : best_uq;
205 static int CompareInts(
const void *p1,
const void *p2) {
206 const int* i1 =
reinterpret_cast<const int*
>(p1);
207 const int* i2 =
reinterpret_cast<const int*
>(p2);
214 double DetLineFit::ComputeErrors(
const ICOORD start,
const ICOORD end,
216 ICOORDELT_IT it(&pt_list_);
218 line_vector -= start;
221 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
222 ICOORD pt_vector = *it.data();
225 int dist = line_vector * pt_vector;
228 distances[pt_index++] = dist;
232 sizeof(distances[0]), CompareInts);
233 double dist = distances[index];
237 return dist * dist / (line_vector % line_vector);