Anyway to simplify/shorten this math equasion I made? - actionscript-3

I am trying to make a grid of slots for an inventory sort of thing , I have it looking how I want it, but I was wondering if there was anyway to simplify or shorten my math. Every square on the grid is 30 X 30.
for (i = 1; i <= Math.floor((QuartzBackground.width - 10) / 30) * 3; i++)
{
// X Position
trace(((QuartzBackground.x + ((i - 1) * 32.5)) - (Math.ceil(i / 9 - 1) * (QuartzBackground.width - 10))) - ((Math.ceil(i / 9) - 1) * 2.5));
// Y Position
trace(QuartzBackground.y + 57.5 + (Math.ceil(i / 9) * 32.5));
}

You have a couple of things repeated at least twice there. It's not much but still.
var QB:DisplayObject = QuartzBackground;
var QBwm10:Number = QB.width - 10;
for (i = 1; i <= Math.floor(QBwm10 / 30) * 3; i++)
{
var Mcid9:Number = Math.ceil(i / 9);
trace(QB.x + (i - 1) * 32.5 - (Mcid9 - 1) * (QBwm10 - 2.5)); // X Position
trace(QB.y + 57.5 + (Mcid9 * 32.5)); // Y Position
}

Related

Amplitude increasing in the verlet method in backward difference

I am facing trouble in the increasing oscillation on a simple harmonic oscillator using a backward difference. here is my code in Scilab
function [x] = back(h, tf)
k = 2;
m = 1;
i = 2;
t(i - 1) = 0;
x(i - 1) = 10;
v(i - 1) = 0;
t(i) = t(i - 1) + h
v(i) = v(i - 1) - h * (k / m) * x(i - 1)
while t(i) < tf
t(i + 1) = t(i) + h
x(i + 1) = x(i - 1) - 2 * (k / m) * v(i) * h
i = i + 1
end
plot(t, x, 'b');
endfunction
From your code, I suppose that you are trying to implement the velocity-Verlet scheme. Here is its implementation for a simple oscillator with the differential equation:
function [x] = back(h, tf)
k = 2;
m = 1;
t = 0:h:tf;
x(1) = 10;
v(1) = 0;
for i=2:length(t)
x(i) = x(i - 1) + v(i - 1) * h - k / m * x(i-1) * h^2 / 2;
v(i) = v(i - 1) - k / m * (x(i) + x(i-1)) * h / 2;
end
plot(t, x, 'b');
endfunction
[x] = back(0.01, 10)
I'm not quite sure what you are trying to achieve, neither if your math is correct. But assuming that you want to solve the numerical problem of:
//coefficients of:
k = 2.;
m = 1.;
// with an initial condition of:
t(1) = 0.;
x(1) = 10.;
v(1) = 0.;
// time paramters:
N = 50;
tf = 10;
h = tf / 50.;
for ii = 2:N
t(ii) = t(ii - 1) + h;
x(ii) = x(ii - 1) - 2 * (k / m) * v(ii - 1) * h
v(ii) = v(ii - 1) - h * (k / m) * x(ii - 1)
disp(x(ii))
end
plot(t, x, 'b');
will result in:
which doesn't seem right but anyway. Please check your math again.

Add rings to consistent hashing circle to represent data

I am implementing consistent hashing and hence drawing a circle with sectors as shown in the Circle Demo. The sectors represents the Nodes.
The HTML withing which my Circle resides is :
<div id="container1">
<div id="svgcontainer"></div>
</div>
Now I want to add some dots(small rings) over the circumference of the circle to show the key-value pair that belong to a particular node.
I am sing HTML5 for my circle.
After adding the data(key value pair my circle) , the circle should have some rings(or any other representations) on its boundary like required circle output
How can I achieve this in HTML5 ?
TIA :)
The dot for a given sector will be positioned at a point (xd,yd) on the circumference half ways between the sector's (x1,y1) and (x2,y2) points. Calculating the dot's position (xd,yd) will be similar to calculating the sector's (x1,y1) and (x2,y2) but with an angle that is half ways between the anlges used for calculating (x1,y1) and (x2,y2). If you wish to place text near the dot and outside the circle then calculating the text's position (xt,yt) will be similar to calculating the dot's position (xd,yd) but with a larger radius. For example, the existing addSector() function could be modified to...
function addSector() {
sector++;
group.clear();
paper.clear();
var start = 0;
var angle = 360 / sector;
var col = 0;
var x1;
var y1;
var x2;
var y2;
var xd;
var yd;
var xt;
var yt;
var i;
var path;
var dot;
var text;
var textPadding = 15;
for (i = 0; i < sector; i++) {
x1 = Math.round((x + Math.cos(start * Math.PI / 180) * radius) * 100) / 100;
y1 = Math.round((y + Math.sin(start * Math.PI / 180) * radius) * 100) / 100;
x2 = Math.round((x + Math.cos((start + angle) * Math.PI / 180) * radius) * 100) / 100;
y2 = Math.round((y + Math.sin((start + angle) * Math.PI / 180) * radius) * 100) / 100;
path = paper.path("M" + x + "," + y + " L" + x1 + "," + y1 + " A" + radius + "," + radius + " 0 0 1 " + x2 + "," + y2 + "z");
path.attr({"fill": colors[col], "stroke" : null});
group.push(path);
col++;
if (col == colors.length) col = 0;
start += angle;
}
for (i = 0; i < sector; i++) {
start = i * angle;
xd = Math.round((x + Math.cos((start + angle / 2) * Math.PI / 180) * radius) * 100) / 100;
yd = Math.round((y + Math.sin((start + angle / 2) * Math.PI / 180) * radius) * 100) / 100;
dot = paper.circle(xd, yd, 5);
dot.attr({"fill": "#FFFFFF", "stoke": "#000000"});
xt = Math.round((x + Math.cos((start + angle / 2) * Math.PI / 180) * (radius + textPadding)) * 100) / 100;
yt = Math.round((x + Math.sin((start + angle / 2) * Math.PI / 180) * (radius + textPadding)) * 100) / 100;
text = paper.text(xt, yt, i.toString());
}
}

Canvas quadraticCurve center point

I want need to know how detect center coordinates of quadraticCurve in HTML5 canvas. I want to draw arrow in this center point of curve.
There is my draw curve method:
function draw_curve(Ax, Ay, Bx, By, M, context) {
var dx = Bx - Ax,
dy = By - Ay,
dr = Math.sqrt(dx * dx + dy * dy);
// side is either 1 or -1 depending on which side you want the curve to be on.
// Find midpoint J
var Jx = Ax + (Bx - Ax) / 2
var Jy = Ay + (By - Ay) / 2
// We need a and b to find theta, and we need to know the sign of each to make sure that the orientation is correct.
var a = Bx - Ax
var asign = (a < 0 ? -1 : 1)
var b = By - Ay
var bsign = (b < 0 ? -1 : 1)
var theta = Math.atan(b / a)
// Find the point that's perpendicular to J on side
var costheta = asign * Math.cos(theta)
var sintheta = asign * Math.sin(theta)
// Find c and d
var c = M * sintheta
var d = M * costheta
// Use c and d to find Kx and Ky
var Kx = Jx - c
var Ky = Jy + d
// context.bezierCurveTo(Kx, Ky,Bx,By, Ax, Ax);
context.quadraticCurveTo(Kx, Ky, Bx, By);
// draw the ending arrowhead
var endRadians = Math.atan((dx) / (dy));
context.stroke();
var t = 0.5; // given example value
var xx = (1 - t) * (1 - t) * Ax + 2 * (1 - t) * t * Kx + t * t * Bx;
var yy = (1 - t) * (1 - t) * Ay + 2 * (1 - t) * t * Ky + t * t * By;
var k = {};
k.x = xx;
k.y = yy;
SOLVED BY THIS CODE, T is parameter which set position on the curve:
var t = 0.5; // given example value
var xx = (1 - t) * (1 - t) * Ax + 2 * (1 - t) * t * Kx + t * t * Bx;
var yy = (1 - t) * (1 - t) * Ay + 2 * (1 - t) * t * Ky + t * t * By;
var k = {};
k.x = xx;
k.y = yy;

How can I make Math.random call a different value everytime it is called in AS3?

Let's say I had something like what's below in a function, the first time it gets called, I have a random value, but every other time it's called, I get the same number as it spit out the first time. What can I do to ensure it is randomized each time it is called?
if (Cosmo.hitTestObject(Asteroid5))
{
Asteroid5.y = (Math.random() * 20 - 5);
Asteroid5.x = (Math.random() * 20 - 15);
Asteroid5.x = Asteroid5.x + (Math.random() * 20 - 15);
Asteroid5.y = Asteroid5.y + (Math.random() * 20 - 5);
}
[Edited, based on comments below]
The AS3 reference page for Math.random() says:
Returns a pseudo-random number n, where 0 <= n < 1. The number
returned is calculated in an undisclosed manner, and is
"pseudo-random" because the calculation inevitably contains some
element of non-randomness.
If you want to use a seed-based pseudo-random number generator, this should help you:
http://www.kirupa.com/forum/showthread.php?365564-AS3-Seeded-Pseudo-Random-Number-Generator
in your 1st 2 statements, it looks like you're using Asteroid5.y & Asteroid5.x as variables. so one idea would be to use variables of the correct type (i'm assuming it's Number):
var rndX:Number = (Math.random() * 20 - 15);
var rndY:Number = (Math.random() * 20 - 5);
var rndX2:Number = rndX + (Math.random() * 20 - 15);
var rndY2:Number = rndY + (Math.random() * 20 - 5);
Asteroid5.x = rndX2;
Asteroid5.y = rndY2; or
var rndX:Number;
var rndY:Number;
for ( var i:uint=0; i<2; i++ )
{
rndX += (Math.random() * 20 - 15);
rndY += (Math.random() * 20 - 5);
}
Asteroid5.x = rndX;
Asteroid5.y = rndY;
now you can step through your code in debug mode and see exactly what going on as the values of the variables change. also maybe search your code for 'Asteroid5.x =' & 'Asteroid5.y =' & 'Asteroid5.y=' & 'Asteroid5.x='

as3 getting the "hours" in milliseconds in mp3

In As3 the code below gets the minutes and the seconds:
var minutes:uint = Math.floor(PrayPrayer.position / 1000 / 60);
var seconds:uint = Math.floor(PrayPrayer.position / 1000) % 60;
But what if your listening to an audio talk that goes over the hour mark?
What is the math needed to get the hours from an mp3 talk?
var hours:uint = Math.floor(PrayPrayer.position / 1000) % 60 & (((???????)));
this is my conversion method:
public static var MINUTE:Number = 60;
public static var HOUR:Number = 60 * MINUTE;
public static var DAY:Number = 24 * HOUR;
/**
* returns string created from seconds value in following format hours:minutes:seconds, i.e. 121 seconds will be displayed as 00:02:01
* #param seconds <i>Number</i>
* #return <i>String</i>
*/
public static function secondsToHMS(seconds:Number, doNotRound:Boolean = false):String
{
var _bNegative:Boolean = seconds < 0;
seconds = Math.abs(seconds);
var time:Number = (doNotRound) ? seconds:Math.round(seconds);
var ms:Number;
var msec:String;
if (doNotRound)
{
ms = seconds - (seconds | 0);
msec = prependZeros((ms * 1000) | 0, 3);
}
var sec:Number = (time | 0) % MINUTE;
var min:Number = Math.floor((time / MINUTE) % MINUTE);
var hrs:Number = Math.floor(time / HOUR);
//
return (_bNegative ? "-":"") +
((hrs > 9) ? "":"0") + hrs + ":" +
((min > 9) ? "":"0") + min + ":" +
((sec > 9) ? "":"0") + sec +
(doNotRound ? "." + msec:"");
}
prependZeros is another utility to add "0" in front of given string.
So PrayPrayer.position is in milliseconds. Your minutes line is dividing by 1000 to get seconds, then dividing by 60 to go from seconds to minutes. Your seconds line is looking at the remainder.
What you started in your hours line is using %, so will look at the remainder - you're using seconds there. % is the modulo operator. It gives you the remainder of integer division. So your line
var seconds:uint = Math.floor(PrayPrayer.position / 1000) % 60;
is finding the number of seconds (PrayPrayer.position / 1000), which could be something big like 2337, dividing by 60 and just keeping the remainder. 2337/60 = 38 remainder 57, so 2337%60 will be 57.
An easy way to find hours is to use the same trick with your minutes.
var minutes:uint = Math.floor(PrayPrayer.position / 1000 / 60);
var seconds:uint = Math.floor(PrayPrayer.position / 1000) % 60;
var hours:uint = Math.floor(minutes / 60);
minutes %= 60; // same as minutes = minutes % 60. Forces minutes to be between 0 and 59.