Some Geometric Problems(Algorithms)



Segment intersection: Given two segments, do they intersect?
Intersection and Orientation
Two segments (p1,q1) and (p2,q2) intersect if and only if
one of the following two conditions is verified
• general case:
- (p1,q1,p2) and (p1,q1,q2) have different
orientations and
- (p2,q2,p1) and (p2,q2,q1) have different
orientations
• special case
- (p1,q1,p2), (p1,q1,q2), (p2,q2,p1), and (p2,q2,q1) are
all collinear and
- the x-projections of (p1,q1) and (p2,q2) intersect
- the y-projections of (p1,q1) and (p2,q2) intersect
• Orientation test
- counterclockwise (left turn): σ < τ
- clockwise (right turn): σ > τ
- collinear (left turn): σ = τ
• The orientation depends on whether the expression (y2−y1) (x3−x2) − (y3−y2) (x2−x1)
is positive, negative, or null.
Point Inclusion
• given a polygon and a point, is the point inside or outside the polygon?
• orientation helps solving this problem in linear time
• Draw a horizontal line to the right of each point and extend it to infinity
• Count the number of times a line intersects the polygon. We have:
- even number ⇒ point is outside
- odd number ⇒ point is inside
• What about points d and g ?? Degeneracy!
Simple Closed Path — Part I
• “Connect the dots” without crossings
• Pick the bottommost point a as the anchor point
• For each point p, compute the angle q(p) of the segment (a,p) with respect to the x-axis:
• Traversing the points by increasing angle yields a simple closed path.
• The question is: how do we compute angles?
- We could use trigonometry (e.g., arctan).
- However, the computation would be inefficient since trigonometric functions are not in the normal
instruction set of a computer and need a call to a math-library routine.
- Observation:, we don’t care about the actual
values of the angles. We just want to sort by angle.
- Idea: use the orientation to compare angles
without actually computing them!!
θ(p) < θ(q) ⇔ orientation(a,p,q) = CCW
• We can sort the points by angle by using any
“sorting-by-comparison” algorithm (e.g., heapsort or merge-sort) and replacing angle comparisons with orientation tests
• We obtain an O(N log N)-time algorithm for the simple closed path problem on N points
Graham Scan Algorithm
Algorithm Scan(S, a):
Input: A sequence S of points in the plane beginning
with point a such that:
1) a is a vertex of the convex hull of the points of S
2) the remaining points of S are
counterclockwise around a.
Output: Sequence S from which the points that are
not vertices of the convex hull have been removed.
S.insertLast(a) {add a copy of a at the end of S}
prev ← S.first() {so that prev = a initially}
curr ← S.after(prev) {the next point is on the}
{current convex chain}
repeat
next ← S.after(curr) {advance}
if points (point(prev), point(curr), point(next))
make a left turn then
prev ← curr
else
S.remove(curr) {point curr is on the convex hull}
prev ← S.before(prev)
curr ← S.after(prev)
until curr = S.last()
S.remove(S.last()) {remove the copy of a}
Read full article from GEOMETRIC ALGORITHMS

No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts