java - Finding the closest possible position for a rectangle in a field of rectangles -
i programming on 2d castle defense , faced problem today not able find solution for. see following picture:
i want find closest possible position place red rectangle in field of rectangles without overlapping rectangles. closest possible position mean position closest current mouse position, closest possible position given point.
what right algorithm problem?
thanks!!!
this optimization problem constraints linear , objective function (piecewise) quadratic or linear, depending on how want define distance cursor.
assuming rectangles defined x_i, y_i, w_i, h_i i=1..n
, red rectangle has size w, h
, decision variables x, y
position red rectangle.
the non-overlapping constraints then:
x >= x_i + w_i
or y >= y_i + h_i
or x <= x_i - w
or y <= y_i - h
i=1..n
there multiple ways define objective (the distance of red rectangle cursor):
- proper eucledian squared distance between cursor , nearest point of red rectangle (results in piecewise quadratic function think)
- the eucledian squared distance center of red rectangle (quadratic)
- manhattan distance center of red rectangle (piecewise linear)
then use quadratic programming solver or milp (mixed-integer linear programming) solver find answer. if number of rectangles not big (say less hundred) quite fast think free solvers glpk or lp solve.
note express constraints these solvers, might have transform constraints , objective, example use big m method constraints, or transform problem linear objective. means have additional binary variables, , couple of more constraints (the number of additional variables , constraints proportional number of rectangles).
Comments
Post a Comment