Skip to content

Commit

Permalink
Rewrite and simplify swapping
Browse files Browse the repository at this point in the history
Inspired by forest bond's 'calibrator: Convert to device coordinates
immediately.' patch:
forestbond@1283252
  • Loading branch information
tias committed Jun 26, 2012
1 parent bfeaf2e commit 07978b9
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/calibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,20 @@ bool Calibrator::finish(int width, int height)
// based on old_axys: inversion/swapping is relative to the old axis
XYinfo new_axis(old_axys);

// Should x and y be swapped?
bool do_swap_xy = false;
if (abs(clicked.x[UL] - clicked.x[UR]) < abs(clicked.y[UL] - clicked.y[UR])) {
do_swap_xy = true;
new_axis.do_swap_xy();
}

if (do_swap_xy) {
std::swap(clicked.x[LL], clicked.x[UR]);
std::swap(clicked.y[LL], clicked.y[UR]);
}

// calculate average of clicks
float x_min = (clicked.x[UL] + clicked.x[LL])/2.0;
float x_max = (clicked.x[UR] + clicked.x[LR])/2.0;
float y_min = (clicked.y[UL] + clicked.y[UR])/2.0;
float y_max = (clicked.y[LL] + clicked.y[LR])/2.0;

// Should x and y be swapped?
if (abs(clicked.x[UL] - clicked.x[UR]) < abs(clicked.y[UL] - clicked.y[UR])) {
new_axis.swap_xy = !new_axis.swap_xy;
std::swap(x_min, y_min);
std::swap(x_max, y_max);
}

// the screen was divided in num_blocks blocks, and the touch points were at
// one block away from the true edges of the screen.
const float block_x = width/(float)num_blocks;
Expand All @@ -184,12 +180,7 @@ bool Calibrator::finish(int width, int height)
y_max = scaleAxis(y_max, old_axys.y.max, old_axys.y.min, height, 0);


// If x and y has to be swapped we also have to swap the parameters
if (do_swap_xy) {
std::swap(x_min, y_max);
std::swap(y_min, x_max);
}

// round and put in new_axis struct
new_axis.x.min = round(x_min); new_axis.x.max = round(x_max);
new_axis.y.min = round(y_min); new_axis.y.max = round(y_max);

Expand Down

0 comments on commit 07978b9

Please sign in to comment.