Calling different draw functions

Hello! I followed TheCodingTrain’s youtube video for the Rubik’s cube, and am now adding some functionality which I need help with. I am using java mode. I created a simple solver (brute force), and now I want to create a visualizer that shows how the solver searches the tree of possible solutions. I need some help to get me started with the drawing code.

what I have is something like this:
class Cube{ methods for turning cube}
class Solver {
takes Cube → constructs tree → searches tree for solution
// i want to add a drawTree() function that draws each step of creating and searching the tree, the
// algorithm for drawing I can try and figure out
}

void draw(){
if(boolean solving==true){
solver.drawTree();
}

else drawCube();
}

I don’t know how to make it draw() interact with solver.drawTree();. I made so that when I press button to start solving (creating the tree), i then call drawTree(); manually from inside the solver and it enters the drawTree function, but it isnt drawing anything from there and the cube stays on the screen. Other thing I tried is when I press button → set solving = true, then start solving. But the draw() never checks the boolean while the solving is in progress, so it doesnt call drawTree. Any suggestions?

Sorry if this is vague, ask clarifying questions and I’ll try to answer!

Update: I can make the void draw() call the drawTree() function, but it doesn’t actually draw anything on the screen. I am trying to just draw some lines at first. drawTree looks like this:

void drawTree(){
println(“drawTree”);
for(int i = 0; i < 50000; i++){
line(50,50,200,200,i,i+100);
}
}
I am using P3D so I figure I need to give 3D coordinates. Anyone know what I am doing wrong?

hi
about functions helpful videos

Function parameters and return values

Use a function to simplify a program

Draw gradients, review functions and image loading

Functions help keep code organized

void setup (){
  size(600,600,P3D);

}


void draw(){
drawTree();
}
void drawTree(){
println("drawTree");
for(int i = 0; i < 50000; i++){
line(50,50,200,200,i,i+100);
}
}

thanks, that makes sense. It actually calls the drawTree that way, but the lines are not showing on the screen. Do you know why?

The thing is that draw() only updates the screen at the end and not throughout

And you cannot force the screen to update.

The work around is that you use thread and it changes an image that you can display in draw () - when you do it right. I posted one example here

And jeremy douglas made another approach

links see below -

1 Like

You keep increasing the z = i+100 from 100 to 50100 and it is out of the field of view.

Reference:
https://processing.org/reference/line_.htmlhttps://processing.org/reference/line_.html

:)

I see, I will try that approach. Thanks for the suggestion :+1:

There is lines on the top left

Oh, for me they don’t show, originally it showed just the Cube and didnt do anything, but with your version it actually stopped drawing the cube (like I wanted), but I couldnt see the lines. They might have been somewhere out of the camera for me.

Thank you! That is probably my issue since I got it to stop drawing the cube but I didnt see the lines.

@glv @jafal (and Chrisir)
Thank you guys, I got it to draw the lines now. I can start working on my solution from here. Thank you so much for helping me so quickly!

1 Like

I don’t really see how this would help

Because you would see the lines only at the end of draw() and not throughout

@Chrisir hi

I understand that he just want to activate his function void drawTree(){ and his demand is the function Regardless of the content of the function

Because he used experimental values

1 Like

I wrote solvers myself. They run a long time figuring stuff out. When OP wants a solver that has a visible animation, your approach won’t work.

1 Like

for the thread / image approach see Image resize animation and duplication using squareroot size - #13 by Chrisir

for Jeremy’s approach see Update screen during draw() without thread?