Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Last active January 26, 2022 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalgjose/006800734f365472180b2321b9fe9c6c to your computer and use it in GitHub Desktop.
Save amalgjose/006800734f365472180b2321b9fe9c6c to your computer and use it in GitHub Desktop.
Python program to find the points which is closer to a point P in a two dimensional plane.
import math
n = int(input("Enter the number of points\n"))
line_points = []
for i in range(n):
x, y = input("Enter the coordinate x,y \n").split(',')
coordinates = [int(x), int(y)]
line_points.append(coordinates)
xp, yp = input("Enter the coordinate of center point P \n").split(',')
pt = [int(xp), int(yp)]
dist = []
c = 0
least_distance = 0
for i in range(n):
xa = int(line_points[i][0])
ya = int(pt[0])
xb = int(line_points[i][1])
yb = int(pt[1])
# Euclidean distance formula
distance = math.sqrt(((xa - ya)**2) + ((xb - yb)**2))
dist.append(distance)
# Finding the least distance value
if distance <= least_distance:
least_distance = distance
# Finding the points with least distance
c_list = list()
for i in range(len(dist)):
if dist[i] == least_distance:
c_list.append(line_points[i])
print("Points which are closer to the point P : ", tuple(c_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment