background preloader

Coding

Facebook Twitter

Algorithm Tutorials. Line Sweep Algorithms By bmerryTopCoder Member A previous series of articles covered the basic tools of computational geometry.

Algorithm Tutorials

In this article I'll explore some more advanced algorithms that can be built from these basic tools. They are all based on the simple but powerful idea of a sweep line: a vertical line that is conceptually “swept” across the plane. In practice, of course, we cannot simulate all points in time and so we consider only some discrete points.

In several places I'll refer to the Euclidean and Manhattan distances. In addition, a balanced binary tree is used in some of the algorithms. Closest pair Given a set of points, find the pair that is closest (with either metric). Suppose that we have processed points 1 to N − 1 (ordered by X) and the shortest distance we have found so far is h. Line segment intersections We'll start by considering the problem of returning all intersections in a set of horizontal and vertical line segments. Event active set Convex hull The BoxUnion. Algorithm Tutorials. Geometry Concepts: Line Intersection and its Applications By lbackstromTopCoder Member ...read Section 1 Line-Line IntersectionFinding a Circle From 3 PointsReflectionRotationConvex Hull In the previous section we saw how to use vectors to solve geometry problems.

Algorithm Tutorials

Now we are going to learn how to use some basic linear algebra to do line intersection, and then apply line intersection to a couple of other problems. Line-Line Intersection One of the most common tasks you will find in geometry problems is line intersection. Double det = A1*B2 - A2*B1 if(det == 0){ //Lines are parallel }else{ double x = (B2*C1 - B1*C2)/det double y = (A1*C2 - A2*C1)/det } To see where this comes from, consider multiplying the top equation by B2, and the bottom equation by B1.

This gives you the location of the intersection of two lines, but what if you have line segments, not lines. To find the perpendicular bisector of XY, find the line from X to Y, in the form Ax+By=C. ...continue to Section 3. Algorithm Tutorials. Bitwise operations. Bitwise operators include: AND OR and XOR These require two operands and will perform bit comparisions.

Bitwise operations

AND & will copy a bit to the result if it exists in both operands. OR | will copy a bit if it exists in eather operand. XOR ^ copies the bit if it is set in one operand (but not both). XOR example program which swaps the contents of two variables. Try it out with one of these simulators. Ones Complement This operator is unary (requires one operand) and has the efect of 'flipping' bits. Try it out Bit shift. The following operators can be used for shifting bits left or right. The left operands value is moved left or right by the number of bits specified by the right operand.

Usually, the resulting 'empty' bit is assigned ZERO. Try it out with one of these silulators Examples: Bit shifting. Problem: Bit shifting problem. Sphere Online Judge (SPOJ) - User shelly. The Flames Calculation.