is there a way to make this code shorter? - processing
i wrote this program and, being new to this sort of thing, I can only write very sloppy programs, is there a way to make this code more efficient/shorter, i know most of it is redundant but, even after taking out all of the nonessential code, it still feels like I'm doing something wrong. I've been asked to actually explain what the code does and the logic of the increments and decrements, each of the integer arrays provides either an X value or a Y value, these are used to draw lines, all of them with identical X and Y values, then, in the draw function, specific X/Y values (if not all of them) are selected, the increments/decrements add 1/subtract 1 from these specific integer arrays every time the screen refreshes, this extends the lines in certain directions, creating a shape which can be repeated in a pattern indefinitely.
int[] myintegerarray = new int[66];
void setup()
{
background(0,100,0);
myintegerarray[0] = 0;
myintegerarray[1] = 0;
myintegerarray[2] = 0;
myintegerarray[3] = 0;
myintegerarray[4] = 900;
myintegerarray[5] = 0;
myintegerarray[6] = 900;
myintegerarray[7] = 0;
myintegerarray[8] = 450;
myintegerarray[9] = 450;
myintegerarray[10] = 450;
myintegerarray[11] = 450;
myintegerarray[12] = 450;
myintegerarray[13] = 450;
myintegerarray[14] = 450;
myintegerarray[15] = 450;
myintegerarray[16] = 0;
myintegerarray[17] = 900;
myintegerarray[18] = 0;
myintegerarray[19] = 900;
myintegerarray[20] = 900;
myintegerarray[21] = 900;
myintegerarray[22] = 900;
myintegerarray[23] = 900;
myintegerarray[24] = 450;
myintegerarray[25] = 0;
myintegerarray[26] = 450;
myintegerarray[27] = 0;
myintegerarray[28] = 450;
myintegerarray[29] = 900;
myintegerarray[30] = 450;
myintegerarray[31] = 900;
myintegerarray[32] = 0;
myintegerarray[33] = 450;
myintegerarray[34] = 0;
myintegerarray[35] = 450;
myintegerarray[36] = 900;
myintegerarray[37] = 450;
myintegerarray[38] = 900;
myintegerarray[39] = 450;
myintegerarray[40] = 450;
myintegerarray[41] = 450;
myintegerarray[42] = 450;
myintegerarray[43] = 450;
myintegerarray[44] = 450;
myintegerarray[45] = 225;
myintegerarray[46] = 450;
myintegerarray[47] = 225;
myintegerarray[48] = 450;
myintegerarray[49] = 675;
myintegerarray[50] = 450;
myintegerarray[51] = 675;
myintegerarray[52] = 675;
myintegerarray[53] = 450;
myintegerarray[54] = 675;
myintegerarray[55] = 450;
myintegerarray[56] = 225;
myintegerarray[57] = 225;
myintegerarray[58] = 675;
myintegerarray[59] = 675;
myintegerarray[60] = 675;
myintegerarray[61] = 675;
myintegerarray[62] = 225;
myintegerarray[63] = 225;
myintegerarray[64] = 225;
myintegerarray[65] = 225;
size (900, 900);
}
void draw() {
myintegerarray[2] ++;
myintegerarray[3] ++;
myintegerarray[6] --;
myintegerarray[7] ++;
myintegerarray[9] --;
myintegerarray[11] ++;
myintegerarray[12] --;
myintegerarray[14] ++;
myintegerarray[18] ++;
myintegerarray[19] --;
myintegerarray[22] --;
myintegerarray[23] --;
myintegerarray[27] ++;
myintegerarray[31] --;
myintegerarray[34] ++;
myintegerarray[38] --;
myintegerarray[42] ++;
myintegerarray[43] ++;
myintegerarray[40] --;
myintegerarray[41] --;
myintegerarray[44] ++;
myintegerarray[46] --;
myintegerarray[50] ++;
myintegerarray[48] --;
myintegerarray[53] --;
myintegerarray[55] ++;
myintegerarray[58] --;
myintegerarray[59] ++;
myintegerarray[60] ++;
myintegerarray[61] --;
myintegerarray[62] --;
myintegerarray[63] ++;
myintegerarray[64] ++;
myintegerarray[65] --;
line(myintegerarray[0],myintegerarray[1],myintegerarray[2],myintegerarray[3]);
line(myintegerarray[4],myintegerarray[5],myintegerarray[6],myintegerarray[7]);
line(myintegerarray[8],myintegerarray[9],myintegerarray[10],myintegerarray[11]);
line(myintegerarray[12],myintegerarray[13],myintegerarray[14],myintegerarray[15]);
line(myintegerarray[16],myintegerarray[17],myintegerarray[18],myintegerarray[19]);
line (myintegerarray[20],myintegerarray[21],myintegerarray[22],myintegerarray[23]);
line(myintegerarray[24],myintegerarray[25],myintegerarray[26],myintegerarray[27]);
line(myintegerarray[28],myintegerarray[29],myintegerarray[30],myintegerarray[31]);
line(myintegerarray[32],myintegerarray[33],myintegerarray[34],myintegerarray[35]);
line(myintegerarray[36],myintegerarray[37],myintegerarray[38],myintegerarray[39]);
line(myintegerarray[40],myintegerarray[41],myintegerarray[42],myintegerarray[43]);
line(myintegerarray[40],myintegerarray[42],myintegerarray[43],myintegerarray[41]);
line(myintegerarray[44],myintegerarray[45],myintegerarray[46],myintegerarray[47]);
line(myintegerarray[48],myintegerarray[49],myintegerarray[50],myintegerarray[51]);
line(myintegerarray[52],myintegerarray[53],myintegerarray[54],myintegerarray[55]);
line(myintegerarray[56],myintegerarray[53],myintegerarray[57],myintegerarray[55]);
line(myintegerarray[58],myintegerarray[59],myintegerarray[60],myintegerarray[61]);
line(myintegerarray[62],myintegerarray[63],myintegerarray[64],myintegerarray[65]);
line(myintegerarray[60],myintegerarray[63],myintegerarray[58],myintegerarray[65]);
line(myintegerarray[63],myintegerarray[60],myintegerarray[65],myintegerarray[58]);
}
To shorten the initialization of the array, you could use an array initializer:
myintegerarray = {0, 0, 0, 0, 900, 0, 900, ...};
To shorten the incrementing and decrementing logic, you need to have a pattern as to what is being incremented and decremented. Maybe the first half of the array is incremented, and the second half is decremented. Then just use for loops:
for(int i = 0; i < 33; i++){
myintegerarray[i]++;
}
for(int i = 33; i < 66; i++){
myintegerarray[i]--;
}
To shorten the line logic, setup a similar pattern and just use a for loop:
for(int i = 0; i < 66; i+=4){
line(myintegerarray[i],myintegerarray[i+1],myintegerarray[i+2],myintegerarray[i+3]);
}
But really, I wouldn't worry too much about making your code as short as possible. As long as you understand your code and it works, you're doing fine. You'll learn how to make code shorter naturally as you go along. The most important thing is that you can read your own code, so don't worry too much about anything else.
Related
How to use FF_CONSTANT for Force feedback for Linux?
I am not able to utilize FF_CONSTANT force effect. My try code is: struct ff_effect joy_effect_, joy_effect_2; if (iwantconstantforce) { joy_effect_.id = -1; joy_effect_.type = FF_CONSTANT; joy_effect_.direction = 0x0000; // down joy_effect_.replay.length = 100; joy_effect_.replay.delay = 0; joy_effect_.trigger.button = 0; joy_effect_.trigger.interval = 100; joy_effect_.u.constant.level = 65535; joy_effect_.u.constant.envelope.attack_length = joy_effect_.replay.length / 10; joy_effect_.u.constant.envelope.fade_length = joy_effect_.replay.length / 10; joy_effect_.u.constant.envelope.attack_level = joy_effect_.u.constant.level / 10; joy_effect_.u.constant.envelope.fade_level = joy_effect_.u.constant.level / 10; } I am able to produce FF_SPRING and FF_DAMPER effects with following codes. if (youwantdampereffect) { joy_effect_.id = -1; joy_effect_.direction = 0; // down joy_effect_.type = FF_DAMPER; joy_effect_.replay.length = 20; joy_effect_.replay.delay = 0; joy_effect_.u.condition[0].right_saturation = 65535; joy_effect_.u.condition[0].left_saturation = 65535; joy_effect_.u.condition[0].right_coeff = 65535 / 2; joy_effect_.u.condition[0].left_coeff = 65535 / 2; joy_effect_.u.condition[0].deadband = 0; joy_effect_.u.condition[0].center = 0; int ret = ioctl(ff_fd_, EVIOCSFF, &joy_effect_); // upload the effect } if (youwantspringeffect) { joy_effect_2.id = -1; joy_effect_2.direction = 0; // down joy_effect_2.type = FF_SPRING; joy_effect_2.replay.length = 20; joy_effect_2.replay.delay = 0; joy_effect_2.u.condition[0].right_saturation = 65535 / 2; joy_effect_2.u.condition[0].left_saturation = 65535 / 2; joy_effect_2.u.condition[0].right_coeff = 32767; joy_effect_2.u.condition[0].left_coeff = 32767; joy_effect_2.u.condition[0].deadband = 0; joy_effect_2.u.condition[0].center = 0; int ret = ioctl(ff_fd_, EVIOCSFF, &joy_effect_2); // upload the effect } I do not find any info about what is constant force effect feels like or when it makes sense to use it. Can somebody brief its importance and usage? Thanks :)
Viewing the matched features in OpenCV stitching pipeline
I am facing trouble with a 2D stitching problem as posted here: link I want to see the feature matching that happens in the stitching pipeline. I can do it by writing the stitching algorithm on my own like the below code. However, can anyone please tell me how to get this information from the stitching pipeline?. I want to see the matched features on another file. double minDist = Double.MAX_VALUE; double maxDist = 0; double distance; for (int i = 0; i < matchesArr.length; i++) { distance = matchesArr[i].distance; if (distance < minDist) minDist = distance; else if (distance > maxDist) maxDist = distance; } final double thresholdFactor = 3.5; List<DMatch> good_matches = new Vector<DMatch>(); for (int i = 0; i < matchesArr.length; i++) { if (matchesArr[i].distance <= thresholdFactor * minDist) { good_matches.add(matchesArr[i]); } } LinkedList<Point> listImage1 = new LinkedList<Point>(); LinkedList<Point> listImage2 = new LinkedList<Point>(); List<KeyPoint> keyPointsList1 = keyPoints1.toList(); List<KeyPoint> keyPointsList2 = keyPoints2.toList(); for (int i = 0; i < good_matches.size(); i++) { listImage1.addLast(keyPointsList1.get(good_matches.get(i).queryIdx).pt); listImage2.addLast(keyPointsList2.get(good_matches.get(i).trainIdx).pt); } MatOfDMatch goodMatches = new MatOfDMatch(); goodMatches.fromList(good_matches); Features2d.drawMatches(processedImage1, keyPoints1, processedImage2, keyPoints2, goodMatches, imgMatch, new Scalar(254, 0, 0), new Scalar(254, 0, 0), new MatOfByte(), 2); boolean imageMatched = imgcodecs.imwrite("imageMatched.jpg", imgMatch);
Unity line renderer smooth algorithm
I need an !effective! algorithm to smooth a line renderer (basically, the given Vector3 which holds the points of the renderer) something like that Here is my code, but the fps with it is very low: public static List<Vector3> MakeSmoothCurve(Vector3[] arrayToCurve, float smoothness) { List<Vector3> points; List<Vector3> curvedPoints; int pointsLength = 0; int curvedLength = 0; if (smoothness < 1.0f) smoothness = 1.0f; pointsLength = arrayToCurve.Length; curvedLength = (pointsLength * Mathf.RoundToInt(smoothness)) - 1; curvedPoints = new List<Vector3>(curvedLength); float t = 0.0f; for (int pointInTimeOnCurve = 0; pointInTimeOnCurve < curvedLength + 1; pointInTimeOnCurve++) { t = Mathf.InverseLerp(0, curvedLength, pointInTimeOnCurve); points = new List<Vector3>(arrayToCurve); for (int j = pointsLength - 1; j > 0; j--) { for (int i = 0; i < j; i++) { points[i] = (1 - t) * points[i] + t * points[i + 1]; } } curvedPoints.Add(points[0]); } return (curvedPoints); }
You can use a CurveField https://docs.unity3d.com/ScriptReference/EditorGUILayout.CurveField.html With that you can easily edit/test your curve and retrieve a point at given time. https://docs.unity3d.com/ScriptReference/AnimationCurve.Evaluate.html
Fully Associative Cache implementation
I don't understand why my code for the fully associative cache doesn't match the trace files that I'm given. The parameters are each cache line is 32 bytes and the total cache size is 16KB. My implementations for set associative caches of 2,4,8,and 16 all work perfectly (using least recently used replacement policy). But for fully associative, which could also just be described as a set associative of 32, is VERY close to the trace file but not quite. Frankly, I don't know how to debug this one since there's a vast amount of steps (at least the way I did it) Here's the relevant parts of my code (excuse the inefficiency) //Fully Associative int **fullyAssoc; fullyAssoc = new int*[64]; //where fullyAssoc[0][index] is way 0, fullyAssoc[2][index] is way 1 etc.. int **LRU32; LRU32 = new int*[32]; for (int i = 0; i < 64; ++i){ //Initialize all entries in fullyAssoc to 0 fullyAssoc[i] = new int[16 * CACHE_LINE / 32]; } for (int i = 0; i < 16; i++){ //Initialize LRU array LRU32[0][i] = 0; LRU32[1][i] = 1; LRU32[2][i] = 2; LRU32[3][i] = 3; LRU32[4][i] = 4; LRU32[5][i] = 5; LRU32[6][i] = 6; LRU32[7][i] = 7; LRU32[8][i] = 8; LRU32[9][i] = 9; LRU32[10][i] = 10; LRU32[11][i] = 11; LRU32[12][i] = 12; LRU32[13][i] = 13; LRU32[14][i] = 14; LRU32[15][i] = 15; LRU32[16][i] = 16; LRU32[17][i] = 17; LRU32[18][i] = 18; LRU32[19][i] = 19; LRU32[20][i] = 20; LRU32[21][i] = 21; LRU32[22][i] = 22; LRU32[23][i] = 23; LRU32[24][i] = 24; LRU32[25][i] = 25; LRU32[26][i] = 26; LRU32[27][i] = 27; LRU32[28][i] = 28; LRU32[29][i] = 29; LRU32[30][i] = 30; LRU32[31][i] = 31; } int fullyAssocLRU = 0; int memCount = 0; while(getline(fileIn, line)){ stringstream s(line); s >> instruction >> hex >> address; int indexFull; int tagFull; unsigned long long address, addressFull; address = address >> 5; //Byte offset addressFull = address; indexFull = addressFull % 16; tagFull = addressFull >> 4; if (assocCache(fullyAssoc, indexFull, 32, tagFull, LRU32) == 1){ fullyAssocLRU++; } } void LRU_update(int **lru, int index, int way, int ways){ int temp = 0; int temp2[ways]; int temp_index = 0; int i = 0; while(i < ways){ if (lru[i][index] == way/2){ temp = lru[i][index]; i++; continue; } else{ temp2[temp_index] = lru[i][index]; temp_index++; } i++; } for (int j = 0; j < ways - 1; j++){ lru[j][index] = temp2[j]; } lru[ways - 1][index] = temp; } bool assocCache(int **block, int index, int ways, int tag, int **lru){ bool retVal = false; for(int i = 0; i < 2*ways; i = i + 2){ if (block[i][index] == 0){ block[i][index] = 1; block[i+1][index] = tag; LRU_update(lru, index, i, ways); return retVal; } else{ if (block[i+1][index] == tag){ retVal = true; LRU_update(lru, index, i, ways); return retVal; } else{ continue; } } } int head = 2 * lru[0][index]; block[head][index] = 1; block[head+1][index] = tag; LRU_update(lru, index, head, ways); return retVal; } The trace files is supposed to be: 837589,1122102; 932528,1122102; 972661,1122102; 1005547,1122102; //For direct mapped 993999,1122102; 999852,1122102; 999315,1122102; 1000092,1122102; //For set associative 1000500,1122102; //For fully associative (LRU) My output is: 837589,1122102; 932528,1122102; 972661,1122102; 1005547,1122102; 939999,1122102; 999852,1122102; 999315,1122102; 1000092,1122102; 1000228,1122102; As you can see, for the fully associative one, it's only 272 off the correct output. Why would it be off when switching from 16 ways to 32 ways?
Ah, I mistakenly though a fully associative cache for a 32 line size cache of 16KB cache size is 32 ways, when it's actually 512 ways.
modifying the lengthY of some cubes is slow
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/