Unclear progress bar logic - ruby-on-rails

My progress bar:
https://imgur.com/M6S3xkA
The problem:
How you can see, this is not a regular progress bar, the logic is unclear for me.
Let's say - when you have ~30 points (from 1000), you should have 50% in progress bar,
but how to count this magic number?
In normal case with 30 points (from 1000) you should have around <5%.
I will happy for any clues!

You should calculate the progress based on PARTS, which you split. In your case, you split range to 5 parts.
Step 1: Find which part the current point are in. (if point is 30, it is over 2 parts - (0..3) and (3..20))
Step 2: Find which percentage of the point in current part (if point is 30, current part is (20..50) -> percentage = (30 - 20) / (50 - 20) = 0.333
Step 3: Combine them and divide the parts number.
Sample code:
PARTS = [(0..3), (3..20), (20..50), (50..200), (200..1000)]
PARTS_NUMBER = PARTS.size # 5
def calculate_percentage point
part_index = PARTS.find_index {|x| x.include?(point)}
part = PARTS[part_index]
remain = (point - part.first) * 1.0 / (part.last - part.first)
(part_index + remain) / PARTS_NUMBER
end
calculate_percentage(30) # => 0.4666666666666667
Hope it helps :D

Related

Manim:why is my graph plotted in half the animation time?

I have this simple script, where I would like the ValueTracker (displayed as a number on the right) to evolve at the same speed with the graph. Instead the graph plotting finishes in 5 seconds. Do you know why and how to fix it?
from manim import *
class TrackerExample(Scene):
def sin(self,x):
f = 1 / 50
return np.sin(x * 2 * math.pi * f) / 2 + 0.5
def construct(self):
axes= Axes(x_range=[0, 749, 100],
y_range=[0, 4, 2],
axis_config={"include_numbers": True, "font_size": 24},
x_axis_config={},
x_length=4,
y_length=3
).move_to(LEFT * 2)
self.play(Write(axes, lag_ratio=0.01, run_time=1))
tracker = ValueTracker(0)
sin_graph = axes.plot(lambda x: self.sin(x), color=BLUE)
numbers = DecimalNumber(0).move_to(2*RIGHT)
numbers.add_updater(lambda m: m.set_value(int(tracker.get_value())))
self.add(numbers)
self.play(tracker.animate.set_value(749), Write(sin_graph), run_time=10,
rate_func=linear)
example = TrackerExample()
example.construct()
I am using the community version of manim, 0.16.0.post0
This is due to your choice of Animation: Write draws, in the first 50% of the animation run time, the stroke of the mobject and then fills it in the remaining 50% of the run time. Replace Write with Create, and it will look as expected.
And by the way, if you want to render your scene directly from the script, use the render method, not construct:
example = TrackerExample()
example.render()

Distribute 4 SKSpriteNodes along SkScene Width programmatically

I know scene coordinates are (0,0) at center so how can i distribute 4 SKNodes along the width using a for loop
for index 1...4 {
let node = SKNode()
node.postion = evenly distribute along the scene width. who can i do it?
}
You could use the width of the scene divided by the number of nodes minus 1. This would then be multiplied by the index minus 1, then added to the minimum x value of your scene.
let widthRange = scene.frame.maxX - scene.frame.minX
for index 1...4 {
let node = SKNode()
node.postion = scene.frame.minX + widthRange/(4-1) * (index-1)
}
This should place each node an equal distance away from each other along the width of the scene, starting from each edge.
If your index for the for loop doesn't start at 1, the code would need to be modified to something like this:
let widthRange = scene.frame.maxX - scene.frame.minX
var counter = 0
for index 352...356 {
let node = SKNode()
node.postion = scene.frame.minX + widthRange/(4-1) * counter
counter+=1
}
If you're unsure of which version to use, use the second version, as it's less reliant on the format of the for loop (as long as you reset counter to 0 before every time you use this).
If you need any clarifications as to what does what, feel free to ask.
Edit
This should clarify what the code actually does.
let widthRange = scene.frame.maxX - scene.frame.minX is used to get the size of the scene, then set it to a variable that is used later. Making this a variable is sort of optional as you could just use the scene size, however I put it this way so it's less messy.
var counter = 0 simply makes a counter variable, that's used in the for-loop, to place the nodes. Make sure to set it to 0 before every time you run the loop. counter+=1 is then used later so each node is multiplied by an increasing value, which creates the different x-positions. (I'll get into that later)
node.postion = scene.frame.minX + widthRange/(4-1) * counter is the complicated line. First, I use scene.frame.minX to start placement from the lowest width/x-position. widthRange/(4-1) is a little harder to understand. It uses the scenes width (from earlier), then divides by one less than the amount of nodes. The best way to understand this, is that to cut a rectangle (only in a single direction) so it has 2 cuts through it (2 of the nodes), and 2 edges (the other 2 nodes), you would need 3 sections. (The rectangle would be cut into 3). This gets the distance between each node. The pattern of #ofNodes-1 follows for every amount of nodes. The * counter part is the part that makes the node's x-position actually change. Since every time the loop is gone through, counter increases by 1 (starting at 0) the amount of "sections" from before (widthRange/(4-1)) is increased by 1.

Speed up game after every 50 points

I am making a 2D reaction game with sprite-kit and I am having the same problem again. I already asked this question once and had good answers and everything worked finde but now I am stuck again.
Like the title says I want to speed up my game every 50 points, but it just speeds up when I get the right point number like 50, 100, 150.. The problem is that I have combo points and its always some points more. For example from 48 to 51 points, so it never speeds up. How can I speed up the game even its some points more ? I mean from 50 to 100 one speed up and from 100 to 150 and so on. Here is my code so far:
if (points % 10 == 0) {
if (readyToSpeed) {
speed++;
NSLog(#"speed up");
readyToSpeed = NO;
}
}
Thanks for the help ! (code in objective-c please)
EDIT: Works perfectly using both answers combined.
Instead of incrementing your speed you could do it like so:
speed = baseSpeed + (points / 50);
Where baseSpeed is the speed at the start of the game.
Don't worry about the exact 50x multiple points, keep track of the current point "group" via modulo to derive your speed value
cur_speed = points - (points % 50);
e.g. if they're at 203 points, then cur_speed is 203-(203%50) -> 203-3 -> 200. If they suddenly jump to 308 pts because they hit an insane points combo, then the speed becomes 308-(308%50) -> 308-8 -> 300
if you want just between 50-100 and 100-150 do something like this:
If points > 50 && points < 100 {
//speedup code
}else if points > 100 && points < 150 {
//speedup code
} //etc
Update: if you want this to be endless do this:
var speednumber = points/50
speednumber = speednumber-decimal //some code to cut of the decimals
setSpeedTo(speednumber+1) //you got to make a function for this
setSpeedTo(1) will be normal
2 will be from 50-100
3 100-150
Etc.

How to round a double to the nearest hundredth in Swift?

I am making an app, which takes one number usually something like 10.00 or 100.00 and multiplies it by 0.15 or 0.20 and outputs it to a Label.
What ends up happening in some situations is the answer comes out to be 2.3234342, notice all the numbers after the decimal, which I don't want.
I want Swift automatically round up to the nearest hundredth or keep hundredth the same and then delete everything after the hundredth.
I want the code to automatically determine whether it will round up or keep the number the same.
You can use round() with a "scale factor" of 1000:
let x = 14.14910001
let y = round(1000.0 * x) / 1000.0
println(y)
May be it will help you.
someFloat+=0.005
label.Text = String(format: "a float number: %.02f ", someFloat))

Need help in using turtle module

So I'm writing a program using the turtle module. I have 2 questions:
I have 4 turtles. How can I make their names show up on the screen while drawing?
After I finish drawing, how can I exit one screen and open another within the same program?
I have 4 turtles. How can I make their names show up on the screen
while drawing?
Sure, here's a crude example:
from random import choice
from turtle import Turtle, Screen
NAMES = ['Donnie', 'Raph', 'Mickey', 'Leo']
SPEEDS = ["slowest", "slow", "normal", "fast", "fastest"]
FONT = ("Arial", 12, "normal")
MAGNIFICATION = 5
STAMP_SIZE = 20
MAXIMUM_SPEED = 10
LINE_OFFSET = 50
screen = Screen()
WINDOW_WIDTH = screen.window_width()
START, FINISH = LINE_OFFSET - WINDOW_WIDTH//2, WINDOW_WIDTH//2 - (LINE_OFFSET + MAXIMUM_SPEED)
turtles = {name: Turtle(shape='turtle') for name in NAMES}
for offset, turtle in enumerate(turtles.values(), start=-len(NAMES)//2):
turtle.turtlesize(MAGNIFICATION)
turtle.color("black", "white")
turtle.penup()
turtle.goto(START, offset * MAGNIFICATION * STAMP_SIZE)
turtle.speed(choice(SPEEDS))
turtle.write("", font=FONT) # dummy write for 1st undo
winner = False
while not winner:
for name, turtle in turtles.items():
turtle.undo() # unwrite name
turtle.forward(turtle.speed() + 1)
turtle.write(name, font=FONT) # rewrite name
if turtle.xcor() >= FINISH:
winner = True
break
screen.exitonclick()
To do better with respect to text centering and eliminating flicker, we would need one or more extra invisible turtles to just handle the text. However, when the turtles turn, the text won't turn with them, it'll always be horizontal.

Resources