I know this a stupid question to ask, but I was going through a tutorial and I just dont seem to understand the below lines of code…Have pulled my hair understanding the geometrical algorithm behind this, but…..Oh!!! This is very simple but I just dont seem to get it. Please help….I am a beginner…
private void drawPict(Canvas canvas, int x, int y, int w, int h,
float sx, float sy) {
canvas.save();
canvas.translate(x, y);
canvas.clipRect(0, 0, w, h);
canvas.scale(0.5f, 0.5f);
canvas.scale(sx, sy, w, h);
canvas.drawPicture(mPicture);
canvas.restore();
}
@SuppressWarnings("unused")
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(mPicture.beginRecording(getWidth(), getHeight()));
mPicture.endRecording();
int x = getWidth()/2;
int y = getHeight()/2;
if (false) {
canvas.drawPicture(mPicture);
} else {
drawPict(canvas, 0, 0, x, y, 1, 1);
drawPict(canvas, x, 0, x, y, -1, 1);
drawPict(canvas, 0, y, x, y, 1, -1);
drawPict(canvas, x, y, x, y, -1, -1);
}
}
I have looked into this again…and understand that canvas.scale has 4 parameters, being the co-ordinates of the points I assume…but I cannot still understand the float sx, float sy…
sx=-1 and sy=1…it will scale, agreed…but is not there a better way?
Scaling by a factor of +1 gives an identity transformation along an axis, and scaling by a factor of -1 gives a reflection across an axis. In the present example, you have each of the four possible combinations of reflection/no-reflection across each of the axes. Scaling both coordinates by +1 is the identity transformation. Scaling x-coordinates by -1 and y-coordinates by +1 gives a reflection across a vertical line, that is, a left-right reflection. Vice-versa for scaling x by +1 and y by -1, which is an up-down reflection. Scaling both coordinates by -1 gives a double reflection, which is identical to a rotation by 180 degrees.
The Canvas class does not have a separate call for reflections, so scaling is used instead.