Draw a dotted line with the mouse using if statements

Hi everyone im a beginner and im not sure how to approach this question. The hint part of the question is confusing me and everything ive tried hasn’t given me a dotted line. Does it want us to draw two different lines or change the color of the same line upon keyPressed? It seems like something simple that I’m overcomplicating tbh but I can’t tell what.

Either way how would you go about solving this question? You can only use if statements and booleans to solve it, not loops.

thank you :’)

It is stated that the dashing is made by alternating between black and white when drawing.

How do you draw a solid line?
Did you look at pmouseX,pmouseY? It stand for previous mouse position.

Just alternate by toggling a boolean variable dashingFlag and evaluate it with if:

if(dashingFlag) 
  stroke(255); 
   else 
  stroke(0);

Right before drawing a line

But that would just change the stroke color to black and draw a black line instead of a line with alternating color.

with the if statement set up that way^ and the line command like

line(mouseX, mouseY, pmouseX, pmouseY); 

a solid line is drawn that changes colour and becomes dotted on keyPressed, it doesn’t draw small sectional lines of black and white like the question wants.

im sorry if I didn’t apply your input correctly but that’s what I got from it.

I think the task presumes that your background color is white.

When you then draw a line that alternates between white and black, it’s a dashed line.

That is also stated clearly in the assignment.

Anyway, post your entire, runable code and let’s work from there.

it alternates between white and black every frame which is rather fast. Did you try it? The idea is that during drawing the color changes so fast, that the lines becomes dashed.

Did you test it?

this doesnt work as intended, what needs to be changed?

boolean dash = true;

void setup(){
  background(100);
  size(500,500); 
}  
 
void draw(){
  if(dash){
    stroke(255);
}
    else{
      stroke(0);
      line(mouseX, mouseY, pmouseX, pmouseY);
    } 

}

I didn’t add the keyPressed command into the code here cz im not sure if I should use the boolean variable keyPressed or the function keyPressed, neither works with this attempt tho lol :’)

The bracket } needs to be after the stroke command.

Now the line command is inside the else part of the if-clause. Bad.

You can use ctrl-t in processing.

Then you get auto-format and you can spot such mistakes by the indent of the lines.

Use the former please

Surround your current if else clause by

  • an if clause with keyPressed and
  • set the new else block (so insert a new else block please, for keyPressed==false) so that it sets the color to black (solid line)

Pseudo-Code

if(keyPressed) {
    // dashed line 
    if(dash){
       stroke(255);
       dash=false; 
    }
    else{
      stroke(0);
      dash=true; 
    }
} // ------------------
else {
   // solid line
   stroke(0);  // standard color for solid line
} //else 


line(mouseX, mouseY, pmouseX, pmouseY);

not tested.

Hello @onyx88,

I worked on this from the inside out.

This will alternate the background:

// ALternate Colors
// v1.0.0
// GLV 2022-03-22

boolean toggle = false;

void setup() 
  {
  size(100, 100);
  frameRate(5); //Slowed things down. Do NOT use in final code.
  }

void draw() 
  {
  toggle = !toggle; // ! is the NOT operator
  println(toggle);
  
  if(toggle)
    {
    background(0);
    }
  else 
    {
    background(255, 255, 0);   
    }    
  }	

You can use the same concept to set the stroke() color of your line to alternate every frame.

References:

I then wrapped this around above code:

 if(mousePressed)
    {
    if (keyPressed)
      {
     // Code here...
      }
    else
     {
     // Code here...
     }
   }

And voila!

image

:)

AWESOME! Thank you!!

2 Likes