Horje
Find if a Point Lies Inside a Circle in JavaScript

A circle is a closed curve where every point on the curve is equidistant from a fixed point called the center. The problem at hand involves determining whether a given point is located inside a circle or not. Given a circle (coordinates of center and radius) and a point (coordinate), find if the point lies inside or on the circle, or not using JavaScript

Examples: 

Input: x = 4, y = 4 
circle_x = 1, circle_y = 1, rad = 6; // Circle
Output: Inside

Input: x = 3, y = 3
circle_x = 0, circle_y = 1, rad = 2; // Circle
Output: Outside

These are the following approaches:

Using Distance Formula

One straightforward method to determine if a point lies inside a circle is by using the distance formula. The distance between the center of the circle (x1, y1) and the given point (x2, y2) is calculated using the formula:

distance = √((x2 - x1)² + (y2 - y1)²)

Note: If the calculated distance is less than the radius of the circle, i.e. point lies inside the circle.

Example: Implementation to find if a point lies inside a Circle using Distance Formula.

JavaScript
function pointInsideCircle(x, y, cx, cy, radius) {
    const distance =
        Math.sqrt((x - cx) ** 2 + (y - cy) ** 2);
    return distance < radius;
}
// Given Coordinates 
const x = 2, y = 3;
// Coordinates of the center of the circle
const cx = 0, cy = 0;
// Radius of the circle
const radius = 5;

console.log(pointInsideCircle(x, y, cx, cy, radius));

Output
true

Time complexity: O(1)

Space complexity: O(1)

Using Circle Equation Approach

Another approach involves using the equation of a circle. The equation of a circle with center (h, k) and radius r is:

(x - h)² + (y - k)² = r²

If a point (x, y) satisfies this equation, it lies on the circle. To determine if the point lies inside the circle, we just need to check if the distance from the center (h, k) to (x, y) is less than the radius r.

Example: Implementation to find if a point lies inside a Circle using Circle Equation Approach.

JavaScript
function pointInsideCircle(x, y, cx, cy, radius) {
    const distanceSquared = 
        (x - cx) ** 2 + (y - cy) ** 2;
    return distanceSquared < radius ** 2;
}
// Given Coordinates 
const x = 2, y = 3;
// Coordinates of the center of the circle
const cx = 0, cy = 0;
// Radius of the circle
const radius = 5;

console.log(pointInsideCircle(x, y, cx, cy, radius)); 

Output
true

Time complexity: O(1)

Space complexity: O(1)




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Decode URI in TypeScript? How to Decode URI in TypeScript?
JavaScript Program to Compute the Bitwise OR of all Numbers in a Range JavaScript Program to Compute the Bitwise OR of all Numbers in a Range
JavaScript Program to Print the Nth Row of Pascal’s Triangle JavaScript Program to Print the Nth Row of Pascal’s Triangle
JavaScript Program to Find Number Pairs (x, y) in an Array Such That x^y &gt; y^x JavaScript Program to Find Number Pairs (x, y) in an Array Such That x^y &gt; y^x
How to Create Full-Page Tabs using CSS &amp; JavaScript? How to Create Full-Page Tabs using CSS &amp; JavaScript?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13