Identify / Understand a Function in a Code

Hi! I found this code in OpenProcessing and I want to change some of its functions, I have been trying to understand the code but I simply cant.
So, what I’m trying to do, is that instead of manipulating the mesh with the handles, it can be automated and that it changes with data. In other words, I want to connect an API to the code and that data should affect the shape of the mesh.
Hope somebody can help =)

</>
StrechTransform myTransform = new StrechTransform();

float tileSize = 20;
float margin = 100;

color anchorColor = color(0, 150, 0);
color anchorHiColor = color(200, 100, 0);

int anchorNum = -1;
boolean dragOrigin = false;
boolean dragTarget = false;

void setup() {
size(700, 700);
smooth();

myTransform.addAnchor(new PVector(margin, margin));
myTransform.addAnchor(new PVector(width-margin, margin));
myTransform.addAnchor(new PVector(width-margin, height-margin));
myTransform.addAnchor(new PVector(margin, height-margin));
myTransform.addAnchor(new PVector(300, 300), new PVector(350, 330));
myTransform.addAnchor(new PVector(400, 400), new PVector(350, 370));
}

void draw() {

strokeWeight(0.5);
background(255);

if (anchorNum >= 0) {
if (dragOrigin) {
myTransform.setAnchorOrigin(anchorNum, mouseX, mouseY);
}
if (dragTarget) {
myTransform.setAnchorTarget(anchorNum, mouseX, mouseY);
}
}

// horizontal lines
for (float y = margin; y <= height-margin; y+=tileSize) {
for (float x = margin; x < width-margin; x+=tileSize) {
PVector p1 = myTransform.transform(x, y);
PVector p2 = myTransform.transform(x+tileSize, y);
line(p1.x, p1.y, p2.x, p2.y);
}
}
// vertical lines
for (float y = margin; y < height-margin; y+=tileSize) {
for (float x = margin; x <= width-margin; x+=tileSize) {
PVector p1 = myTransform.transform(x, y);
PVector p2 = myTransform.transform(x, y+tileSize);
line(p1.x, p1.y, p2.x, p2.y);
}
}

pushStyle();
for (int i = 0; i < myTransform.getAnchorCount(); i++) {
stroke(anchorColor);
if (i == anchorNum) stroke(anchorHiColor);

PVector anchorOrig = myTransform.getAnchorOrigin(i);
PVector anchorDest = myTransform.getAnchorTarget(i);
line(anchorOrig.x, anchorOrig.y, anchorDest.x, anchorDest.y);

}

strokeWeight(1);
fill(255);
for (int i = 0; i < myTransform.getAnchorCount(); i++) {
stroke(anchorColor);
if (i == anchorNum) stroke(anchorHiColor);

PVector anchorOrig = myTransform.getAnchorOrigin(i);
ellipse(anchorOrig.x, anchorOrig.y, 13, 13);

}

noStroke();
for (int i = 0; i < myTransform.getAnchorCount(); i++) {
fill(anchorColor);
if (i == anchorNum) fill(anchorHiColor);

PVector anchorDest = myTransform.getAnchorTarget(i);
ellipse(anchorDest.x, anchorDest.y, 7, 7);

}
popStyle();

}

void mousePressed() {
dragOrigin = false;
dragTarget = false;
anchorNum = -1;

int io = myTransform.getAnchorByOriginPos(new PVector(mouseX, mouseY), 10);
int it = myTransform.getAnchorByTargetPos(new PVector(mouseX, mouseY), 10);
if (it >= 0) {
anchorNum = it;
dragTarget = true;
}
else if (io >= 0) {
anchorNum = io;
dragOrigin = true;
}
}

void mouseReleased() {
dragOrigin = false;
dragTarget = false;
}

void keyReleased() {

if (key == ‘+’) {
myTransform.addAnchor(new PVector(mouseX, mouseY));
anchorNum = myTransform.getAnchorCount() - 1;
}

if (keyCode == DELETE || keyCode == BACKSPACE) {
if (anchorNum >= 0) {
myTransform.removeAnchor(anchorNum);
anchorNum = -1;
}
}

if (key == ’ ') {
int mode = myTransform.getWeightingMode();
if (mode == StrechTransform.SIMPLE) {
myTransform.setWeightingMode(StrechTransform.DIRECTIONAL);
println(“weightingMode = DIRECTIONAL”);
}
else {
myTransform.setWeightingMode(StrechTransform.SIMPLE);
println(“weightingMode = SIMPLE”);
}
}

if (key == ‘q’ || key == ‘Q’) {
myTransform.setWeightingExponent3(myTransform.getWeightingExponent3() + 0.5);
println("exponent3 = " + myTransform.getWeightingExponent3());
}
if (key == ‘a’ || key == ‘A’) {
myTransform.setWeightingExponent3(myTransform.getWeightingExponent3() - 0.5);
println("exponent3 = " + myTransform.getWeightingExponent3());
}
if (key == ‘w’ || key == ‘W’) {
myTransform.setWeightingExponent1(myTransform.getWeightingExponent1() + 0.5);
println("exponent1 = " + myTransform.getWeightingExponent1());
}
if (key == ‘s’ || key == ‘S’) {
myTransform.setWeightingExponent1(myTransform.getWeightingExponent1() - 0.5);
println("exponent1 = " + myTransform.getWeightingExponent1());
}
if (key == ‘e’ || key == ‘E’) {
myTransform.setWeightingExponent2(myTransform.getWeightingExponent2() + 0.5);
println("exponent2 = " + myTransform.getWeightingExponent2());
}
if (key == ‘d’ || key == ‘D’) {
myTransform.setWeightingExponent2(myTransform.getWeightingExponent2() - 0.5);
println("exponent2 = " + myTransform.getWeightingExponent2());
}
}

// helping function that calculates the difference of two angles
float angleDifference(float theAngle1, float theAngle2) {
float a1 = (theAngle1 TWO_PI + TWO_PI) TWO_PI;
float a2 = (theAngle2 TWO_PI + TWO_PI) TWO_PI;

if (a2 > a1) {
float d1 = a2 - a1;
float d2 = a1 + TWO_PI - a2;
if (d1 <= d2) {
return -d1;
}
else {
return d2;
}
}
else {
float d1 = a1 - a2;
float d2 = a2 + TWO_PI - a1;
if (d1 <= d2) {
return d1;
}
else {
return -d2;
}
}
}

// calculates the weighted average from a list of angles
float angleAverage(ArrayList angles, ArrayList weights) {
PVector res = new PVector();

for (int i = 0; i < angles.size(); i++) {
PVector v = new PVector(cos(angles.get(i)),
sin(angles.get(i)));
v.mult(weights.get(i));

res.add(v);

}

return atan2(res.y, res.x);
}

// calculates the weighted average from a list of angles
float angleAverage(ArrayList angles, float[] weights) {
PVector res = new PVector();

for (int i = 0; i < angles.size(); i++) {
PVector v = new PVector(cos(angles.get(i)),
sin(angles.get(i)));
v.mult(weights[i]);

res.add(v);

}

return atan2(res.y, res.x);
}

</>

Instead of pasting code, why not post links and make some attempt to credit the original author? It appears to come from here:

https://hartmut-bohnacker.de/projects/stretch-transform

If there is a sketch using this library on OpenProcessing, a link to that would be nice.

1 Like

I will keep that in mind, thanks =)