We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone, I am translating a working code for research project and I need to take this phrase from javascript to processing. Any help would be greatly appreciated thank you.
Bristle.prototype.update = function(newPosition) {
var i, pos, previousPos, ang, length;
// Set the first position to the provided position
pos = this.positions[0];
pos.x = newPosition.x;
pos.y = newPosition.y;
previousPos = pos;
// Update the other positions
for (i = 1; i < this.nPositions; i++) {
pos = this.positions[i];
length = this.lengths[i];
ang = p.atan2(previousPos.y - pos.y, previousPos.x - pos.x);
pos.x = previousPos.x - length * Math.cos(ang);
pos.y = previousPos.y - length * Math.sin(ang);
previousPos = pos;
}
};
Answers
It is unclear is what this code is intended to do (e.g. it is clearly part of a broader project). It seems like it is designed to move something (position A), and then move other things in relation to that new position A. Rather than trying to rewrite a fragment of code, you are probably better off trying to explain the overall program flow (if you can post a working link, or show with screenshots etc that would probably help) From the look of it, this function is relying on separate arrays to store positions, but those aren't described in the code above.
Depending on what you are trying to do, it may be easier to create a class rather than simply rewriting a function.
http://jagracar.com/sketches/sourceCode/oilPainting.js source code http://jagracar.com/sketches/oilPainting.php working link
This is the class it is from
That's the 1st time I've seen any1 needing to convert JS to Java! @-)
That "oilPainting.js" was written for p5.js library: http://p5js.org/reference/
Therefore you need to convert it to Java Mode: https://Processing.org/reference/
Some tips for it: https://GitHub.com/processing/p5.js/wiki/Processing-transition
Here's my attempt to turn that JS' class Bristle to Java's: :-bd
Thank you very much GoToLoop, the reason I'm translating it is so I can run it offline such as on phone and for research project. I'm using it for my outcome explaining some if the processes used to make paintings from regular images through the medium of generative art.
@Althalus7 said:
You don't need to translate JS to run on a phone. See platforms like phonegap. To run offline you can run on a local server and if need be wrap this up as an application: for example using a platform like Electron.
This particular sketch already has a java version: http://www.openprocessing.org/sketch/128167
Nice find, @BarbaraAlmeida! Although I believe my converted tweaked version for class Bristle is a little better, heheheh.
Interestingly, my decision to split the unified JS method paint() into 2 Java methods, 1 for screen canvas and the other for custom canvas, matches the original Java version there. ;))
Thank you, I'll have a look when I can get on my computer.