0

I'm scaling an value and it works as I intend when the scale is 1 or greater but it doesn't seem to scale as intended when scaling down.

// increase by 10%
amount = 10
scale = 1
total = scale + amount/100

// decrease by 10%
amount = 10
scale = 1
total = scale - amount/100

I know this is easy but I'm blanking on this.

Work as intended meaning, I'm zooming in on an image and zoom in is a nice gradient but scaling down the image disappears in 9 steps. I would like it to scale gradually at a consistent rate.

Edit. I found this in my other class. I think it does what I want but is there be another way to write it?

scaleByAmount = amount/100

if (currentScale<=1) {
    newScale = currentScale+currentScale*scaleByAmount;
}
else {
    newScale = currentScale+scaleByAmount;
}
  • 1
    doesn't seem to scale as intended Define "as intended". – dxiv Feb 27 '18 at 06:47
  • It seems to me like the "total" is the correct factor ... Increase by 10 per cent is equivalent to multiplying by $1.1$ and decrease by 10 per cent is equivalent to multiplying by $0.9$. – Matti P. Feb 27 '18 at 06:47
  • It's unclear what the problem here is. – Matti P. Feb 27 '18 at 06:49
  • I'm scaling an image in and out. It seems that zoom out happens rather quickly while zoom in increases the image size at a relatively normal value when the scale is less than 1 – 1.21 gigawatts Feb 27 '18 at 06:54
  • 2
    @1.21gigawatts Again, what do you expect? Note that increasing by 10% then decreasing by 10% will not return you to the original value, but rather to $,0.9 \cdot 1.1 = 0.99,$ of it. – dxiv Feb 27 '18 at 06:56
  • @dxiv That might be the issue then. As I scale up 10% of 1 would be .1 while 10% of 100 would increase by 10. So I need to increase and decrease by a consistent value no matter the current scale? – 1.21 gigawatts Feb 27 '18 at 07:00
  • 1
    @1.21gigawatts That depends on what you want to achieve, and how you document/convey that to your users. In the context of zooming, it's common to consider all percentages relative to the original size. – dxiv Feb 27 '18 at 07:04
  • 1
    I thought it was typical to zoom in or out by the same constant factor in both directions--when you want the "scale" to get larger, you multiply, and when you want it to get smaller, you divide. For example, multiplying by $1.1$ nine times and then dividing by $1.1$ nine times will get you back to the same scale you started with. – David K Feb 27 '18 at 07:12
  • @DavidK Do you mean something like size*.1 to zoom in and size/.1 to zoom out? – 1.21 gigawatts Feb 27 '18 at 07:41
  • @1.21gigawatts It should be $size \cdot 1.1$ to zoom in, and $size \cdot \frac{1}{1.1}$ to zoom out. Remember that $size \cdot 1$ is just the original size, so $size \cdot 0.1 = size \cdot (1-0.9)$, which is a $90$ percent decrease. – Toby Mak Feb 27 '18 at 10:43

1 Answers1

1

It should be $size⋅1.1$ to zoom in, and $size⋅\frac{1}{1.1}$ to zoom out. Since $size⋅1$ is just the original size, so $size⋅0.1=size⋅(1−0.9)$, which is a $90$ percent decrease.

In general, if you want to increase by $x \%$, then the formula is $size \cdot (1+x \%)$. If you want to decrease by $x%$, then the formula is just $size \cdot (1-x \%)$.

Toby Mak
  • 16,827