A while loop is causing some graphical glitches - processing

I was just messing around with the while loop, testing it vs the for loop, when this graphical glitch occurred. This happens on an inconsistent basis, so I do refresh it to get results, but occasionally the screen will be split into horizontal lines, each with their different color. Why is this occurring?
int i = 0;
void setup() {
size(400, 400);
}
void draw() {
while(i < 1) {
background(random(255),random(255),random(255));
}
println("yes");
}
void mousePressed() {
i += 1;
}

Related

Variables and conditions in processing

Just wondering if I want to do a loop to make every time I mousePressed to draw a circle and each time fill with 3 different colour eg yellow orange and blue and so on I use for(int countmouseClick=n; n<=3 ; n++) how should I finish the rest? Should I reset n if n=3 to n= 0?. The circles can stay on the canvas
Not sure if it correct.
void mousePressed(){
for ( int count=0; n<= 2; n++){
fill(255,0,0);
fill(0,255,0);
fill(0,0,255);
}
circle(mouseX,mouseY,20);
}
//i have adopted the idea of %, it telling me the variable n doesn't exist, why is this
int count;
void setup(){
size(400,400);
background(255);
}
void draw(){
}
void mousePressed(){
int count=n;
if(n>2){ //reset mouse click to 0
n=0;
}
for(int n= 0;n<=2;n++){//count mouseclicked, if n<=2, n=n+1
if(n%3=0){
fill(255,0,0);
}else if (n%3=1){
fill(0,255,0);
}
else {
fill(0,0,255);
}
circle(mouseX,mouseY,20);
}
}
a better way of writing the code is to write a helper function which takes countmouseclick and return the color
you can read these articles for understanding the code below
https://processing.org/reference/modulo.html
https://processing.org/reference/switch.html
Color getColor(int countMouseClick){
switch(countMouseClick%3) {
case 0:
return COLOR.ORANGE;
case 1:
return COLOR.YELLOW;
break;
case 2:
return COLOR.BLUE;
default:
return COLOR.GREEN;
}

my code is slow and I have no idea how to fix it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm making a program that you can draw in and when there are more than 38000 _points it starts to lag.
here's the code:
public class _point {
float x = 0;
float y = 0;
boolean active = true;
color c = color(255, 255, 255);
public void _point() {
}
public void change_pos(float x1, float y1, color c1)
{
x = x1;
y = y1;
c = c1;
}
public void u_line(float px, float py)
{
if (dist(x, y, px, py) < 15)
{
stroke(c);
strokeWeight(5);
line(x, y, px, py);
}
}
public void remove_beans()
{
if (eraser) {
if (dist(x, y, mouseX, mouseY)<main_tool.radius/2) {
active = false;
}
}
}
public void a_line()
{
for (int i = 0; i < _points.size(); ++i) {
_point part = _points.get(i);
if (dist(x, y, part.x, part.y) < 15)
{
line(x, y, part.x, part.y);
}
}
}
}
and where its drawn:
ArrayList<_point> _points = new ArrayList<_point>();
void draw_beans() {
for (int i = 0; i < _points.size(); ++i) {
_point part = _points.get(i);
if (part.active == false) {
_points.remove(i);
}
if (i != 0 && i != _points.size()-1 && i != _points.size()) {
OPTIMIZE(_points.get(i+1), part, _points.get(i-1));
}
if (i != 0) {
_point past = _points.get(i-1);
part.u_line(past.x, past.y);//past.x,past.y
}
}
}
this is were the points are added to the array:
void add_beans() {
if (!eraser && !IIIINNZ()) {
_points.add(new _point());
_point part = _points.get(_points.size()-1);
part.change_pos(mouseX-transX, mouseY-transY, color(int(color_text0.texts), int(color_text1.texts), int(color_text2.texts)));
}
for (int i = 0; i < _points.size(); ++i) {
if (eraser && !IIIINNZ()) {
_point part = _points.get(i);
part.remove_beans();
}
}
}
and an optimization:
void OPTIMIZE(_point next, _point thiss, _point prev) {
if (dist(thiss.x, thiss.y, next.x, next.y)+dist(thiss.x, thiss.y, prev.x, prev.y)<14) {
thiss.active = false;
}
}
setup:
void setup() {
size(512, 512);
noCursor();
}
draw:
void draw() {
background(#222833);//27,27,33
textFont(font);
pushMatrix();
translate(transX, transY);
draw_beans();
popMatrix();
show_options();
main_tool.update();
println(_points.size());
}
I have tried to multithread it but the thread doesn't align with the animation thread so there are missing points.
I am using the Processing 3 java library and vscode.
It's unclear what OPTIMIZE and IIIINNZ do.
Without being able to run your code and test I can't tell if those function slow it down or is it simply the (re)rendering.
I advise using VisualVM to Profile the CPU usage of your sketch(PApplet subclass). The CPU Profiler will list the methods from the slowest:
Focus your efforts on the top slowest and try not to sacrifice code readability if possible.
(On code readability I recommend using a Java Style guide overall. It will save time scanning/reading longer and longer programs)
Your question reminds me a little bit of an older one I answered a while ago.
It's similar because of the many lines rendered. In addition yours uses distance() (which in turn uses Math.sqrt() which can cost the CPU a bit, but as much as rendering. You could use squared distance instead, but that will make the code a bit harder to read and if compared to rendering isn't that slow I'd leave that in).
Essentially there are many point instances that render lines based on threshold distances. Each stroke(), strokeWeight(), line() call will have it's cost.
I've tried to group line rendering into something simpler: a single shape accessing all the point instances:
void draw_beans() {
beginShape(LINES);
for (int i = 0; i < points.size(); ++i) {
Point part = points.get(i);
if (part.active == false) {
points.remove(i);
}
//if (i != 0 && i != points.size()-1 && i != points.size()) {
// OPTIMIZE(points.get(i+1), part, points.get(i-1));
//}
if (i != 0) {
Point past = points.get(i-1);
//part.u_line(past.x, past.y);//past.x,past.y
vertex(past.x, past.y);
}
}
endShape();
}
This barely made a dent and even if it would've been waaay faster it would've been limiting.
It's worth baring in mind how line() is implemented behind the hood for each renderer. Surprisingly, the FX2D renderer might work better for your setup (e.g. size(512, 512, FX2D);)

Processing Translations

In the processing language I am trying to create a translation similar to the image below:
Output Goal
In my code, the image is moving but it isn't showing the original picture in addition to the translation and it isn't being shown across the screen as i'd like.
I have included the code I have so far below:
PImage img;
int reps=10;
void setup()
{
size(600,120);
triangle(30,5,50,30,15,20);
save("image.png");
img=loadImage("image.png");
}
void draw()
{
for (int i=0; i<reps; i++);
{
pushMatrix();
image(img,0,0);
translate(img.height,0);
scale(-1,1);
image(img,0,0);
popMatrix();
}
}
This is what it produces so far:
current_output
Im happy it's translating, I am just trying to figure out how to see the original in addition to the translation and have it shown multiple times.
Thanks in advance!!
The images have to be translated differently depending on the index. The 2nd image has to be translated more than the 1st one and the 3rd more than the 2nd:
translate(img.height * i, 0);
function draw:
void draw()
{
for (int i=0; i<reps; i++);
{
pushMatrix();
translate(img.height * i, 0);
scale(-1,1);
image(img,0,0);
popMatrix();
}
}

Processing: Rendering stops when delay function used

I created a simple delay function for my sketch and tried to use it, but it seems as if the rendering stops i.e., there is simple a grey screen and then everything is rendered all at once.
Could someone please tell me where I am going wrong? What exactly is happening?
Also how are draw() and setup() defined internally? I understand that setup() is a one time render and draw() like an infinite loop.
Code:
void delay(int delay)
{
int time = millis();
while(millis() - time <= delay);
}
void setup(){
size(600,400);
smooth();
}
void draw(){
background(0); //black
delay(1000);
fill(255); //white
ellipse(width/2, height/2, 300, 300);
delay(1000);
}
Also how are draw() and setup() defined internally? I understand that setup() is a one time render >and draw() like an infinite loop.
The thing is that draw() only renders a frame at the end of each execution, so stoping it is usually not a good idea.
http://wiki.processing.org/w/I_display_images_in_sequence_but_I_see_only_the_last_one._Why%3F
If you want to time things use booleans as flags for when things should happen.
check those also:
http://wiki.processing.org/w/What_are_setup()_and_draw()%3F
http://wiki.processing.org/w/How_do_I_display_a_message_for_a_few_seconds%3F
If you want delay, use a Thread() object, like the following code, to run the delays in a separate thread:
void delay(int delay)
{
try
{
Thread.sleep(delay);
}
catch(Exception EX)
{
}
}
void setup(){
size(600,400);
smooth();
thread("sleepy"); // the function sleepy will be started in a new thread
}
void draw(){
// draw() does nothing here except refresh the screen
}
void sleepy()
{
while(true)
{
background(0); //black
delay(1000);
fill(255); //white
ellipse(width/2, height/2, 300, 300);
delay(1000);
}
}
Additionally, the above delay method, Thread.sleep(), does not consume an entire CPU core as it runs (The millis() method is akin to a temporary infinite loop).
Tested on Processing 2.2.1, Windows 7 professional.

Use of delay() in Processing Environment

I am using Processing language to sketch a rectangle that grows in size with time. Following code is not giving any output.
void setup()
{
size(900,900);
}
void draw()
{
int edge=100;
for(int i=0;i<300;i++)
{
delay(100);
edge++;
rect(100,100,edge,edge);
}
}
I suspect having wrongly used delay() function.
Here is one such "roll your own" delay method which is good for most purposes. Just change the values passed into the delay method to alter the timing. This just outputs "start" and "end" roughly every 2 seconds for example.
void draw()
{
System.out.println("start");
delay(2000);
System.out.println("end");
delay(2000);
}
void delay(int delay)
{
int time = millis();
while(millis() - time <= delay);
}
I recommend rolling your own delay system using the millis() function.
Have a look at this example.
With processing, the screen does not get refreshed until the program flow reaches the end of draw()
Try the following:
void setup()
{
size(900,900);
frameRate(10);
}
int edge = 100;
void draw()
{
edge++;
rect(100,100,edge,edge);
}

Resources