as3 scaleX by percent of mc size - actionscript-3

How do I scale the X of an mc by a percentage?
For example I have the var percent. It starts at zero and goes to 100. But mx.scaleX = 33; will not work it needs to be .33 or .3 but again my numbers I am working with is 0 though 100 and I need the percentage of that to be written in a ways that the scaleX can handle it.
// other code to get correct percent; // result is 0 through 100
yellowProgress.scaleX = percent;

If percent is 100, scaleX should be 1 - so:
1 (as percent) is 1% of 100 (maximum percent), so you need 1% of 1 (maximum scaleX) which is 0.01. To get 1% of something, just divide it by 100.
So yellowProgress.scaleX = percent / 100; will do the trick.

Related

How to Calculate Scale Factor if Screen Resolution Will Change in AS3

If i change the screen resolution then, how I will calculate the Change Scale Factor of an object :
For example my object current this.scaleX is 0.8 , this.scaleY is 0.8 (as you know scaleX and scaleY are the properties of the Flex) and my current screen resolution is 1360*768
Now I change the screen resolution to 800*600 ,
Then how I will calculate the changed scaleX and scaleY of my object.
I am using the following formula to find the changed/updated Scale Factor :
scaleX = scaleX * (768 / 1360) * (800 / 600);
scaleY = scaleY * (768 / 1360) * (800 / 600);
But it is not giving Correct result to me,
So please Help me to solve this problem.
To calculate the scale of an axis you only need to take into account the values of that axis on the screen. So either X (width) or Y (height), but not both. Also I would use the width and height of the screen at a scale of 1 as reference values. This will make it easier to calculate the new scale. In your case:
screenWidthAtFullScale = 1360 / 0.8
screenHeightAtFullScale = 768 / 0.8
scaleX = 800 / screenWidthAtFullScale => 0.47
scaleY = 600 / screenHeightAtFullScale => 0.625
Note that this will distort your object. It would be better to calculate one scale and apply that to both the X and Y axis to prevent that.

Evenly distribute components relatively their size

Say there is HGroup which is 100px wide, and there are two images within: image_1 is 20px and image_2 is 30px.
I want image_1 to be centered within first 40 pixels (from pixel 11 to 30) and image_2 within remaining 60 pixels (from pixel 56 to 85) since they divide the whole 100-pixels wide space relatively their size.
Is there a component or property within Spark which allow to do that?
Maybe I'm wrong, but there isn't any method to do this. The instances names have been created for the example:
// container width = 100
var C:HGroupContainer = new HGroupContainer();
this.addChild(C);
const W1:int = 40;
const W2:int = 60;
var r1:Rect1 = new Rect1();
r1.x = (W1 - r1.width) / 2;
this.addChild(r1);
var r2:Rect2 = new Rect2();
r2.x = W1 + (W2 - r2.width) / 2;
this.addChild(r2);
The coordinates x = 0, y = 0 corresponding to the upper-left corner of the images.
You have to wrap the images in HGroups and set the horizontal alignment to center there. That's the Flex way to do it.

Flash AS3 square root?

I can't do this: How can I get a number of displacement (movieclip)?
Example: MovieClip.x == 0 and MovieClip.y == 0
Then I move it.
MovieClip.x == 50 and MovieClip.y == -90
Now if I make a tween, what is the number of displacement (moving)?
I'm going to take a guess and interpret the question to be something like, "How far as the MovieClip moved (or been displaced)?".
The answer would then be: 102.956 -> 103 units (pixels). You can use the Pythagorean theorem to figure this out:
xDist = 50 - 0
yDist = -90 - 0
distanceTraveled = sqrt((xDist * xDist) + (yDist * yDist))
Which would link as to why the poster asked about sqaure root.
Use Math.sqrt() to take the sqaure root of a number.

Getting percent of height, vertical volume slider

ok I am having a brain fart right now and can't think straight. I've done this many times before.
I am making a vertical volume slider. The top of my slider track is at -80 and the bottom is -16. So my volume handle can slide between -80 and -16 on the Y axis.
-48 is basically 50%. I can't for the life of me figure out this percentage right now, can someone give me a little math help. I took math in summer school all 3 years of high school for a reason :P
max = -80
min = -16
so total points in range = 64
so 50% of total = 64 / 2 = 32
Now starts moving up from -16. You get -48 when you move 32 (50%) points up.
So the percent for any value v (in positive) in range 16 to 80 is:
percent = (v - 16) / 64 * 100
For this example: (48 - 16) / 64 * 100 = 50
Negative numbers confuse me on this sort of thing, but that's what spreadsheets and trial & error are for.
If I read the question right, you know -48 is 50%, and you'd like to know the percentages for other inputs (e.g., -12, -60, etc)
Remove the bias so our range is 0 .. max
Divide that by the max to get the percentage
Take the absolute value
if v is your input value (-48), then p = Math.abs((v+16)/64) where "+16" is just subtracting your lower bound (-16), and "64" is the absolute value of the upper bound minus the lower bound.
Put another way,
var lower:int = -16;
var upper:int = -80;
var v:int = -32;
var p:Number = Math.abs((v - lower) / (Math.abs(upper - lower));
I haven't tested this - no compiler here.

Least amount of voters, given two halves

One of my former students sent me a message about this interview question he got while applying for a job as a Junior Developer.
There are two candidates running for president in a mock classroom election. Given the two percentages of voters, find out the least amount of possible voters in the classroom.
Examples:
Input: 50.00,50.00
Output: 2
Input: 25.00,75.00
Output: 4
Input: 53.23, 46.77
Output: 124 // The first value, 1138 was wrong. Thanks to Loïc for the correct value
Note: The sum of the input percentages are always 100.00%, two decimal places
The last example got me scratching my head. It was the first time I heard about this problem, and I'm kindof stumped on how to solve this.
EDIT: I called my student about the problem, and told me that he was not sure about the last value. He said, and I quote, "It was an absurdly large number output" :( sorry! I should've researched more before posting it online~ I'm guessing 9,797 is the output on the last example though..
You can compute these values by using the best rational approximations of the voter percentages. Wikipedia describes how to obtain these values from the continued fraction (which can be computed these using the euclidean algorithm). The desired result is the first approximation which is within 0.005% of the expected value.
Here's an example with 53.23%:
10000 = 1 * 5323 + 4677
5323 = 1 * 4677 + 646
4677 = 7 * 646 + 155
646 = 4 * 155 + 26
155 = 5 * 26 + 25
26 = 1 * 25 + 1
25 = 25* 1 + 0
Approximations:
1: 1 / 1
-> 1 = 100%
2: 1 / (1 + 1/1)
-> 1/2 = 50%
2.5: 1 / (1 + 1 / (1 + 1/6))
-> 7/1 = 53.75%
3: 1 / (1 + 1 / (1 + 1/7))
-> 8/15 = 53.33%
3.5: 1 / (1 + 1 / (1 + 1 / (7 + 1/3)))
-> 25/47 = 53.19%
4: 1 / (1 + 1 / (1 + 1 / (7 + 1/4)))
-> 33/62 = 53.23%
The reason we have extra values before the 3rd and 4th convergents is that their last terms (7 and 4 respectively) are greater than 1, so we must test the approximation with the last term decremented.
The desired result is the denominator of the first value which rounds to the desired value, which in this vase is 62.
Sample Ruby implementation available here (using the formulae from the Wikipedia page here, so it looks slightly different to the above example).
First you can notice that a trivial solution is to have 10.000 voters. Now let's try to find something lower than that.
For each value of N starting à 1
For Each value of i starting à 1
If i/N = 46.77
return N
Always choose the minimum of the two percentages to be faster.
Or faster :
For each value of N starting à 1
i = floor(N*46.77/100)
For j = i or i+1
If round(j/N) = 46.77 and round((N-j)/N) = 53.23
return N
For the third example :
605/1138 = .5316344464
(1138-605)/1138 = .4683655536
but
606/1138 = .5325131810
(1138-606)/1138 = .4674868190
It can't be 1138...
But 62 is working :
33/62 = .5322580645
(62-33)/62 = .4677419355
Rounded it's giving you the good values.
(After some extensive edits:)
If you only have 2 voters, then you can only generate the following percentages for candidates A and B:
0+100, 100+0, or 50+50
If you have 3 voters, then you have
0+100, 100+0, 33.33+66.67, 66.67+33.33 [notice the rounding]
So this is a fun problem about fractions.
If you can make 25% then you have to have at least 4 people (so you can do 1/4, since 1/2 and 1/3 won't cut it). You can do it with more (i.e. 2/8 = 25%) but the problem asks for the least.
However, more interesting fractions require numbers greater than 1 in the numerator:
2/5 = 40%
Since you can't get that with anything but a 2 or more in the numerator (1/x will never cut it).
You can compare at each step and increase either the numerator or denominator, which is much more efficient than iterating over the whole sample space for j and then incrementing i;
i.e. if you have a percentage of 3%, checking solutions all the way up in the fashion of 96/99, 97/99, 98/99 before even getting to x/100 is a waste of time. Instead, you can increment the numerator or denominator based on how well your current guess is doing (greater than or less than) like so
int max = 5000; //we only need to go half-way at most.
public int minVoters (double onePercentage) {
double checkPercentage = onePercentage;
if (onePercentage > 50.0)
checkPercentage = 100-onePercentage; //get the smaller percentage value
double i=1;
double j=1; //arguments of Math.round must be double or float
double temp = 0;
while (j<max || i<max-1) { //we can go all the way to 4999/5000 for the lesser value
temp = (i/j)*100;
temp = Math.round(temp);
temp = temp/100;
if (temp == checkPercentage)
return j;
else if (temp > checkPercentage) //we passed up our value and need to increase the denominator
j++;
else if (temp < checkPercentage) //we are too low and increase the numerator
i++;
}
return 0; //no such solution
}
Step-wise example for finding the denominator that can yield 55%
55/100 = 11/20
100-55 = 45 = 9/20 (checkPercentage will be 45.0)
1/1 100.0%
1/2 50.00%
1/3 33.33%
2/3 66.67%
2/4 50.00%
2/5 40.00%
3/5 60.00%
3/6 50.00%
3/7 42.86% (too low, increase numerator)
4/7 57.14% (too high, increase denominator)
4/8 50.00%
4/9 44.44%
5/9 55.56%
5/10 50.00%
5/11 45.45%
6/11 54.54%
6/12 50.00%
6/13 46.15%
6/14 42.86%
7/14 50.00%
7/15 46.67%
7/16 43.75%
8/16 50.00%
8/17 47.06%
8/19 42.11%
9/19 47.37%
9/20 45.00% <-bingo
The nice thing about this method is that it will only take (i+j) steps where i is the numerator and j is the denominator.
I cannot see the relevance of this question to a position as junior developer.
Then answer that jumped into my head was more of a brute-force approach. There can be at most 5001 unique answers because there 5001 unique numbers between 00.00 and 50.00 . Consequently, why not create and save a look-up table. Obviously, there won't be 5001 unique answer because some answers will be repeated. The point is, there are only 5001 valid fractions because we are rounding to two digits.
int[] minPossible = new int[5001];
int numSolutionsFound = 0;
N = 2;
while(numSolutionsFound < 5001) {
for(int i = 0 ; i <= N/2 ; i++) {
//compute i/N
//see if the corresponding table entry is set
//if not write N there and increment numSolutionsFound
}
N++;
}
//Save answer here
Now the solution is merely a table look up.
FWIW I realize the euclidean solution is "correct". But I'd NEVER come up with that mid interview. However, I'd know something like that was possible -- but I won't be able to whip it out on the spot.