简体   繁体   中英

How to find a point in the perimeter of a rectangle?

I'm using Python 2.7 and Pygame but any language answer will do.

I'm trying to find a given point along the vector in the perimeter of a rectangle. I know the vector, and the center, width and height of the rectangle, but the center can be (0, 0) for the sake of simplicity

So for example I want to find the point in the perimeter of the rectangle following the vector of roughly (0.7, 0.7) of a rectangle that is 2 wide by 6 height.

What I've got working now does the work; but there's has got to be a better, more elegant way: I'm taking the length of the wider value between height and width of the rectangle and using every number in between that and 0 and testing that against each of the 4 sides of the rectangle and see if it's in it.

This is the code I use specific to my game, not generalized at all: http://pastebin.com/8Ai1iQeL

I'm going to do this for vectors in the upper right quadrant but it shouldn't be hard to generalize this to the others. You know the angle of the vector and side lengths of the rectangle. So

1) Determine whether the vector will hit the right side or the top of the rectangle. Do this by constructing the right triangle whose hypotenuse is the diagonal of the rectangle, ie the line from the origin to the upper corner. If the angle A of the vector is greater than this angle, it will hit the top; otherwise it will hit the side.

2) Suppose it hits the side (the solution will be analogous if it hits the top). If the width of the triangle is w, then you can construct a right triangle whose vertices are the origin, the point (w/2, 0), and the point (w/2, y), where (w/2, y) is the point you want to find. Then just use the law of sines to get y.

As written this solution involves some branching (4 x 2) to cover all of the possibilities, but I bet if you work through it you can find ways to eliminate code duplication. For example, at the outset you can just rotate every vector into the upper right quadrant, and then after solving use the rotation to reconstruct the proper location of the point.

I'd go about this by comparing the ratio of the components of your vector, which is also the slope of the line parallel to the vector, to the same quantity for the vector pointing from the rectangle's center to its corner. That tells you whether the vector hits a horizontal or vertical side. After that, you can use a simple proportionality to find the intersection point.

Suppose your vector is (x,y) , and for the moment assume that both coordinates are positive. The slope is y/x and the equivalent quantity for the rectangle is h/w , using a coordinate system in which the rectangle's center is at (0,0) . Now, if y/x > h/w , your intersection point will be on the top edge, so you know its height is h/2. You can then compute the coordinates as (0.5*h*x/y,0.5*h) . If y/x < h/w , the intersection point is on the right edge, and the coordinates are (0.5*w,0.5*w*y/x) .

To use this in practice, you'd want to actually do the comparison between y*w and x*h , both to avoid problems with division by zero and to avoid the relatively expensive division operator (not that that really makes much of a difference). Also, you can find the correct signs for the components of the intersection point by just using the signs of x and y . So in code, it'd look something like this:

def intersect_perimeter(x, y, w, h):
    if abs(y*w) > abs(x*h):
        return (0.5*h*x/abs(y), 0.5*h*sign(y))
    else:
        return (0.5*w*sign(x), 0.5*w*y/abs(x))

(untested). This will fail if x is zero and either y or w is zero, but in that case you have either a zero vector (and the problem is undefined) or a zero-width rectangle (again, the problem is undefined). So I wouldn't bother with error checking for that case.

If your rectangle is centered at a point other than (0,0) , you just need to add the position vector representing the rectangle's center to the result from that function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM