Can 4 threads draw in one display at once?
in
Programming Questions
•
1 year ago
Hi! I'm trying to optimize a program by dividing one task into four threads. On each frame I want to draw 10 collections of 600 lines, so I'm creating four threads. The first two draw 2 collections, the next two 3 collections (2+2+3+3=10).
The lines must be drawn using rect() because if I use line() there are very ugly position rounding errors. That means I must translate() and rotate() the rect() into the right position. If I used lines I could just calculate the position of both ends and avoid translating and rotating.
Now, the result is quite cool and messed up :) Not exactly what I want. The problem is (I think) that each traslate() and rotate() is interfering with the other calls to the same functions in the other threads, so the translations and rotations pile up. I even get some crazy huge rectangles for free.
Can this problem be distributed at all into several threads?
- void setup() {
- size(600, 300);
- colorMode(HSB, 1);
- noStroke();
- smooth();
- }
- void draw() {
- background(0);
- float f = frameCount / 88.0;
- SimpleThread t1 = new SimpleThread(0, 1, f);
- t1.start();
- SimpleThread t2 = new SimpleThread(2, 3, f);
- t2.start();
- SimpleThread t3 = new SimpleThread(4, 6, f);
- t3.start();
- SimpleThread t4 = new SimpleThread(7, 9, f);
- t4.start();
- if (frameCount % 50 == 0) {
- println(int(frameRate));
- }
- }
- class SimpleThread extends Thread {
- int yfrom;
- int yto;
- float f;
- SimpleThread (int from, int to, float rf) {
- yfrom = from;
- yto = to;
- f = rf;
- }
- void run () {
- for (int y = yfrom; y <= yto; y++) {
- float they = y/10.0;
- for (int x=0; x < width; x++) {
- float thex = x/100.0;
- fill((they + thex + f) % 1.0, 1, 1);
- translate(x, 50+y*20);
- rotate(they + thex + f);
- rect(0, 0, 1, 15);
- resetMatrix();
- }
- }
- }
- void quit() {
- interrupt();
- }
- }
1