Line & Intersection

Hello people,

I would like to draw lines from side to side of the canvas [random points] but I don’t want that it draws if the drawn line intersect a circle, which is in the middle. My code is this

void setup(){
  background(0);
  size (800, 400);
}

void draw(){
  stroke(255);
  line(random(0,800), 0 , 0, random(0,400));

  fill(0);
  ellipse(width/2, height/2, 100,100);
}

the problem acutally is that this program only draws lines from the top side to the left side because I don’t know how to tell him:

  line(random(0,800), 0 or 400 , 0 or 800, random(0,400));

the second problem is that I really don’t know how to tell him: if the line intersect the circle then don’t draw.
Can you help me?

Thanks

1 Like

How many lines do you want to draw?

One trick is to draw the lines first. Then draw the circle setting its fill to the same color as the background. Pseudocode below.

Kf

final int BG_COLOR=0;  //Black

void setup(){

   ....
   fill(BG_COLOR);
   stroke(255);  //Color: White for lines and circle edges
   noLoop();
}

void draw(){
    background(BG_COLOR);  
 
   //Draw lines here
   for(int i=0;i<=N_LINES;i++){
       ....
   } 

  //Now ellipse draw on top of the lines
  ellipse(width/2, height/2, 100,100);
}

void mousePressed(){
    redraw();  //Updates sketch when mouse is clicked
}

1 Like

One approach:

  1. draw background
  2. draw lines
  3. draw a filled circle the same color as background

Another approach:

  1. draw lines on PGraphics
  2. create an inverted circle PGraphics as a mask
  3. apply the mask image to the lines image
  4. display the PGraphics on your main canvas

Another approach:

  1. Use circle-line collision detection to find the point where a line enters / exits the circle.
  2. Draw two lines – one segment up to the circle, one segment after.
1 Like