Determine the maximum and minimum distances between 4 points in a 3-space. The u
ID: 3556574 • Letter: D
Question
Determine the maximum and minimum distances between 4 points in a 3-space. The user will be promted to enter any 4 points such as P1(x1,xy1,z1), P2(x2,y2,z2,), P3(x2,y2z3) and P4(x4,y4,z4). Then your program must determine the maximum and minimum between these points and display a message indicatiing for example: max distance is from p1_to_p3 and min distance is from p4_to_p3, etc I went to a tutor we wrote the code together but he did a bad job explaining how the code works to me. Can someone please explain how this code work. Line for line if possible or a general breakdown of it. Thanks a lot! %input should be like this [x1 y1 z1;x2 y2 z2;x3 y3 z3;x4 y4 z4] disp('input should be like this [x1 y1 z1;x2 y2 z2;x3 y3 z3;x4 y4 z4]'); coordinate=input('enter the coordinates matrix:'); distance=zeros(4,4); for i=1:4 for j=1:4 distance(i,j)=sqrt((coordinate(i,1)-coordinate(j,1))^2+(coordinate(i,2)-coordinate(j,2))^2 ... +(coordinate(i,3)-coordinate(j,3))^2); end end mind=intmax; maxd=0; for i=1:4 for j=1:4 if(mind>distance(i,j)&& i~=j) mind=distance(i,j); minx=i; miny=j; end if(maxd maxd=distance(i,j); maxx=i; maxy=j; end end end fprintf('max distance from P%i_to_P%i ',maxx,maxy); fprintf('min distance from P%i_to_P%i',minx,miny);Explanation / Answer
import java.util.*;
public class Q2 {
public void main()
{
Scanner scanner = new Scanner(System.in);
double[] p1 = new double[3];
double[] p2 = new double[3];
double[] p3 = new double[3];
double[] p4 = new double[3];
System.out.print("Enter coordinates of first point");
p1[0] = scanner.nextDouble();
p1[1] = scanner.nextDouble();
p1[2] = scanner.nextDouble();
System.out.print("Enter coordinates of second point");
p2[0] = scanner.nextDouble();
p2[1] = scanner.nextDouble();
p2[2] = scanner.nextDouble();
System.out.print("Enter coordinates of third point");
p3[0] = scanner.nextDouble();
p3[1] = scanner.nextDouble();
p3[2] = scanner.nextDouble();
System.out.print("Enter coordinates of fourth point");
p4[0] = scanner.nextDouble();
p4[1] = scanner.nextDouble();
p4[2] = scanner.nextDouble();
double[] allDist = new double[6];
allDist[0] = getDist(p1, p2);
allDist[1] = getDist(p1, p3);
allDist[2] = getDist(p1, p4);
allDist[3] = getDist(p2, p3);
allDist[4] = getDist(p2, p4);
allDist[5] = getDist(p3, p4);
String[] names = new String[6];
names[0] = "p1 to p2";
names[1] = "p1 to p3";
names[2] = "p1 to p4";
names[3] = "p2 to p3";
names[4] = "p2 to p4";
names[5] = "p3 to p4";
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 5; j++)
{
if (allDist[j] > allDist[j+1])
{
double temp = allDist[j];
allDist[j] = allDist[j+1];
allDist[j+1] = temp;
String tem = names[j];
names[j] = names[j+1];
names[j+1] = tem;
}
}
}
System.out.println("Max distance is between" + names[5]);
System.out.println("Min distance is between" + names[0]);
}
public double getDist(double[] p1, double[] p2)
{
double dist = Math.sqrt(Math.pow(p1[0]-p2[0], 2) + Math.pow(p1[1]-p2[1], 2) + Math.pow(p1[2]-p2[2], 2) );
return dist;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.