# Distance between a point and a rect

• 01-03-2012, 02:29 PM
branquinho
Distance between a point and a rect
Hi guys. I need your help. I've a project to school that I need to find the distance between a point and a line without using the distance formula in R3.

Code:

public static void main(String[] args){
int[] eq = {1,0,2,2,-1,-3};    //data from equation (x,y,z)=(x0,y0,z0)+k(a,b,c)    eq={x0,y0,z0,a,b,c}
int[] P = {1,-1,2};              //point
distance(P,eq);
}

public static void distance(int[] P, int[] r){
double [] x={r[0],r[3],0};
double [] y={r[1],r[4],0};
double [] z={r[2],r[5],0};
double [] omega={0,0,0};
double [] i={0,0,0};
double alpha_t = r[3]*(-r[0]) - r[4]*(-r[1]) - r[5]*(-r[2]);
double [] alpha={r[3],r[4],r[5],alpha_t};
omega[0]=alpha[0]*x[1]+alpha[1]*y[1]+alpha[2]*z[1];
omega[1]=alpha[0]*x[0]+alpha[1]*y[0]+alpha[2]*z[0]-alpha[3];
omega[2]=-omega[1]/omega[0];
i[0]=x[0]+x[1]*omega[2];
i[1]=y[0]+y[1]*omega[2];
i[2]=z[0]+z[1]*omega[2];
double dist_=Math.pow((P[0]-i[0]), 2)+Math.pow((P[1]-i[1]), 2)+Math.pow((P[2]-i[2]), 2);
double dist=Math.sqrt(dist_);
System.out.println(dist);
}

This code works with some numbers, but not always... :s:
If someone could help me, I would be grateful.
• 01-03-2012, 06:41 PM
Re: Distance between a point and a rect
Ok, first off, could you modify your post to put the code inside [code][/code] tags so we can read it?
• 01-03-2012, 07:03 PM
branquinho
Re: Distance between a point and a rect
Quote: