Skip to content

The maths

Mike Harris edited this page Nov 22, 2018 · 3 revisions

Likely to be some difficult maths used within this library so will try to list as much of the maths here as possible as well as links to documentation/help.

Geometry maths

Intersection(|?)

The equation used for these 2 functions comes from here: Wikipedia They calculate u and then substitue u into the equation to calculate the point of intersection for x and y coordinates.

The line U *= 0.999 if cleaning is used to calculate a point slightly before the intersection point and can be used to seperate nodes in a self intersection.

Area

Area Maths

Clockwise?

This uses a near identical formula to Area, but without forcing the result to be positive. If the result of the area calculation is positive the polygon is deemed to be Anti-Clockwise, else the polygon is clockwise

Dissolve

First start at point which you are sure will be part of the final geometry. Jump to the next node and check if the line intersects with the other polygon. When an intersection is found, the point of the intersection is added to the new polygon and then you continue with to the next node on the other polygon.

Both nodes of both polygons must travel in a clockwise rotation to work

Clip

Works in the same way as dissolve, except you need to have the nodes traveling in opposite directions (clockwise and anti-clockwise)

Point_in_polygon

Draw a horizontal line from your point to infinity on the x axis Count the number of times this line crosses one of your lines in the polygon If the count is even, then the point is outside the polygon. If the count is odd, then the point is inside the polygon.

Generalisation algorithms

Douglas Peucker

This is a recursive algorithm:

  • Create a new polygon, which starts off with just the first node of the old
  • Take the first and the last node
  • Calculate the distances between the line and each point along the line
  • If none of the distances are greater than the tolerance then return the last node
  • Find the node that is the furest from the line
  • Split the nodes into 2 halfs with the maxium node being the end of the first half and start of the second half
  • Run each half through his function, to break it down into smaller and smaller line segments

Stats , nearest neighbour - analysis