/** * User barycentric interpolation to determine the shift at a given point. */ Vec2d _interpolateShift(const Coordinate& p) { Vec2d result; // calculate the grid cell that this point falls in. int r = (p.y - _e.getMinY()) / _gridSpacing; int c = (p.x - _e.getMinX()) / _gridSpacing; double dx = p.x - (_e.getMinX() + c * _gridSpacing); double dy = p.y - (_e.getMinY() + r * _gridSpacing); //// // Using barycentric interpolation. //// // if we're right on a point (most interestingly if we're right on the upper right point which // would cause the following two blocks to fail). if (dx == 0.0 && dy == 0.0) { result.val[0] = _getX(r, c); result.val[1] = _getY(r, c); } // if we're in the lower right triangle else if (dx > dy) { Coordinate t1 = gridCoordinate(r, c); Coordinate t2 = gridCoordinate(r, c + 1); Coordinate t3 = gridCoordinate(r + 1, c + 1); double a1 = triArea2(t3, t2, p); double a2 = triArea2(t1, t3, p); double a3 = triArea2(t1, t2, p); double areaSum = a1 + a2 + a3; result.val[0] = (_getX(r, c) * a1 + _getX(r, c + 1) * a2 + _getX(r + 1, c + 1) * a3) / areaSum; result.val[1] = (_getY(r, c) * a1 + _getY(r, c + 1) * a2 + _getY(r + 1, c + 1) * a3) / areaSum; } // if we're in the upper left triangle else { Coordinate t1 = gridCoordinate(r, c); Coordinate t4 = gridCoordinate(r + 1, c); Coordinate t3 = gridCoordinate(r + 1, c + 1); double a1 = triArea2(t3, t4, p); double a3 = triArea2(t1, t4, p); double a4 = triArea2(t1, t3, p); double areaSum = a1 + a3 + a4; result.val[0] = (_getX(r, c) * a1 + _getX(r + 1, c) * a4 + _getX(r + 1, c + 1) * a3) / areaSum; result.val[1] = (_getY(r, c) * a1 + _getY(r + 1, c) * a4 + _getY(r + 1, c + 1) * a3) / areaSum; } return result; }
/* * call-seq: * inspect -> String * * Human-readable description. * ===Return value * String */ VALUE _inspect(VALUE self) { VALUE array[6]; array[0]=rb_str_new2("#<%s:(%d, %d, %d, %d)>"); array[1]=rb_class_of(self); array[2]=_getX(self); array[3]=_getY(self); array[4]=_getWidth(self); array[5]=_getHeight(self); return rb_f_sprintf(6,array); }