Title: Design of Spatial Information Systems
1 DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF
JOENSUU JOENSUU, FINLAND
- Design of Spatial Information Systems
- Lecture 7
- Algorithms for Spatial Databases
- Alexander Kolesnikov
2Length of polyline
- Given polyline
- Length of the polyline
- (L2 measure)
-
3Area of triangle
There are several different methods for computing
planar areas depending on the information
available.
4Cross-product of vectors in 3-D space
Vector c is orthogonal to the plane defined by
the vectors a and b. Magnitude of the vector c is
the area of the parallelogram, formed by a and
b
where ? is angle between vectors a and b.
Direction from a to b.
5Area of 2-D parallelogram
y
x
Vector c is orthogonal to plane X,Y
The area of parallelogram
6Area of triangle in 2-D space
y
x
7Area of a simple polygon
u
Prove it!
8Sign of the area
- Order of scanning and sign of area.
- Area is positive for counter-clockwise order.
- Area is negative for clockwise order.
- Examples
- Shape and Hole have different sign of area.
9Point location relatively line segment
- A point C is located on the left-hand side of
the oriented segment AB, if the area of triangle
S(ABC) is positive, or the three points A,B, and
C are collinear, if the area is equal to zero
S(ABC)0.
B
C
B
C
A
Slt0
Sgt0
A
B
A
S0
10Detection of intersection
- With the area function we can test whether two
segments AB and CD intersects, in which case A
must be on one side of CD and B on the other,
with the same holding for C and D.
B
B
A
C
C
D
D
A
Intersection 1) A and B on different sides of
CD 2) C and D on differrent sides of AB
Non-intersection 1) A and B on one side of CD
2) C and D on one side of AB
11 Point in polygon
http//www.cs.joensuu.fi/koles/svf/
12Point in polygon
- The crossing number method
- The winding number method
13Point in polygon
D
A
B
C
E
14The crossing number method
Count the number cn of times a ray starting from
the point P crosses the polygon boundary edges.
The point is outside when this "crossing number
cn is even otherwise, when it is odd, the point
is inside.
ray
edge
15The crossing number method
The point is outside when this "crossing number"
is even otherwise, when it is odd, the point is
inside. Examples Outside A cn4 B
cn4 D cn2 Inside C cn1 E
cn3
D
B
A
C
E
16Special cases
- A cn23229 ? A in polygon?! No!
A
17Edge crossing rules
1. An upward edge includes its starting endpoint,
and excludes its final endpoint 2. A downward
edge excludes its starting endpoint, and includes
its final endpoint 3. Horizontal edges are
excluded 4. The edge-ray intersection point
must be strictly right of the point P.
cn0
cn0
cn0
cn1
cn1
18Special cases
- A cn23229 ? A in polygon?! No!
- A cn(01)(101)(11)(10)6 ? A is outside
the polygon
1
A
1
0
1
1
1
1
0
0
19Algorithm
PointInPolygon(Point p, Points V)integer
cn 0 // the crossing number counter FOR
(each edge EiViVi1 of the polygon)
IF (Ei crosses upward ala Rule 1
Ei crosses downward ala Rule 2) THEN
IF(P.x lt x_intersect of Ei with
yP.y) THEN // Rule 4
cn // a valid crossing to the right of P.x
ENDIF ENDIF
ENDFOR RETURN (cn1) // 0 if even (out),
and 1 if odd (in) END
20Problem with Crosing Number method
- If a polygon is simple, then Crossing Number
method give correct result for all points. - But for non-simple polygons, the method can give
wrong answer - for some points.
- Example a polygon overlaps with itself, then
points in the region of - overlap are found to be outside using the
crossing number.
21Crossing Number Inside or outside?
A cn1 Inside B cn2
Outside? C cn2 Outside
22The Winding Number method
- The Winding Number counts the number of times
the polygon winds around the point P. - The point is outside only when this Winding
Number" is equal to 0 otherwise, the point is
inside. - If the Winding Number wn is positive, the
polygon wind the point - wn times at counter-clockwise order, otherwise
the direction is - clockwise
23The Winding Number calculation
W(u) gives a continuous mapping from the curve C
to the unit circle S1, and so can be represented
in coordinates as W(u)(cos q(u), sin q(u)) The
Winding Number wn is then equal to the integer
number of times W(u) wraps around S1.
Too expensive! Let us consider more simple
approach.
24Counter for edge crosses
If an upward edge crosses the ray to the right of
P, then P is on the left side of the edge since
the triangle (ViVi1P) is oriented
counterclockwise. On the other hand, a downward
edge crossing the positive ray would have P on
the right side since the triangle (ViVi1P) would
then be oriented clockwise. To avoid computing
the actual edge-ray intersection point use the
isLeft() attribute.
25The Winding Number
- Edge crosses the ray from below to above,
- the
crossing is positive (1) - Edge crosses the ray from above to below,
- the
crossing is negative (-1). - Then adds all crossing values to get Winding
Number wn.
26Crossing Number Inside or outside?
A cn1 Inside B cn2
Outside? C cn2 Outside
27Winding Number Inside or outside?
A wn 1 Inside B wn 112
Inside C wn -110 Outside