Anyone good at math, that can help me out?

Hi there!

Lets for simplicity say that I would like to make a progress bar. The basic math is as following:

Progress/MaxProgress * 100 --(1/2 * 100 = 50%)

Same principal for what I want to achieve, but with a few things to take in mind.

-- We have MaxValueA and MaxValueB. MaxValueA == 30 and MaxValueB == 12
-- We have ProgressValueA and ProgressValueB. ProgressValueA == 1 and ProgressValueB == 2
-- The amount of ProgressValueA's + ProgressValueB's will always be equal to MaxValueB.
-- We have 14*ProgressValueA's and 16*ProgressValueB's (equivalent MaxValueA aka. 30). Our "TotalWeight" combined will therefore be 46. (14*1 + 16*2)
-- How would I transform those 46 weight into MaxValueA(30), but keeping the individual weight of each ProgressValue. (For example if we wanted to change 46 to 23, then our ProgressValueA would be 0.5 and ProgressValueB would be 1)

Hope it made sense, tried to simplify it as much as possible.

2 Likes

How can ProgressValueA’s + ProgressValueB’s always equal MaxValue if MaxValue is 12, ProgressValueA is 1, and ProgressValueB is 2? 1+2 != 12

1 Like

Hi!

It’s the amount of ProgressValueA’s + ProgressValueB’s (not their values), that is always equal to the value of MaxValueB, meaning that there will always be 12 of them (in this sample). It can everything from 10 ProgressValueA’s and 2 ProgressValueB’s or 5 ProgressValueA’s and 7 ProgressValueB’s, ect.

And then the combined ProgressValue’s value, should always scale correctly so the total value of both ProgressValueA’s total-value + ProgressValueB’s total-value will always be equal to MaxValueA.

Do you think you could provide a visual example? It’s a bit hard to visualize what you’re looking to achieve.

2 Likes

“But sliceTypeA could be 7
and SliceTypeB* could be 3”

So you’re just trying to find what percent is sliceTypeA and what percent is sliceTypeB?

local totalSlices = sliceTypeA + sliceTypeB
local percentageSliceTypeA = sliceTypeA / totalSlices
local percentageSliceTypeB = sliceTypeB / totalSlices
-- then to get a readable percentage and not just a decimal,
percentageSliceTypeA *= 100
percentageSliceTypeB *= 100

I’m trying to figure out how much all SliceTypeA’s-slices (combined) takes of the cake, and then also what each SliceTypeA’s-slice takes of their portion of the cake they fill up. (and vice versa for SliceTypeB)

So let’s say that all SliceTypeA’s slices combined takes up 50% of the cake, and there is 10 SliceTypeA’s, then each slice takes up 5%.

OH okay, so you’re looking to find out the percentage of each slice?

local cakeSlicePercentage = { typeA = 0.5, typeB = 0.5 } -- 0.5 is 50%
local numberSlices = { typeA = 5, typeB = 2 }
local percentagePerSliceA = cakeSlicePercentage.typeA / numberSlices.typeA
local percentagePerSliceB = cakeSlicePercentage.typeB / numberSlices.typeB

Or if you know what % of the cake is taken by A and B:

local cakeSlicePercentage = { a = 0.05, b = 0.02 } -- one A slice takes 5%, one B slice takes 2%
local percentageOfCake = { a = 0.5, b = 0.5 }
local numberSlicesA = percentageOfCake.a / cakeSlicePercentage.a
local numberSlicesB = percentageOfCake.b / cakeSlicePercentage.b

He needs to know the number of slices of each type where the sum equals 30 slices total.

This will dictate the size of each sector, or “slice”. The 12 is arbitrary value you assign to this complete “cake” .

1 Like

These are the pre-defined variables, that I have. No matter which one I change, the math should adapt. :slight_smile:

local How_big_is_the_cake				= 	12
local How_big_is_sliceTypeA				= 	 1
local How_big_is_sliceTypeB				= 	 2
local How_many_sliceTypeBs_is_there	= 	20 -- Since there is always 30 slices, "how_many_sliceTypeAs_is_there" will automatically be 10.

The size of each type of slice will be dictated by the number of slices… if you have 30 of A and none of B, that will dictate that each slice size of A will be 12/30.

You could also have 29 slices of A that are tiny and one giant slice of B, or 29 slices of A that are medium size and still one slice of B that is not as giant… and both scenario will equal size 12 total.

1 Like

30 is the total of the slices, so you could divide the total of SliceTypeA slices with it and then times 100 for the percentage. For each slice, just divide the percentage with the total. (this also works with SliceTypeB)

local TotalSliceTypeAPercentage = TotalSliceTypeASlices / 30 * 100
local SliceTypeAPercentage = TotalSliceTypeAPercentage / TotalSliceTypeASlices
--[[
This means if the cake has 12 SliceTypeA slices:
The total covers 40% (which is the TotalSliceTypeAPercentage)
Each slice covers around 3% (which is the SliceTypeAPercentage)
]]

Doesnt this assume the slice sizes are all equal size?

This does not take into consideration that sliceTypeA and sliceTypeB isn’t equal size (slice size).

Edit: instead the approach is to make a totalWeight I assume, and somehow incorporate it.

local WeightSliceTypeA = AmountOfSliceTypeAs * SizeOfSliceTypeAs
local WeightSliceTypeB = AmountOfSliceTypeBs * SizeOfSliceTypeBs
local CombinedWeight = WeightSliceTypeA + WeightSliceTypeB 
1 Like

Okay I get what you mean now. Something like this should work maybe:

local total_slices = 30
local weight_a = 1
local weight_b = 2
local total_weight = weight_a + weight_b

local b_slices = 20
local a_slices = total_slices - b_slices

local percentage_a = a_slices * weight_a / total_weight
local percentage_b = b_slices * weight_b / total_weight

local total_percentage = percentage_a + percentage_b
local fixed_percentage_a = percentage_a / total_percentage -- this will give us a number between 0 and 1
local fixed_percentage_b = percentage_b / total_percentage -- also a number between 0 and 1
print(fixed_percentage_a, fixed_percentage_b) -- fixed a + fixed b = ~1

-- now for the 12:
local partitions = 12 -- probably not the right word but whatever
local partitions_a = fixed_percentage_a * partitions
local partitions_b = fixed_percentage_b * partitions
-- partitions_a + partitions_b should be 12
print(partitions_a, partitions_b) -- a gets 2.4, b gets 9.6
4 Likes

This is closer, i believe still that the 12 value is arbitrary.

1 Like

12 can be changed, any of the variables declared with a number literal can be changed.

I will give it a read-through and adapt it to my script, and see if everything does as it should. :slight_smile:
For now, it looks correct.

1 Like
local amountA = 14
local amountB = 16

local total = amountA * 1 + amountB * 2

local percentA = 1 / total
local percentB = 2 / total

print(percentA * 23) -- 0.5
print(percentB * 23) -- 1

print(percentA * 30) -- 0.6521739130
print(percentB * 30) -- 1.3043478260
1 Like