modifying the lengthY of some cubes is slow - performance

I want to write a 3d version of a fft. (Like this:https://wiki.mozilla.org/File:Fft.png)
So I created a few bars and in an outside function, my first aproach was to set the lengthY to a value. Then I call bar.modified() to force it to be repainted.
If I now use more than 50 bars, it is horrible slow (on my 4 core CPU). I guess there's a better way to do it, right?
Source:
var elements = new Array();
create3d = function(len) {
var r = new X.renderer3D();
r.init();
if(a.length == 0){
for ( var y = 0; y < len; y++) {
var c = new X.cube();
a.push(c);
}
}
for ( var i = 0; i < len; i++) {
a[i].center = [i*2 , 0, 0];
a[i].lengthX = 1;
a[i].lengthY = 20;
a[i].lengthZ = 1;
a[i].color = [i%2,0,0];
r.add(a[i]);
}
r.render();
};
function setVal(index,val){
var element = a[index];
element.lengthY = val;
element.modified();
}

I created a JSFiddle on how to do that and it is pretty fast for 1000 cubes
http://jsfiddle.net/haehn/6fVRC/

Related

Question about fromCharCode and ending an "if" statement

I'm working on a simple cipher algorithm and I'm trying to figure out how to stop the if statement at character 122 (z) and start back at char 97 (a) once the character count exceeds 122. This is what I have so far and have looked around on MDN and W3 schools and haven't come up with anything.
enter code here
function simpleCipher(str) {
var newString = [];
for (var i = 0; i < str.length; i ++) {
if (str.charCodeAt(i) > 97 || str.charCodeAt(i) < 122) {
var convertString = String.fromCharCode(str.charCodeAt(i) + 4);
var powerString = newString.push(convertString);
} else {
return;
}
}
return newString.join('');
}
Use modular arithmetic.
var aPos = 97;
var zPos = 122;
var charsCount = zPos - aPos + 1;
...
if (aPos <= str.charCodeAt(i) && str.charCodeAt(i) <= zPos) { // probably you want && here
var charNumber = str.charCodeAt(i) - aPos;
var charNumberEncoded = (charNumber + 4) % charsCount;
var convertString = String.fromCharCode(aPos + charNumberEncoded);
var powerString = newString.push(convertString);
}
It is considered a good practice to give constants names and not use numbers in the code. Such numbers often referenced as magic numbers and make it harder to read the code.

actionscript 2: pick 3 random nummers from array

i need 3 random variables from array 0-36 (roulette).
i have this code. script return first 3 nummers from random array.
i need return 3 random nummers (from random position) from random array.
help me please.
onClipEvent (load) {
// field 0-36
var rands = [];
for (var i = 0; i < 36; ++i)
{
rands[i] = i;
}
// random sorting array
rands.sort(function(a,b) {return random(3)-1;});
// final variables 1,2,3
random1 = rands[0];
random2 = rands[1];
random3 = rands[2];
}
this is possible code for 1 variable, i need convert this to 3 variables in AS2
n = 3;
for (var i:Number = 0; i < n; i++) {
var randomSelection = Math.floor((Math.random() * rands.length));
trace("Selected: " + rands[randomSelection]);
}
It's not 100% clear from your question what you want, but this is my guess:
var n = 3;
var random = [];
for (var i:Number = 0; i < n; i++) {
// Store the random selections in an array
random.push(Math.floor(Math.random() * rands.length));
}
// You could assign them to variables or access directly via array
var random1 = random[0];
var random2 = random[1];
var random3 = random[2];

how to get screen coords using projection with a multipolygon in d3

I am using D3 to display customer location within counties. For MOST of my counties I am not having any problems. I am using the following code snippet to get the XY coordinates for my counties
var countyCoords = county.geometry.coordinates[0];
...
for (var j = 0; j < countyCoords.length; j++)
{
var x = projection(countyCoords[j])[0];
var y = projection(countyCoords[j])[1];
Works like a charm, with one exception. If my county.geometry["type"] is a MultiPolygon, these lines fail. How do I get the screen locations (i.e. x,y) of a MultiPolygon in d3?
Here are some lines from my input to d3.json() [edited for display here]
"counties":
{
"type":"GeometryCollection",
"geometries":[
{"type":"MultiPolygon","properties":{"NAME":"Chester"},"id":"Chester","arcs":[[[13,2111,15,2112,17,18,2113,2114,20,2115,2116,-1951,2117,2118,2119,9,2120,11,2121]],[[2122,2123]]]},
{"type":"Polygon","properties":{"NAME":"Clarion"},"id":"Clarion","arcs":[[-1966,2124,-2004,-2018,-2063]]},
I get the counties by using this line:
counties = topojson.feature(topoData, topoData.objects.counties);
I iterate through the counties and collect the XY locations mentioned above like this:
for (var i = 0; i < counties.features.length; i++)
{
var county = counties.features[i];
var countyCoords = county.geometry.coordinates[0];
var xy = [];
for (var j = 0; j < countyCoords.length; j++)
{
var x = projection(countyCoords[j])[0];
var y = projection(countyCoords[j])[1];
var dataPoint = [x,y];
xy.push(dataPoint);
}
county.properties["XY"] = xy;
}
This works correctly for the county "Clarion" but fails for the county "Chester".
A GeoJSON multipolygon has an additional array level to its structure that you'd need to iterate through:
http://geojson.org/geojson-spec.html
To account for this, you'd have to do something like this:
if (county.geometry.type == "MultiPolygon") {
for (var x=0; x< countyCoords.length;x++) {
for (var j = 0; j < countyCoords[x].length; j++) {
var x = projection(countyCoords[x][j])[0];
var y = projection(countyCoords[x][j])[1];
Here is the code to solve my problem. It is pretty much the suggestion of Elijah but tailored to my specific needs. (Note: this is only for Multipolygon types, normal polygon types should use the code identified in my original post). Also, please feel free to point out anywhere where my code can be improved.
for (var i = 0; i < counties.features.length; i++)
{
var county = counties.features[i];
if (county.geometry["type"] === "MultiPolygon")
{
var xy = [];
for (var k = 0; k < county.geometry.coordinates.length; k++)
{
var countyCoords = county.geometry.coordinates[k][0];
for (var j = 0; j < countyCoords.length; j++)
{
var x = projection(countyCoords[j])[0];
var y = projection(countyCoords[j])[1];
var dataPoint = [x,y];
xy.push(dataPoint);
}
county.properties["XY"] = xy;
}
}
}

Image rotation algorithm for a square pixel grid

I'm currently working on my own little online pixel editor.
Now I'm trying to add a rotation function.
But I can't quite figure out how to realize it.
Here is the basic query for my pixel grid:
for (var y = 0;y < pixelAmount;y++) {
for (var x = 0;x < pixelAmount;x++) {
var name = y + "x" + x;
newY = ?? ;
newX = ?? ;
if ($(newY + "x" + newX).style.backgroundColor != "rgb(255, 255, 255)")
{ $(name).style.backgroundColor = $(newY + "x" + newX).style.backgroundColor; }
}
}
How do I calculate newY and newX?
How do you rotate a two dimensional array?
from this^ post I got this method (in c#):
int a[4][4];
int n=4;
int tmp;
for (int i=0; i<n/2; i++){
for (int j=i; j<n-i-1; j++){
tmp=a[i][j];
a[i][j]=a[j][n-i-1];
a[j][n-i-1]=a[n-i-1][n-j-1];
a[n-i-1][n-j-1]=a[n-j-1][i];
a[n-j-1][i]=tmp;
}
}
or this one:
int[,] array = new int[4,4] {
{ 1,2,3,4 },
{ 5,6,7,8 },
{ 9,0,1,2 },
{ 3,4,5,6 }
};
int[,] rotated = RotateMatrix(array, 4);
static int[,] RotateMatrix(int[,] matrix, int n) {
int[,] ret = new int[n, n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
ret[i, j] = matrix[n - j - 1, i];
}
}
return ret;
}
the first method doesn't use a second array (/matrix) to save memory..
Take a look at this doc (Section 3: Rotating a bitmap with an angle of any value). It walks you through how to do the math and gives you some sample code (C++, but it should be good enough for what you need).
If very quick performance is not of huge importance (which is the case by default), you can consider rotating the picture clockwise by flipping it against the main diagonal and then horizontally. To rotate counterclockwise, flip horizontally, then against the main diagonal. The code is much simpler.
For diagonal flip you exchange the values of image[x,y] with image[y,x] in a loop like this
for( var x = 0; x < pixelAmount; ++x )
for( var y = x + 1; y < pixelAmount; ++y )
swap(image[x,y],image[y,x]);
For horizontal flip you do something like
for( var y = 0; y < pixelAmount; ++y )
{
i = 0; j = pixelAmount - 1;
while( i < j ) {
swap( image[i,y], image[j,y] );
++i; --j;
}
}

add custom clickable buttons

I am developing an app for win phone 7. In that I need to add clickable buttons(not specific number) into a grid dynamically.and navigate the page with some information to other page.
can any body help me please....
private void buildThumbs(Grid gridThumb, int p) {
int n;
if (p % 3 == 0)
n = p / 3;
else
n = (p / 3) + 1;
GridLength height = new GridLength(100);
for (int i = 0; i < n; i++)
{
RowDefinition rowDef = new RowDefinition();
rowDef.MinHeight = 100;
gridThumb.RowDefinitions.Add(rowDef);
}
MovieThumb[,] thumb = new MovieThumb[n, 3];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 3; j++)
{
Image ni=new Image();
ImageSourceConverter ims = new ImageSourceConverter();
ni.SetValue(Image.SourceProperty ,ims.ConvertFromString( "/Images/book1.png"));
thumb[i, j].SetValue(Grid.ColumnProperty, j);
thumb[i,j].SetValue(Grid.RowProperty,i);
thumb.Click += new RoutedEventHandler(thumb_click("sandy"));
thumb[i, j].CoverImage = ni;
thumb[i, j].Loading = "Loading";
thumb[i, j].progress = false;
gridThumb.Children.Add(thumb);
}
}
}
I am not quite sure if I understand your problem, but if you want to set the Column and Row number for your dynamically created buttons, you may do so with Grid.SetColumn(buttonname, column); Grid.SetRow(buttonname, row); before you add the object to the gridThumb.
We need some more information for what exactly you want to accomplish and what doesn't work for you to help :)

Resources