I created code, that animates creation of lissajous table. It works properly, but sometimes, when the first circle travels almost entire lap it slows down dramatically. How can I optimize this code:
void calculations()
{
float line1 = 0, plane = 0, line2 = 0;
float inside = 0, sizePlusOfset = size + offset;
if (whatLine)
{
line1 = cols;
line2 = rows;
plane = height;
centerY = offset;
} else
{
line1 = rows;
line2 = cols;
plane = width;
centerX = offset;
}
for (int i = 0; i < line1; i++)
{
if (whatLine)
{
centerX = sizePlusOfset + i * size;
} else
{
centerY = sizePlusOfset + i * size;
}
inside = angle * (i + factor) - HALF_PI;
x = radius * cos(inside);
y = radius * sin(inside);
for (int j = 0; j < line2; j++)
{
if (whatLine)
{
curves.get(j).get(i).setX(centerX + x);
} else
{
curves.get(i).get(j).setY(centerY + y);
}
}
strokeWeight(1);
stroke(255, 230);
ellipse(centerX, centerY, diameter, diameter);
strokeWeight(8);
point(centerX + x, centerY + y);
stroke(255, 150);
strokeWeight(1);
if (whatLine)
{
line(centerX + x, 0, centerX + x, plane);
} else
{
line(0, centerY + y, plane, centerY + y);
}
}
}
void show(boolean lisajousAction)
{
for (int i = 0; i < 2; i++)
{
calculations();
whatLine = !whatLine;
}
for (int j = 0; j < rows; j++) {
for (int i = 0; i < cols; i++) {
if (lisajousAction)
{
curves.get(j).get(i).addPoint();
}
curves.get(j).get(i).show();
}
}
angle -= angleChange1 * delta_time;
// If the angle is - two pi, the first circle has traveled the entire lap.
float inverseFactor = abs(1/factor);
if (angle < -TWO_PI * inverseFactor) {
for (int j = 0; j < rows; j++) {
for (int i = 0; i < cols; i++) {
curves.get(j).get(i).reset();
}
}
angle = 0;
}
}
I know that one way to optimize this code would be to check if a given pixel is entered into the table and not enter it into the table, but I don’t know how to do it optimally. Entire code needed to run it is here: Lisous_Table - Pastebin.com