Question

In: Computer Science

Closest and Farthest points. Distance between two points (x1,y1) and (x2, y2) can becalculated...

Closest and Farthest points. Distance between two points (x1, y1) and (x2, y2) can be
calculated as d = sqt( (x1 − x2) ^2 + (y1 − y2)^ 2)
. You are given the coordinates of a source
point and three destination points. Write a Python program stored in a file q8.py that
takes these information as input and determines the closest and farthest destination
points from the source point.

ex:

Enter source coordinates : 0 0
Enter point A coordinates : 1 4
Enter point B coordinates : -4 5
Enter point C coordinates : -2.4 -0.9

From source point (0.0 , 0.0) ,
Point C ( -2.4 , -0.9) is closest , with distance of 2.56 units .
Point B ( -4.0 , 5.0) is farthest , with distance of 6.4 units

Solutions

Expert Solution

def readPoint(prompt):
        points = input(prompt)
        points = points.split()
        return float(points[0]), float(points[1])

def distance(p1, p2):
        return ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5

points = []

points.append(readPoint('Enter source coordinates : '))
points.append(readPoint('Enter point A coordinates : '))
points.append(readPoint('Enter point B coordinates : '))
points.append(readPoint('Enter point C coordinates : '))

# assume first point to be result
maxDis = distance(points[0], points[1])
maxDisPoint = points[1]
minDis = distance(points[0], points[1])
minDisPoint = points[1]

# check if better points are present
for i in range(2, len(points)):
        d = distance(points[0], points[i])
        if d > maxDis:
                maxDis = d
                maxDisPoint = points[i]
        if d < minDis:
                minDis = d
                minDisPoint = points[i]

print('From source point', points[0])
print('Point', minDisPoint, ' is closest, with distance of', minDis, 'units')
print('Point', maxDisPoint, ' is farthest, with distance of', maxDis, 'units')

Related Solutions

1. Let ρ: R2 ×R2 →R be given by ρ((x1,y1),(x2,y2)) = |x1 −x2|+|y1 −y2|. (a) Prove...
1. Let ρ: R2 ×R2 →R be given by ρ((x1,y1),(x2,y2)) = |x1 −x2|+|y1 −y2|. (a) Prove that (R2,ρ) is a metric space. (b) In (R2,ρ), sketch the open ball with center (0,0) and radius 1. 2. Let {xn} be a sequence in a metric space (X,ρ). Prove that if xn → a and xn → b for some a,b ∈ X, then a = b. 3. (Optional) Let (C[a,b],ρ) be the metric space discussed in example 10.6 on page 344...
The parametric equations x = x1 + (x2 − x1)t,    y = y1 + (y2 − y1)t...
The parametric equations x = x1 + (x2 − x1)t,    y = y1 + (y2 − y1)t where 0 ≤ t ≤ 1 describe the line segment that joins the points P1(x1, y1) and P2(x2, y2). Use a graphing device to draw the triangle with vertices A(1, 1), B(4, 3), C(1, 6). Find the parametrization, including endpoints, and sketch to check. (Enter your answers as a comma-separated list of equations. Let x and y be in terms of t.)
We define a relation ∼ on R^2 by (x1,y1)∼(x2,y2) if and only if (y2−y1) ∈ 2Z....
We define a relation ∼ on R^2 by (x1,y1)∼(x2,y2) if and only if (y2−y1) ∈ 2Z. Show that the relation∼is an equivalence relation and describe the equivalence class of the point (0,1).
Two identical loudspeakers are located at (x1, y1) = (0, 0) and (x2, y2) = (2.5...
Two identical loudspeakers are located at (x1, y1) = (0, 0) and (x2, y2) = (2.5 m,−0.5 m). The loudspeakers are driven by separate amplifiers, so that the loudspeakers are out of phase with each other, and produce sound waves with frequency of 650 Hz. The ambient temperature of air is 37 0C. A small microphone is moved along the y-axis. 1. At what distances along the y-axis will there be constructive interference? 2. At what distances along the y-axis...
Let X1,X2,X3 be i.i.d. N(0,1) random variables. Suppose Y1 = X1 + X2 + X3, Y2...
Let X1,X2,X3 be i.i.d. N(0,1) random variables. Suppose Y1 = X1 + X2 + X3, Y2 = X1 −X2, Y3 =X1 −X3. Find the joint pdf of Y = (Y1,Y2,Y3)′ using : Multivariate normal distribution properties.
Suppose that X1, X2, , Xm and Y1, Y2, , Yn are independent random samples, with...
Suppose that X1, X2, , Xm and Y1, Y2, , Yn are independent random samples, with the variables Xi normally distributed with mean μ1 and variance σ12 and the variables Yi normally distributed with mean μ2 and variance σ22. The difference between the sample means, X − Y, is then a linear combination of m + n normally distributed random variables and, by this theorem, is itself normally distributed. (a) Find E(X − Y). (b) Find V(X − Y). (c)...
The set R^2 with addition and scalar multiplication defined by (x1, y1) + (x2, y2) =...
The set R^2 with addition and scalar multiplication defined by (x1, y1) + (x2, y2) = (x1 + x2, y1 + y2) c(x1, y1) = (cx1, y1) is not a vector space. Determine which axiom fails and find a counterexample that shows that it fails.
Consider a random sample (X1, Y1), (X2, Y2), . . . , (Xn, Yn) where Y...
Consider a random sample (X1, Y1), (X2, Y2), . . . , (Xn, Yn) where Y | X = x is modeled by Y=β0+β1x+ε, ε∼N(0,σ^2), where β0,β1and σ^2 are unknown. Let β1 denote the mle of β1. Derive V(βhat1).
Show that the set ℝ2R2, equipped with operations (?1,?1)+˜(?2,?2)=(?1+?2+1,?1+?2−1)(x1,y1)+~(x2,y2)=(x1+x2+1,y1+y2−1) ? ⋅˜ (?,?)=(??+?−1,??−?+1) (1)defines a vector space...
Show that the set ℝ2R2, equipped with operations (?1,?1)+˜(?2,?2)=(?1+?2+1,?1+?2−1)(x1,y1)+~(x2,y2)=(x1+x2+1,y1+y2−1) ? ⋅˜ (?,?)=(??+?−1,??−?+1) (1)defines a vector space over ℝR. (2)Show that the vector space ?V defined in question 1 is isomorphic to ℝ2R2 equipped with its usual vector space operations. This means you need to define an invertible linear map ?:?→ℝ2T:V→R2.
Find two integer pairs of the form (x,y) with |x|<1000 such that 22x+28y=gcd(22,28) (x1,y1)=( (x2,y2)=(
Find two integer pairs of the form (x,y) with |x|<1000 such that 22x+28y=gcd(22,28) (x1,y1)=( (x2,y2)=(
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT