# Distance Check within an angle/cone

**URL:** <https://discourse.processing.org/t/distance-check-within-an-angle-cone/39640>\
**Category:** Coding Questions\
**Created:** [November 9, 2022, 6:46am UTC](https://discourse.processing.org/t/distance-check-within-an-angle-cone/39640 "2022-11-09T06:46:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SolBreaker](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@SolBreaker](https://discourse.processing.org/u/SolBreaker)\
**Post date:** [November 9, 2022, 6:46am UTC](https://discourse.processing.org/t/distance-check-within-an-angle-cone/39640/1 "2022-11-09T06:46:33Z")

</div>

Currently, I am attempting a raycasting technique with a line-circle interception (which is not working), and I was wondering if there is a way to limit a distance check within a cone in 2D. For reference, I am trying to make a sort of plane game, with radar detection, with my inspiration attached:

[Twitter Post](https://twitter.com/thegamesarefake/status/1568611147124973570)

If it is not possible, I can revert back to my raycasting, and I would ask if anybody had a line-circle intersection equation, of course taking into account the position of the circle, which I have not been able to find online. I can provide my project if needed.

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [November 9, 2022, 6:00pm UTC](https://discourse.processing.org/t/distance-check-within-an-angle-cone/39640/2 "2022-11-09T18:00:09Z")

</div>

Take a look at [https://raytracing.github.io/books/RayTracingInOneWeekend.html](https://raytracing.github.io/books/RayTracingInOneWeekend.html) chapters 5 and 6. A circle is just a flat sphere so the same code works for each.

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [November 9, 2022, 9:25pm UTC](https://discourse.processing.org/t/distance-check-within-an-angle-cone/39640/3 "2022-11-09T21:25:16Z")

</div>

To get a “view cone” you can use the dot-product to calculate the angle between vectors.

```auto
// Notes: Vectors used in dot-product must be normalized to get the correct results
// Calculations also work in higher dimensions
// This example does not account for the radius of a circle/sphere
PVector center;

int DOT_COUNT = 500;
float HALF_VIEW_ANGLE = 15.0;
// our field of view is double since angle can be left or right
float threshold = cos( radians(HALF_VIEW_ANGLE) );

void setup()
{
  size(512, 512, P2D);
  center = new PVector(width/2, height/2);
}

void draw()
{
  background(0);
  PVector m = new PVector(mouseX, mouseY);
  PVector mdir = m.copy().sub(center).normalize();
  
  randomSeed(42); // use the same points
  
  for (int i=0; i<DOT_COUNT; i++)
  {
    PVector p = new PVector(random(width), random(height));
    PVector pdir = p.copy().sub(center).normalize();
    
    // Dot product
    // result is the cosine of angle between vectors...
    float ca = mdir.dot(pdir);
    
    // ...which matches our calculated threshold
    if (ca > threshold)
    {
      stroke(255,255,0);
    }
    else
    {
      stroke(0,0,255);
    }
    ellipse(p.x, p.y, 2, 2);
  }
  
}

```
