问题
I need help with one tricky thing that I've tried to solve for a couple of days now. It feels like it should be relatively easy and that I am simply missing something somewhere.
I have an array defining each wall point (black dots) and I want to create a new inside point (green dots) for each wall point. The distance between each wall point and inside point should be 600.
I made a picture to show what I mean:
I'm writing this in javascript with aid of WebGL (ThreeJS).
回答1:
I'm going to propose a solution similar to David's, but using vertices rather than line segments. My solution also requires a consistent ordering of points, so I'll go with a clockwise ordering as well.
The following pseudocode will generate an ordered listing (in the same order as that of the original points) of new inside points:
d = 600 // distance of new inside point from wall point
for each 3 consecutive points (a, b, c)
vector u = a - b; normalize u
vector v = c - b; normalize v
w = u + v; normalize w
if angle (a, b, c) is convex // cross product is positive: see below
new inside point = b + (w * d)
else // angle is concave/cross product is negative
new inside point = b - (w * d)
end
end
The cross product (u x v) is given by:
(xu * yv) - (yu * xv)
If the ordering is counter-clockwise, the signs of the cross product are reversed.
This does not check for collisions resulting from trying to put new inside points into a corridor of width less than 600, as @01zhou
mentions in the comments.
回答2:
I don't have a code answer but this is how I would do it.
- For the given point, get the two adjacent points and get their walls.
- For each wall, draw a parallel line on either side of the wall.
- Find the two lines that intersect. Wherever they intersect is the inside point that you're looking for.
- If the three points share a common axis value (e.g. 3 points on the same line), then both sides will intersect. You will have to branch out to other points to discover which one to keep.
回答3:
First get a list of wall segments in (e.g.) clockwise order. Let's suppose that we have a wall segment from (a, b)
to (c, d)
going clockwise. The line defined by the segment is
(1 - t) (a, b) + t (c, d),
where t
is variable. Inside is to the segment's right, where right is defined by the normal vector (d - b, a - c)
. Let
v = (d - b, a - c) / sqrt((d - b)**2 + (a - c)**2)
be the unit normal. Then, moved e
units right, the new line is
(1 - t) (a, b) + t (c, d) + e v.
To determine the segments of the inner boundary, intersect each subsequent pair of lines defining the inner wall.
来源:https://stackoverflow.com/questions/23543212/defining-an-inside-room-point-from-wall-points