Math problem trying to make a progressbar in project zomboid - math

I am making a mod for project Zomboid and I cant seem to figure out a math problem, so the value I am getting ranges from 0 to 1 and I want my progress bar to start at the max width and then descend as the value is increasing.
The first one was easy I got a value between a 100 and 0 so how do this with a value starting at 0?
I tried searching this on google but I am really bad at math and could not find an answer.
function panel:render()
self:drawRectBorder(30, 30, self:getWidth() - 1, 50, 1.0, 1.0, 1.0, 1.0);
--print((bt_core.player:getBodyDamage():getOverallBodyHealth() / 100) * self:getWidth());
self:drawRect(31, 31, (bt_core.player:getBodyDamage():getOverallBodyHealth() / 100) * self:getWidth() - 3, 48, 1, 0, 0, 1);
self:drawRectBorder(30, 110, self:getWidth() - 1, 50, 1.0, 1.0, 1.0, 1.0);
print(bt_core.player:getStats():getFatigue());
if bt_core.player:getStats():getFatigue() == 0 then
self:drawRect(31, 111, self:getWidth() - 3 , 48, 1, 0, 0, 1);
else
self:drawRect(32, 111,bt_core.player:getStats():getFatigue() / (self:getWidth() - 3) , 48, 1, 0, 0, 1);
end
end

To get variable in range 100..0 from variable in range 0..1, you can use y = 100 - x*100

So you have a value 0..1 and you want to map it to 100..0.
Multiplying your value with 100 gives you 0..100.
To invert this you subtract that from 100. 100-0 is 100, 100-100 is 0...
local newVal = 100 - val * 100
or
local newVal = 100 * (1-val)

Related

R - Create new variable using the difference between lagged values

The problem: I need to create a new variable (eventWindowTime) in R that is based on data from two columns - obstacle present (1=yes,0=no) and timeOnTask (assessed continuously).
The dataset: I have data that was continuously collected (to the fractional seconds) from several participants as they performed a task. At various points, participants encountered one or more obstacles. I would like to create obstacle event windows that range from -5s (5 s before the obstacle) to +20s (20 s after the obstacle).
Additional challenges:
Some event windows are overlapping
Some timestamps have multiple measurements (so I can't rep values -5 to 20 relative to the first obstaclePresent == 1)
Things I've tried:
The way I would typically approach this is to use ifelse or case_when functions with lag() to:
set eventWindowTime to 0 when obstaclePresent == 1 && lag(obstaclePresent == 0)
set eventWindow Time to increment the lagged eventWindowTime value by the difference in timeOnTask values across the two rows when obstaclePresent == 1 && lag(obstaclePresent == 1).
then backfill the negative seconds in a second step.
However, R does not seem to hold the lagged values in memory and I keep getting a "Error in vec_slice():
! x must be a vector, not NULL." error.
Here's a small subset of code and a file which can be used to reproduce the problem:
mre <- data.frame(Sub = rep(1, 41), Time = c(723.2, 723.2, 723.3, 723.3, 723.3, 723.4, 723.4, 723.5, 723.5, 723.6, 723.6, 723.6, 723.7, 723.7, 723.7, 723.8, 723.9, 723.9, 723.9, 724, 724, 724, 724, 724.1, 724.1, 724.2, 724.2, 724.2, 724.3, 724.3, 724.3, 724.4, 724.4, 724.5, 724.5, 724.6, 724.6, 724.6, 724.7, 724.7, 724.8), obstaclePresent = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
mre$obstacleEventWindow <- case_when(
mre$obstaclePresent != lag(mre$obstaclePresent,1) & mre$obstaclePresent == 0 ~ 0,
mre$obstaclePresent == lag(mre$obstaclePresent,1) ~ lag(mre$obstacleEventWindow) + mre$newTime - lag(mre$newTime,1),
TRUE ~ 0
)
To be clear, I understand that the case_when() statement is self-referencing. I've worked with other programs where a column is populated on the fly, and you can reference lagged cells without issue. That isn't working here, but I'm at a loss with respect to what to do instead.

Solving for possible combinations of variables that add up to a certain number or range of numbers

Good evening all, this is my first question and I am hoping someone on here might be able to at least point me in a direction.
I am trying to figure out how to optimally stack pallets in a new storage facility. I need to configure the racking ahead of time in order to accept different sized pallets.
I am thinking of using between 3-6 different pallet height openings, say 105", 100", 84", 78", 72" and 66".
What I need to do is figure out every possible combination of these pallet heights that will have the top of the top beam at, say, 439".
An example of a combination would be (1) 105" pallet, (1) 100" pallet and (3) 78" pallets.
Another example would be (1) 105" pallet, (1) 100" pallet, (1) 84" pallet, (1) 78" pallet and (1) 72" pallet.
Obviously there are a number of these combinations...and I need to find them all.
I'm wondering if this is possible with excel? I just discovered "Solver" but haven't quite figured it out yet.
Any input would be greatly appreciated. I am kind of running in circles here...
Using a bit of Python and the constraint solver https://pypi.org/project/python-constraint/:
import constraint
h = [105, 100, 84, 78, 72] # heights
total = 439
n = len(h) # number of different heights
# max number of pallets that can fit
Max = int(max([total/h[i] for i in range(n)]))
problem = constraint.Problem()
problem.addVariables( [f"h{j}" for j in h], range(Max+1) )
problem.addConstraint(constraint.MaxSumConstraint(total,h))
s = problem.getSolutions()
print(f"number of solutions:{len(s)}")
print(s)
Output:
number of solutions:194
[{'h100': 4, 'h105': 0, 'h72': 0, 'h78': 0, 'h84': 0},
{'h100': 3, 'h105': 1, 'h72': 0, 'h78': 0, 'h84': 0},
{'h100': 3, 'h105': 0, 'h72': 1, 'h78': 0, 'h84': 0},
...
{'h100': 0, 'h105': 0, 'h78': 0, 'h84': 0, 'h72': 1},
{'h100': 0, 'h105': 0, 'h78': 0, 'h84': 0, 'h72': 0}]

How to draw frequency separation in a parametric equalizer

I'm trying to do something similar to this parametric equalizer, in regards to the frequency axis only, i.e. the values along the middle line:
This appears to be the standard format for equalizers but I can't work out the formula to do this.
i.e. The values for the first set of frequency lines are 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
but the spacing reduces as you go up to 100
From there, it goes 100, 200, 300, 400, 500.. to 1000 and the spacing resets at 100 then reduces at each interval up to 1000
The same pattern repeats to the max, which in this case is 20,000
How is this done? Is it logarithmic?
With the help of this video I was able to work out a formula to plot the frequency axis as a logarithmic scale.
int factor = 10;
for(int i = 10; i <= FREQ_MAX; i+=factor)
{
fx = (float) ((float) (Math.log10(i) - Math.log10(PEQ.FREQ_MIN))/(Math.log10(PEQ.FREQ_MAX)-Math.log10(PEQ.FREQ_MIN)) * getMaxCanvasWidth());
canvas.drawLine(fx, 0, fx, getHeight(), paintLinesThick);
if(isDisplayedFreq(i))
{
paintText.setTextAlign(Paint.Align.LEFT);
canvas.drawText(getFreqAsFormattedLabel(i), fx + (getMaxCanvasWidth() / 120f), (getHeight() / 2f) + (getHeight() / 50f), paintText);
}
if(i >= (factor*10))
{
factor *= 10;
}
}

Threejs - How to offset all points on a 2d geometry by distance

Using Three.js, (although I believe this is more math related) I have a set of 2D points that can create a 2D geometry. such as square, rectangle, pentagon, or custom 2D shape. Based of the original 2D shape, I would like to create a method to offset the points inward or outward uniformly in such a way like the attached image.
I don't know if there is a simple way to offset/grow/shrink all the points (vector3) uniformly on the 2D shape inward or outward. And if so, it'll be cool if I can offset the points by X distance? Kinda of like saying offset the points on the 2D shape outward or inward by X distance.
And no, I'm not referring to scaling from a center point. While scaling may work for symmetrical shapes, it won't work when it comes to non-symmetrical shapes.
see image for example
Thanks in advance.
You can read that forum thread.
I've made some changes with ProfiledContourGeometry and got OffsetContour, so I leave it here, just in case, what if it helps :)
function OffsetContour(offset, contour) {
let result = [];
offset = new THREE.BufferAttribute(new Float32Array([offset, 0, 0]), 3);
console.log("offset", offset);
for (let i = 0; i < contour.length; i++) {
let v1 = new THREE.Vector2().subVectors(contour[i - 1 < 0 ? contour.length - 1 : i - 1], contour[i]);
let v2 = new THREE.Vector2().subVectors(contour[i + 1 == contour.length ? 0 : i + 1], contour[i]);
let angle = v2.angle() - v1.angle();
let halfAngle = angle * 0.5;
let hA = halfAngle;
let tA = v2.angle() + Math.PI * 0.5;
let shift = Math.tan(hA - Math.PI * 0.5);
let shiftMatrix = new THREE.Matrix4().set(
1, 0, 0, 0,
-shift, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
let tempAngle = tA;
let rotationMatrix = new THREE.Matrix4().set(
Math.cos(tempAngle), -Math.sin(tempAngle), 0, 0,
Math.sin(tempAngle), Math.cos(tempAngle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
let translationMatrix = new THREE.Matrix4().set(
1, 0, 0, contour[i].x,
0, 1, 0, contour[i].y,
0, 0, 1, 0,
0, 0, 0, 1,
);
let cloneOffset = offset.clone();
console.log("cloneOffset", cloneOffset);
shiftMatrix.applyToBufferAttribute(cloneOffset);
rotationMatrix.applyToBufferAttribute(cloneOffset);
translationMatrix.applyToBufferAttribute(cloneOffset);
result.push(new THREE.Vector2(cloneOffset.getX(0), cloneOffset.getY(0)));
}
return result;
}
Feel free to modify it :)
I have some doubts about solutions that do not include number of edges modification.
I faced the same issue in this project where I wanted to ensure a known distance between voronoi cells, and I quickly figured out that scale does not fulfill the use case. But one complication I faced was the disappearance of some edges that I had to handle in a while loop. It was so difficult to debug that I had to create a debug mode that helps see the points and lines, that I also left available. It's possible to activate this debug mode with a checkbox:
Note for the images, I have them as links not embedded as I'm still new contributor (might improve that later).
The edges that shall disappear are shown in red
retraction snapshot1
retraction with edges discard 1
retraction with edges discard 2
Here a link to the function in action, you might have to modify it to have another points format though :
https://github.com/WebSVG/voronoi/blob/8893768e3929ea713a47dba2c4d273b775e0bd82/src/voronoi_diag.js#L278
And here a link to the complete project integrating this function, it has link to a live demo too
https://github.com/WebSVG/voronoi

unknown recursive method, must find how it runs

This was a past exam question and I have no idea what it does! Please can someone run through it.
public static int befuddle(int n){
if(n <= 1){
return n;
}else{
return befuddle(n - 1) * befuddle(n - 2) + 1;
}
}
this is computing the sequence: 0, 1, 1, 2, 3, 7, 22, 155, ...
Which can be expressed using this formula:
when dealing with numerical sequences, a great resources is The Online Encyclopedia of Integer Sequences!, a quick search there shows a similar sequence to yours but with:
giving the following sequence: 0, 0, 1, 1, 2, 3, 7, 22, 155, ...
you can find more about it here
public static is the type of member function it is. I'm assuming this is part of a class? The static keyword allows you to use it without creating an instance of the class.
Plug in a value of 'n' and step through it. For instance, if n = 1, then the function returns 1. If n = 0 -> 0; n = -100 -> -100.
If n = 2, the else branch is triggered and befuddled is called with 1 and 0. So n = 2 returns 0*1 + 1 = 1.
Do the same thing for n = 3, etc. (calls n = 2 -> 1, and n = 1 -> 1, so n=3 -> 1*1+1 = 2.)

Resources