Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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