For a point specified by the x and y coordinates check whether it belongs to the shaded area in the javascript drawing

enter a description of the image here

Can't solve with circle

Triangle:

x=parseFloat(prompt('x')) 
y=parseFloat(prompt('y')) 
if(x>=0 && x<=6 && y<=0 && y>=-6) 
document.write('true') 
else document.write('false') 
Author: OPTIMUS PRIME, 2020-02-27

2 answers

For the entire shape: x >= 0 && y >= x - 6 && y * y + x * x <= 36

 4
Author: Igor, 2020-02-28 02:48:59

In fact, the circle is easier (it seemed to me), because there you just need the Pythagorean theorem: x² + y² must be less than the square of the radius

And with a triangle, the only thing that came up: take one fixed point (6; 0) and through the second pair of points that will be entered, draw a straight line and check its angle formed with the X-axis. If it is less than 45, then it hits.

And since boolean expressions are written in return, it immediately returns true or false.

function isInside(x, y){
  if( x < 0 ) return false;
  
  if( 0 <= y && y <= 6 && 0 <= x && x <= 6 ){ /* Квадрат с сектором */
    return x*x + y*y <= 36;
  }
  
  if( -6 <= y && y <= 0 && 0 <= x && x <= 6){ /* Квадрат с треугольником */
  
    // `x1` → 6, `y1` → 0,
    // `x2` → x, `y2` → y
    // tg a = (y2 - y1) / (x2 - x1)
    
    let angle = Math.atan( Math.abs( y / (x - 6) ) ) * 180/Math.PI;
    
    return angle <= 45;
  }
}

// Треугольник
console.log( 3, -3, isInside(3, -3) ); // true;
console.log( 2, -3, isInside(2, -3) ); // true;
console.log( 4, -3, isInside(4, -3) ); // false;

// Сектор
console.log( 2, 2, isInside(2, 2) ); // true;
console.log( 2, 5, isInside(2, 5) ); // true;
console.log( 4, 4, isInside(4, 4) ); // true;
console.log( 4, 5, isInside(4, 5) ); // false;
 2
Author: OPTIMUS PRIME, 2020-02-28 01:47:35