How to set correct zoomScaleSensitivity for svgpanzoom library? - svgpanzoom

I am using svgpanzoom library (link on github repository) in my project . I was setting some initial value:
var minValue = 0.8;
var maxValue = 6;
var numberOfSteps = 5;
instance.setMinZoom(minValue);
instance.setMaxZoom(maxValue);
After that i was trying calculate and set new value for zoomScaleSensitivity:
var newValueForZoomScale = (maxValue - minValue) / numberOfSteps;
instance.setZoomScaleSensitivity(newValueForZoomScale);
Unfortunately , in this case i have 6 or 7 zoom's steps (depends of screen resolution). My question is how i can set correct value for zoomScaleSensitivity and always have only 5 (numberOfSteps) zoom's steps?

Try
var newValueForZoomScale = (maxValue - minValue) / (numberOfSteps - 1);
That's because if you divide by N, you'll get N+1 steps like:
if start is 1
if end is 6
divide by 5 (number of steps you want) and get (6-1)/5 = 1 for step distance
you'll get 6 steps: 1, 2, 3, 4, 5, 6
So you have to divide by 4, then you'll get (6-1)/4 = 1.25 and 5 steps: 1, 2.25, 3.5, 4.75, 6

Related

SSRS SWITCH showing error rather than answer

I am trying to do a simple calculation for my data. When the Order value = 6, then subtract the values of order = 1 - 2 -3. It returns #error instead of the the answer. How can i make it work correctly?
= SWITCH(
Fields!Order.Value = 6,
Sum(IIF( Fields!Order.Value = 1 ,CDBL(Fields!Totals.Value),CDBL(0)))
- Sum(IIF( Fields!Ordering.Value = 2 ,CDBL(Fields!Totals.Value),CDBL(0)))
- Sum(IIF( Fields!Ordering.Value = 3 ,CDBL(Fields!Totals.Value),CDBL(0)))
,
1 = 1,Sum(Fields!Totals.Value)
)

Is there a convienient way to call a function multiple times without a loop?

I am currently making some code to randomly generate a set of random dates and assigning them to a matrix. I wish to randomly generate N amount of dates (days and months) and display them in a Nx2 matrix. My code is as follows
function dates = dategen(N)
month = randi(12);
if ismember(month,[1 3 5 7 8 10 12])
day = randi(31);
dates = [day, month];
elseif ismember(month,[4 6 9 11])
day = randi(30);
dates = [day, month];
else
day = randi(28);
dates = [day, month];
end
end
For example if I called on the function, as
output = dategen(3)
I would expect 3 dates in a 2x3 matrix. However, I am unsure how to do this. I believe I need to include N into the function somewhere but I'm not sure where or how.
Any help is greatly appreciated.
You can do it using logical indexing as follows:
function dates = dategen(N)
months = randi(12, 1, N);
days = NaN(size(months)); % preallocate
ind = ismember(months, [1 3 5 7 8 10 12]);
days(ind) = randi(31, 1, sum(ind));
ind = ismember(months, [4 6 9 11]);
days(ind) = randi(30, 1, sum(ind));
ind = ismember(months, 2);
days(ind) = randi(28, 1, sum(ind));
dates = [months; days];
end

round to the nearest even number with array of numbers

My function and rounding to nearest even number
function y = rndeven(x)
if x<=1
y=2;
else
y = 2*floor(x);
end
endfunction
When I run it I get:
cc=[0:3]'
both=[cc,rndeven(cc)]
0 0
1 2
2 4
3 6
What I'm trying to get as the Result:
0 2
1 2
2 2
3 4
You can use the modulo 2 to find whether a number is even. If it isn't this will return 1, so just add 1 to this number to find the nearest (larger) even number:
function y = rndeven(x)
x = floor(x);
x(x <= 1) = 2;
y = mod(x,2)+x;
end
This works for any array, order of elements does not matter.
You could also check if it is dividable by 2 if you don't want to use the mod function. The pseudo code would be something like this:
while(x % 2 != 0) x = x + 1
return x

FindRoot, starting values

I have a simple system of equations that I am trying to solve numerically,
ClearAll["Global`*"]
b1 = 1
T = 2
t = .5
FindRoot[{(1 - xbar1) 2 ra1 == n1 - (1 - n1)/T,
ra1 == (pa1^2) (b1^2)/pw,
pw == (1 - xbar1) ra1 - 1,
xbar1 == 1/t Log[(t n1 + ra1)/ra1],
t[1 - xbar1] + Log[T] - Log[ra1] + Log[t (1 - n1)/(Exp[t] - 1)] ==
0},
{{n1, .5}, {ra1, .5}, {pa1, .5}, {pw, 1.5}, {xbar1, .5}},
AccuracyGoal -> 11]
This is the output I get,
FindRoot::nlnum: "The function value {0.25,0.333333,2.25,-0.31093,0.432752
+0.5[0.5]} is not a list of numbers with dimensions {5} at
{n1,ra1,pa1,pw,xbar1} = {0.5,0.5,0.5,1.5,0.5}."de here`{n1,ra1,pa1,pw,xbar1} = ` {0.5,0.5,0.5,1.5,0.5}."``
I am wondering if I am not specifying the initial starting values correctly. I did this on a similar problem just using NSolve and I had no problem getting a result so I am stumped to what the problem is here.

Code Golf: Movement in 3 Dimensions

Assuming a 3 dimensional irregular matrix where y = 1.5(x) and z = .5(y).
Further assuming an object starts at 0,0,0 and must move positively in at least two dimensions, and must move in all three dimensions (x+1, y+1, z-1 is okay, x+1, y+1, z=z is not). It may move any number of "spaces", but must move the same number in all directions.
The object is allowed to wraparound (x(max +1) = x(0)).
Move said object from its starting position to (0, max(y), .5(max(z))) For z, round up for fractions (end point in 4, 6, 3 matrix becomes 0, 6, 2)
Input is an Integer (X).
Output is the list of moves you would make (extra credit for showing the number of spaces you moved)
Sample Input/Output:
X = 4
Y = 6 //(4 * 1.5)
Z = 3 // 6 / 2
0, 0, 0 //Start
2, 5, 2 // (+2, -2, +2)
1, 2, 2 // (+4, +4, +4)
3, 4, 0 // (+2, +2, -2)
1, 6, 2 // (-2, +2, +2)
3, 3, 3 // (-3, -3, -3)
1, 5, 1 // (-2, +2, -2)
0, 6, 2 // (-1, +1, -1)
7 Moves.
Lua, 68 Characters
The long version below always solves the problem with one move by searching for the first all positive move that will solve problem.
x=...
y,z=x*3/2,x*3/4
a,b,c=0,y,math.ceil(z/2)
x,y,z=x+1,y+1,z+1
for i=1,math.huge do
if (x*i)%y==b and (x*i)%z==c then x=x*i break end
end
print("0,0,0\n0,"..b..","..c.."//+"..x..",+"..x..",+"..x.."\n1 move.")
Output for x = 12:
0,0,0
0,18,5//+455,+455,+455
1 move.
Output for x = 1000:
0,0,0
0,1500,375//+557424868,+557424868,+557424868
1 move.
Seems like the search could be replaced with some simple algebraic equation. But why stop there? Rules are easier to bend in golfing then doing the actual work.
So, assuming that there is always a single 1 move answer, and that I do not have to disclose the "number of spaces you moved", here is the 68 character golfed answer:
x=...print("0,0,0\n0,"..(x*3/2)..","..math.ceil(x*3/8).."\n1 move.")
Mathematica - Not Golfed
Just to see if we can get the ball rolling
... and trying to understand the problem ....
f[x_] := (
(* Init code *)
xmax = x;
ymax = 3 Round[xmax]/2;
zmax = Round[ymax]/2;
xobj = 0;
yobj = ymax;
zobj = Ceiling[zmax/2];
p = Join[Permutations[{1, 1, -1}], {{1, 1, 1}}];
Print["X = ", xmax, "\nY = ", ymax, "\nZ = ", zmax];
(* Loop *)
i = 0;
pos = {0, 0, 0};
k = "Start";
While[
(npos= {Mod[pos[[1]], xmax+1], Mod[pos[[2]], ymax+1], Mod[pos[[3]], zmax+1]})
!= {xobj, yobj, zobj},
i++;
Print[npos, " // ", k];
pos= npos+ (k= RandomInteger[{1,xmax}] p[[RandomInteger[{1, Length[p]}]]]);
];
Print[npos, " // ", k];
Print[i, " Moves"];
);
Invoke with
f[4]
Sample Output
X = 4
Y = 6
Z = 3
{0,0,0} // Start
{3,4,3} // {3,-3,3}
{0,0,2} // {-3,3,3}
{2,3,1} // {-3,3,3}
{0,6,2} // {3,3,-3}
4 Moves
Not sure if I'm following the rules ...