If you came here searching for the Karnaugh Map boolean algebra program, please click here.

Amorphous maze creation algorithm is based on starting from an outermost wall and extending the wall by a small segment at a time in a manner that avoids walls from crossing over each other. Each time a line segment is successfully extended, it is added to a global repository of line segments to test against. When the extending operation comes to a dead end, a different coordinate is randomly chosen from the repository as a starting point of a new branch.

There are various sections within my program that need to compute the
distance between two points. If P_{1}=(x_{1},y_{1})
and P_{2}=(x_{2},y_{2}), then by the Pythagorean theorem, distance between two points
P_{1} and P_{2} is:

Sometimes, we need to calculate if a given point lies within a circle or a rectangle. We can test if a certain point P lies inside or outside a circle by calculating the distance between P and the center of the circle using the aforementioned distance formula. As for the rectangle, we can tell if a certain point Q lies inside or outside of a bounded rectangle by conducting four tests that tell on which side of a line a point lies. Which leads to the next topic of:

There are several ways to determine on which side of a line segment P2P3 a point P1 lies. One way is to find the equation of the line y=ax+b that passes through the two points and test if y<ax+b, but this runs into difficulty when the line is perfectly vertical. A better way is to find the normal vector P4P5 emanating from the center point P4 of the line segment and compute the inner product of vectors P1P4 and P4P5.

Iff the inner product is positive, P_{1} lies on the white side of the line segment and iff negative, the yellow side in the picture above.

Sometimes we need to rotate a point. The formula for rotating a point P1 theta radians counter clockwise about the origin, resulting in P2, is:

If we want to test if two line segments intersect, one method is to write the equations of the lines in the format of y=a1x+b1, y=a2x+b2 and solve for (x,y). The problem with this approach is that it cannot handle perfectly vertical lines. To overcome this, we introduce a third scalar variable k.
Consider a single line segment with end points P and Q. Vector originating at point P and terminating at point Q is (
**
Q** - **P** ). Therefore, a vector R parallel to this line is

R= k(Q-P)

where k is an arbitrary scalar constant. Therefore, all points on the line can be expressed as

S=P+k(Q-P)

This form is called the parametric representation of a line.

By expressing the two line segments parametrically, it is relatively simple to determine if line segments 1 and 2 intersect. Let

- Line segment 1: (x1,y1)(x2,y2)
- Line segment 2: (x3,y3)(x4,y4)
- Intersection, if exists, (x
_{5},y_{5})

Using the parametric representation of lines

**P5**=**P1**+m(**P2**-**P1**)**P5**=**P3**+n(**P4**-**P3**)

Where m and n are the amount by which P_{5} is into each line segments.
I.e., m is the distance of P_{1} to P_{5}, normalized with respect to length of line segment 1 and
n is the distance of P_{3} to P_{5}, normalized with respect to length of line segment 2.
In the figure above, if the intersection lies to the left of P_{1}, m becomes negative, and if the intersection lies to the right of
P_{2}, m becomes greater than one. Solving for the intersection requires solving the following four simultaneous equations:

- x
_{5}=x_{1}+(x_{2}-x_{1})m - y
_{5}=y1+(y_{2}-y_{1})m - x
_{5}=x_{3}+(x_{4}-x3)n - y
_{5}=y3+(y_{4}-y3)n

Eliminating x5 and y5,

- x
_{1}+(x_{2}-x_{1})m=x_{3}+(x_{4}-x_{3})n - y
_{1}+(y_{2}-y_{1})m=y_{3}+(y_{4}-y_{3})n

Using Cramer's rule to solve this pair of equations for m and n,

We need to satisfy both 0<m<1 and 0<n<1. Write m and n as:

where M and N are numerators whose signs are adjusted so that the denominator D is always positive.

If the lines are parallel, D, the determinant at the denominator is zero and will cause a divide-by-zero error. So rather than evaluating M/D or N/D, we take the numerators and denominators apart and test for the following four conditions:

- 0<M
- M<D
- 0<N
- N<D

The line segments 1 and 2 intersect if and only if these four conditions are met.

Last update 2011-07-31