All these projectiles going in 1 direction - animation

Guys so basicaly i have this pieces of code in different locations(didnt want to post all code here) in Lua . I want to make a game on love2d. and my problem is: my projectiles are always going in 1 direction. i even make projectile.animnumber but still it gets to go in 1 direction. is there any way to split this massive. (I'm a newbie so don't flame me too much)
projectile = {}
projectile.width = 30
projectile.height = 32
projectile.animNumber = 1
function love.keyreleased(key)
if (key == "space") then
shoot()
love.audio.play(magic_shotSND)
if player.animNumber == 1 then
projectile.animNumber = 1
elseif player.animNumber == 2 then
projectile.animNumber = 2
elseif player.animNumber == 3 then
projectile.animNumber = 3
else
projectile.animNumber = 4
end
end
end
-----
for i,v in ipairs(player.shots) do
if projectile.animNumber == 1 then
v.x = v.x + 300 * dt
elseif projectile.animNumber == 2 then
v.x = v.x - 300 * dt
elseif projectile.animNumber == 3 then
v.y = v.y + 300 * dt
else
v.y = v.y - 300 * dt
end
end
----
function shoot()
local shot = {}
shot.x = player.x - 16
shot.y = player.y - 8
table.insert(player.shots, shot)
end
for i,v in ipairs(player.shots) do
love.graphics.draw(skull, v.x, v.y)
end

Replace all of your "projectile.animNumber" to "v.animNumber"
I think the problem is this:
for i,v in ipairs(player.shots) do
if projectile.animNumber == 1 then
v.x = v.x + 300 * dt
elseif projectile.animNumber == 2 then
v.x = v.x - 300 * dt
elseif projectile.animNumber == 3 then
v.y = v.y + 300 * dt
else
v.y = v.y - 300 * dt
end
end
In your for loop you are checking for projectile.animNumber which doesn't appear previously in your code. Therefore, making the else statement true and causing all projectiles to travel in 1 direction.
Sorry if it was confusing; I'm not that good at explaining things

Related

Ruby algorithms loops codewars

I got stuck with below task and spent about 3 hours trying to figure it out.
Task description: A man has a rather old car being worth $2000. He saw a secondhand car being worth $8000. He wants to keep his old car until he can buy the secondhand one.
He thinks he can save $1000 each month but the prices of his old car and of the new one decrease of 1.5 percent per month. Furthermore this percent of loss increases by 0.5 percent at the end of every two months. Our man finds it difficult to make all these calculations.
How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?
My code so far:
def nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth)
dep_value_old = startPriceOld
mth_count = 0
total_savings = 0
dep_value_new = startPriceNew
mth_count_new = 0
while startPriceOld != startPriceNew do
if startPriceOld >= startPriceNew
return mth_count = 0, startPriceOld - startPriceNew
end
dep_value_new = dep_value_new - (dep_value_new * percentLossByMonth / 100)
mth_count_new += 1
if mth_count_new % 2 == 0
dep_value_new = dep_value_new - (dep_value_new * 0.5) / 100
end
dep_value_old = dep_value_old - (dep_value_old * percentLossByMonth / 100)
mth_count += 1
total_savings += savingperMonth
if mth_count % 2 == 0
dep_value_old = dep_value_old - (dep_value_old * 0.5) / 100
end
affordability = total_savings + dep_value_old
if affordability >= dep_value_new
return mth_count, affordability - dep_value_new
end
end
end
print nbMonths(2000, 8000, 1000, 1.5) # Expected result[6, 766])
The data are as follows.
op = 2000.0 # current old car value
np = 8000.0 # current new car price
sv = 1000.0 # annual savings
dr = 0.015 # annual depreciation, both cars (1.5%)
cr = 0.005. # additional depreciation every two years, both cars (0.5%)
After n >= 0 months the man's (let's call him "Rufus") savings plus the value of his car equal
sv*n + op*(1 - n*dr - (cr + 2*cr + 3*cr +...+ (n/2)*cr))
where n/2 is integer division. As
cr + 2*cr + 3*cr +...+ (n/2)*cr = cr*((1+2+..+n)/2) = cr*(1+n/2)*(n/2)
the expression becomes
sv*n + op*(1 - n*dr - cr*(1+(n/2))*(n/2))
Similarly, after n years the cost of the car he wants to purchase will fall to
np * (1 - n*dr - cr*(1+(n/2))*(n/2))
If we set these two expressions equal we obtain the following.
sv*n + op - op*dr*n - op*cr*(n/2) - op*cr*(n/2)**2 =
np - np*dr*n - np*cr*(n/2) - np*cr*(n/2)**2
which reduces to
cr*(np-op)*(n/2)**2 + (sv + dr*(np-op))*n + cr*(np-op)*(n/2) - (np-op) = 0
or
cr*(n/2)**2 + (sv/(np-op) + dr)*n + cr*(n/2) - 1 = 0
If we momentarily treat (n/2) as a float division, this expression reduces to a quadratic.
(cr/4)*n**2 + (sv/(np-op) + dr + cr/2)*n - 1 = 0
= a*n**2 + b*n + c = 0
where
a = cr/4 = 0.005/4 = 0.00125
b = sv/(np-op) + dr + cr/(2*a) = 1000.0/(8000-2000) + 0.015 + 0.005/2 = 0.18417
c = -1
Incidentally, Rufus doesn't have a computer, but he does have an HP 12c calculator his grandfather gave him when he was a kid, which is perfectly adequate for these simple calculations.
The roots are computed as follows.
(-b + Math.sqrt(b**2 - 4*a*c))/(2*a) #=> 5.24
(-b - Math.sqrt(b**2 - 4*a*c))/(2*a) #=> -152.58
It appears that Rufus can purchase the new vehicle (if it's still for sale) in six years. Had we been able able to solve the above equation for n/2 using integer division it might have turned out that Rufus would have had to wait longer. That’s because for a given n both cars would have depreciated less (or at least not not more), and because the car to be purchased is more expensive than the current car, the difference in values would be greater than that obtained with the float approximation for 1/n. We need to check that, however. After n years, Rufus' savings and the value of his beater will equal
sv*n + op*(1 - dr*n - cr*(1+(n/2))*(n/2))
= 1000*n + 2000*(1 - 0.015*n - 0.005*(1+(n/2))*(n/2))
For n = 6 this equals
1000*6 + 2000*(1 - 0.015*6 - 0.005*(1+(6/2))*(6/2))
= 1000*6 + 2000*(1 - 0.015*6 - 0.005*(1+3)*3)
= 1000*6 + 2000*0.85
= 7700
The cost of Rufus' dream car after n years will be
np * (1 - dr*n - cr*(1+(n/2))*(n/2))
= 8000 * (1 - 0.015*n - 0.005*(1+(n/2))*(n/2))
For n=6 this becomes
8000 * (1 - 0.015*6 - 0.005*(1+(6/2))*(6/2))
= 8000*0.85
= 6800
(Notice that the factor 0.85 is the same in both calculations.)
Yes, Rufus will be able to buy the car in 6 years.
def nbMonths(old, new, savings, percent)
percent = percent.fdiv(100)
current_savings = 0
months = 0
loop do
break if current_savings + old >= new
current_savings += savings
old -= old * percent
new -= new * percent
months += 1
percent += 0.005 if months.odd?
end
[months, (current_savings + old - new).round]
end

Best way to handle multiple checks ruby

So I'm trying to write a converter for HSL to RGB (and eventually into hex)
I'm following this colorspace conversion theory and I seem to be stuck on step 6
Now we need to do up to 3 tests to select the correct formula for each
color channel. Let’s start with Red.
test 1 – If 6 x temporary_R is smaller then 1, Red = temporary_2 +
(temporary_1 – temporary_2) x 6 x temporary_R In the case the first
test is larger then 1 check the following
test 2 – If 2 x temporary_R is smaller then 1, Red = temporary_1 In
the case the second test also is larger then 1 do the following
test 3 – If 3 x temporary_R is smaller then 2, Red = temporary_2 +
(temporary_1 – temporary_2) x (0.666 – temporary_R) x 6 In the case
the third test also is larger then 2 you do the following
Red = temporary_2
Ok lets do it for our Red value
6 x temporary_R = 6 x 0.869 = 5.214, so it’s larger then 1, we need to
do test 2 2 x temporary_R = 2 x 0.869 = 1.738, it’s also larger then
1, we need to do test 3 3 x temporary_R = 3 x 0.869 = 2.607, it’s
larger then 2, so we go for the last formula Red = temporary_2 =
0.0924 which rounded down is 0.09, which is a number we recognize from the RGB to HSL conversion
So far I've monkey patched a function to take my HSL colours
def toRGB(hue, sat, lum)
temp_1 =
case lum
when lum < 0.0
lum x (1.0 * sat)
when lum > 0.0
(lum + sat) - lum
end
temp_2 = (2 * lum) - temp_1.to_f
h = (hue/360.0).round(4)
temp_r = (h + 0.333).round(4)
temp_r = temp_r + 1 if temp_r < 0
temp_r = temp_r - 1 if temp_r > 1
temp_g = h
temp_b = (h - 0.333).round(4)
temp_b = temp_b + 1 if temp_b < 0
temp_b = temp_b - 1 if temp_b > 1
red =
#test 1
#test 2
#test 3
"#{red}"
end
I was trying to use a case statement
red =
case temp_r
when 6 * temp_r < 1
temp_2 + (temp_1 - temp_2) * 6 * temp_r
when 2 * temp_r < 1
temp_1
when 3 * temp_r < 2
temp_2 + (temp_1 - temp_2) * (0.666 - temp_r * 6)
end
but then I started re-reading the instructions and now I can't really see a way to do what I need in ruby. Maybe I'm over-thinking it.
If you want to see the rest of my code in context you can see it here
There are two things I noticed in your code snippet:
You are mixing two types of switch case statement in ruby.
case a
when 1..5
"It's between 1 and 5"
when 6
"It's 6"
end
case
when a > 1 && a < 5
"It's between 1 and 5"
when a == 6
"It's 6"
end
See a difference, in first case when you directly compare cases, you've to mention the variable name you're comparing with, while in second case you're comparing with true & false condition by explicitly putting a condition on your variable so you don't need to put it next to case.
The second thing is as per your condition statement, you'll need an else case in your case statement.
red =
case
when 6 * temp_r < 1
temp_2 + (temp_1 - temp_2) * 6 * temp_r
when 2 * temp_r < 1
temp_1
when 3 * temp_r < 2
temp_2 + (temp_1 - temp_2) * (0.666 - temp_r * 6)
else
temp_2
end

Algorithm for a Program which can convert Roman to Decimal

I need some help regarding algorithm, I have attached a problem in it which says that i have to develop an algorithm for a program which can convert Roman to Decimal.
I don't know how to do it, would prefer a complete algorithm but any help is appreciated.
It is an algorithm not a program!
The idea is to traverse the roman numeral string from end to start. For each character, add the corresponding number to the result. For some special
cases, such as ‘I’, ‘X’, ‘C’, check if the result is greater than the corresponding number. If yes, we need to subtract the number
from the result. If no, we need to add the number to the result.
res = 0
for each character c backwards:
if c =='I'
res += res + (res >= 5 ? -1 : 1)
else if c=='V'
res += res + 5
else if c=='X'
res += 10 * (res >= 50 ? -1 : 1)
else if c=='L'
res += 50;
else if c == 'C'
res += 100 * (res >= 500 ? -1 : 1)
else if c== 'D'
res += 500
else if c=='M'
res += 1000
return res
For example: MMDCCCXCIX
= 0 + MMDCCCXCIX
= 10 + MMDCCCXCI
= 9 + MMDCCCXC (as 10 > 5)
= 109 + MMDCCCX
= 99 + MMDCCC (as 109 > 50)
= 199 + MMDCC
= 299 + MMDC
= 399 + MMD
= 899 + MM
= 1899 + M
= 2899

Colour Difference DeltaE 2000

I am trying to Calculate the CIE Colour Difference DeltaE 2000 based on DE2000 Formula. I have done as per the formula provided in the website, but I am getting strange delta E values. I am confused where I have gone wrong. I have checked manytimes but I am not able to find the mistake.Can someone tell me which part of my code has problem.
function DE_2K = CIEDE2000(Lab1,Lab2)
labuno=Lab1
labdos=Lab2
L1=labuno(1)
a1=labuno(2)
b1=labuno(3)
L2=labdos(1)
a2=labdos(2)
b2=labdos(3)
%*******************************************************************
% Definition for CIE DE2000
%*******************************************************************
L_bar_dash=(L1+L2)/2;
C1 = sqrt((a1)^2+(b1)^2)
C2 = sqrt((a2)^2+(b2)^2)
C_bar = (C1+C2)/2
G = (1 -sqrt(((C_bar)^7)/((C_bar)^7+(25)^7))/2)
a1_dash = a1*(1+G)
a2_dash = a2*(1+G)
C1_dash = sqrt((a1_dash)^2+(b1)^2)
C2_dash = sqrt((a2_dash)^2+(b2)^2)
C_bar_dash = (C1_dash + C2_dash)/2
if (radtodeg(atan(b1/a1_dash)) >= 0 ) h1_dash = radtodeg(atan(b1/a1_dash))
else h1_dash = radtodeg(atan(b1/a1_dash)) + radtodeg(2*pi)
end
if (radtodeg(atan(b2/a2_dash)) >= 0 ) h2_dash = radtodeg(atan(b2/a2_dash))
else h2_dash = radtodeg(atan(b2/a2_dash)) + radtodeg(2*pi)
end
if ((h1_dash - h2_dash) > radtodeg(pi)) H_bar_dash = (h1_dash + h2_dash + radtodeg(2*pi))/2
else H_bar_dash = (h1_dash + h2_dash)/2
end
T = 1 - 0.17*radtodeg(cos(H_bar_dash-radtodeg(pi/6)))+0.24*radtodeg(cos(2*H_bar_dash))+0.32*radtodeg(cos(3*H_bar_dash + radtodeg(pi/30)))- 0.20*radtodeg(cos(4*H_bar_dash + 63))
if ((abs(h2_dash - h1_dash)) <= radtodeg(pi)) DE_h_dash = h2_dash - h1_dash
elseif(abs(h2_dash - h1_dash) > radtodeg(pi) && h2_dash <= h1_dash) DE_h_dash = h2_dash - h1_dash + radtodeg(2*pi)
else DE_h_dash = h2_dash - h1_dash - radtodeg(2*pi)
end
DE_L_dash = L2 - L1
DE_C_dash = C2_dash - C1_dash
DE_H_dash = 2 * sqrt(C1_dash * C2_dash) * radtodeg(sin(DE_h_dash/2))
S_L = 1 + ((0.015 * (L_bar_dash - 50)^2)/sqrt(20 + (L_bar_dash - 50)^2))
S_C = 1 + (0.045 * C_bar_dash)
S_H = 1 + (0.015 * C_bar_dash * T)
DE_angle = 30 * exp( - ((H_bar_dash - 275)/25)^2)
R_C = 2 * sqrt((C_bar_dash)^7/((C_bar_dash)^7 + (25)^7))
R_T = - R_C * radtodeg(sin(2 * DE_angle))
K_L = 1
K_C = 1
K_H = 1
DE_2K = sqrt( (DE_L_dash/(K_L * S_L))^2 + (DE_C_dash/(K_C * S_C))^2 + (DE_H_dash/(K_H * S_H))^2 + (R_T * (DE_C_dash/(K_C * S_C)) * (DE_H_dash/(K_H * S_H))))
end
There are some problems in your calculations:
a) if ((h1_dash - h2_dash) > radtodeg(pi)) : don't you need to take the abs of this?
b) 20*radtodeg(cos(4*H_bar_dash + 63) : you need -63 here
c) I assume your if-else structure correctly handles the three cases; you may need to check that:
....else DE_h_dash = h2_dash - h1_dash - radtodeg(2*pi)
d) sin is a number not in degrees, not in radians so no need to convert here:
radtodeg(sin(DE_h_dash/2))
e) same here: radtodeg(sin(2 * DE_angle))
f) I assume cos/sin take degrees; you many need to double check what is degrees what is radians everywhere.

How to accelerate matlab code?

I'm using matlab to implement a multilayer neural network. In the code I represent
the value of each node AS netValue{k}
the weight between layer k and k + 1 AS weight{k}
etc.
Since these data is three-dimensional, I have to use cell to hold a 2-D matrix to enable matrix multiply.
So it becomes really really slow to train the model, which I expect to have resulted from the usage of cell.
Can anyone tell me how to accelerate this code? Thanks
clc;
close all;
clear all;
input = [-2 : 0.4 : 2;-2:0.4:2];
ican = 4;
depth = 4; % total layer - 1, by convension
[featureNum , sampleNum] = size(input);
levelNum(1) = featureNum;
levelNum(2) = 5;
levelNum(3) = 5;
levelNum(4) = 5;
levelNum(5) = 2;
weight = cell(0);
for k = 1 : depth
weight{k} = rand(levelNum(k+1), levelNum(k)) - 2 * rand(levelNum(k+1) , levelNum(k));
threshold{k} = rand(levelNum(k+1) , 1) - 2 * rand(levelNum(k+1) , 1);
end
runCount = 0;
sumMSE = 1; % init MSE
minError = 1e-5;
afa = 0.1; % step of "gradient ascendence"
% training loop
while(runCount < 100000 & sumMSE > minError)
sumMSE = 0; % sum of MSE
for i = 1 : sampleNum % sample loop
netValue{1} = input(:,i);
for k = 2 : depth
netValue{k} = weight{k-1} * netValue{k-1} + threshold{k-1}; %calculate each layer
netValue{k} = 1 ./ (1 + exp(-netValue{k})); %apply logistic function
end
netValue{depth+1} = weight{depth} * netValue{depth} + threshold{depth}; %output layer
e = 1 + sin((pi / 4) * ican * netValue{1}) - netValue{depth + 1}; %calc error
assistS{depth} = diag(ones(size(netValue{depth+1})));
s{depth} = -2 * assistS{depth} * e;
for k = depth - 1 : -1 : 1
assistS{k} = diag((1-netValue{k+1}).*netValue{k+1});
s{k} = assistS{k} * weight{k+1}' * s{k+1};
end
for k = 1 : depth
weight{k} = weight{k} - afa * s{k} * netValue{k}';
threshold{k} = threshold{k} - afa * s{k};
end
sumMSE = sumMSE + e' * e;
end
sumMSE = sqrt(sumMSE) / sampleNum;
runCount = runCount + 1;
end
x = [-2 : 0.1 : 2;-2:0.1:2];
y = zeros(size(x));
z = 1 + sin((pi / 4) * ican .* x);
% test
for i = 1 : length(x)
netValue{1} = x(:,i);
for k = 2 : depth
netValue{k} = weight{k-1} * netValue{k-1} + threshold{k-1};
netValue{k} = 1 ./ ( 1 + exp(-netValue{k}));
end
y(:, i) = weight{depth} * netValue{depth} + threshold{depth};
end
plot(x(1,:) , y(1,:) , 'r');
hold on;
plot(x(1,:) , z(1,:) , 'g');
hold off;
Have you used the profiler to find out what functions are actually slowing down your code? It shows what lines take the most time to execute.

Resources