# Line & Intersection

**URL:** <https://discourse.processing.org/t/line-intersection/1118>\
**Category:** Coding Questions\
**Created:** [June 20, 2018, 11:22am UTC](https://discourse.processing.org/t/line-intersection/1118 "2018-06-20T11:22:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rioma7](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@rioma7](https://discourse.processing.org/u/rioma7)\
**Post date:** [June 20, 2018, 11:22am UTC](https://discourse.processing.org/t/line-intersection/1118/1 "2018-06-20T11:22:36Z")

</div>

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

```auto
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:

```auto
  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

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 21, 2018, 2:27am UTC](https://discourse.processing.org/t/line-intersection/1118/2 "2018-06-21T02:27:09Z")

</div>

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

```java
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
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 23, 2018, 2:35am UTC](https://discourse.processing.org/t/line-intersection/1118/3 "2018-06-23T02:35:32Z")

</div>

> [@rioma7](#):
>
> if the line intersect the circle then don’t draw.

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.
