I'm having such a problem: i can't figure easiest way to find distance between point and block (as shown on picture)

Everything that i tried is not properly working
Could you help me please and tell way to find that distance.
Posted 15 April 2012 - 08:01 AM
P=P1+u*(P2-P1)for any 2 points, P1 and P2, on a line. This means that any point along the line can be expressed in terms of the parameter, u. A value of u=0 evaluates to P1 on the line, a value of u=1 evaluates to P2 on the line, and values of u in between 0 and 1 evaluate to points on the linesegment described by P1 and P2. Values of u less than 0 or greater than 1 evaluate to points beyond the ends of the line segment.
u=((P.x-P1.x)*(P2.x-P1.x) + (P.y-P1.y)*(P2.y-P1.y)) / length(P2-P1)^2
function closestPointOnLineU(p, p1, p2)
local length=math.sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y))
return ((p.x-p1.x)*(p2.x-p1.x) + (p.y-p1.y)*(p2.y-p1.y)) / (length*length)
end
function closestPointOnLineSegment(p, p1, p2)
local u=closestPointOnLineU(p,p1,p2)
u=math.max(0, math.min(1, u))
return {x=p1.x+u*(p2.x-p1.x), y=p1.y+u*(p2.y-p1.y)}
end
function distanceToLineSegment(p, p1, p2)
local closest=closestPointOnLineSegment(p,p1,p2)
return math.sqrt((p.x-closest.x)*(p.x-closest.x) + (p.y-closest.y)*(p.y-closest.y))
end
function distanceToBox(p, A, B, C, D)
local d1=distanceToLineSegment(p, A, B)
local d2=distanceToLineSegment(p, C, D)
local d3=distanceToLineSegment(p, A, C)
local d4=distanceToLineSegment(p, B, D)
return math.min(d1,d2,d3,d4)
end
Note that it's quick code, so I might have made an error somewhere, but initial tests indicate it works.
Posted 15 April 2012 - 08:51 AM
float distance_point_to_box(float x, float y,
float min_x, float min_y,
float max_x, float max_y) {
float dx = x < min_x ? x - min_x : x < max_x ? 0.0f : max_x - x;
float dy = y < min_y ? y - min_y : y < max_y ? 0.0f : max_y - y;
return std::sqrt(dx * dx + dy * dy);
}
Posted 15 April 2012 - 10:01 AM
Posted 15 April 2012 - 11:31 AM
int lx,ly; //temporary point float t_distance[4]; //FIND POINT ON TOP SIDE ly = block->y; lx = (block->y - y) / tan(angle) + x; if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h) //find distance to it t_distance[0] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly)); //FIND POINT BOTTOM SIDE ly = block->y + block->h; lx = (block->y + block->h - y) /tan(angle) + x; if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h) //find distance to it t_distance[1] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly)); //FIND POINT ON LEFT SIDE lx = block->x; ly = tan(angle) * (block->x - x) + y; //check if temp point is onside of block if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h) //find distance to it t_distance[2] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly)); //FIND POINT ON RIGHT SIDE lx = block->x + block->w; ly = tan(angle) * (block->x + block->w - x) + y; if( lx >= block->x && lx <= block->x + block->w && ly >= block->y && ly <= block->y + block->h) //find distance to it t_distance[3] = sqrt((float) (x - lx)*(x - lx) + (y - ly)*(y - ly));Then just find minimum in t_distance and there u have it!
Posted 15 April 2012 - 01:31 PM
Posted 15 April 2012 - 01:46 PM