Mercurial > traipse_dev
comparison orpg/mapper/map_utils.py @ 71:449a8900f9ac ornery-dev
Code refining almost completed, for this round. Some included files are still in need of some clean up, but this is test worthy.
author | sirebral |
---|---|
date | Thu, 20 Aug 2009 03:00:39 -0500 |
parents | 072ffc1d466f |
children |
comparison
equal
deleted
inserted
replaced
70:52a5fa913008 | 71:449a8900f9ac |
---|---|
1 #------------------------------------------------ | 1 """ |
2 # file: map_utils.py | 2 file: map_utils.py |
3 # | 3 |
4 # This file contains generic utility functions | 4 This file contains generic utility functions |
5 # for use in the openrpg mapping system | 5 for use in the openrpg mapping system |
6 # ----------------------------------------------- | 6 """ |
7 | 7 |
8 import math | 8 import math |
9 | 9 """ |
10 #----------------------------------------------------------------------- | 10 distance_between() |
11 # distance_between() | 11 Returns the distance between two points |
12 # Returns the distance between two points | 12 """ |
13 #----------------------------------------------------------------------- | |
14 def distance_between( x1, y1, x2, y2 ): | 13 def distance_between( x1, y1, x2, y2 ): |
15 "Returns the distance between two points" | 14 "Returns the distance between two points" |
16 dx = x2 - x1 | 15 dx = x2 - x1 |
17 dy = y2 - y1 | 16 dy = y2 - y1 |
18 return math.sqrt( dx*dx + dy*dy ) | 17 return math.sqrt( dx*dx + dy*dy ) |
19 | 18 |
20 #----------------------------------------------------------------------- | 19 """ |
21 # proximity_test() | 20 proximity_test() |
22 # Tests if 'test_point' (T) is close (within 'threshold' units) to the | 21 Tests if 'test_point' (T) is close (within 'threshold' units) to the |
23 # line segment 'start_point' to 'end_point' (PQ). | 22 line segment 'start_point' to 'end_point' (PQ). |
24 # | 23 |
25 # The closest point (R) to T on the line PQ is given by: | 24 The closest point (R) to T on the line PQ is given by: |
26 # R = P + u (Q - P) | 25 R = P + u (Q - P) |
27 # TR is perpendicular to PQ so: | 26 TR is perpendicular to PQ so: |
28 # (T - R) dot (Q - P) = 0 | 27 (T - R) dot (Q - P) = 0 |
29 # Solving these two equations gives the equation for u (see below). | 28 Solving these two equations gives the equation for u (see below). |
30 # | 29 |
31 # If u < 0 or u > 1 then R is not within the line segment and we simply | 30 If u < 0 or u > 1 then R is not within the line segment and we simply |
32 # test against point P or Q. | 31 test against point P or Q. |
33 #----------------------------------------------------------------------- | 32 """ |
34 def proximity_test( start_point, end_point, test_point, threshold ): | 33 def proximity_test( start_point, end_point, test_point, threshold ): |
35 "Test if a point is close to a line segment" | 34 "Test if a point is close to a line segment" |
36 x1,y1 = start_point | 35 x1,y1 = start_point |
37 x2,y2 = end_point | 36 x2,y2 = end_point |
38 xt,yt = test_point | 37 xt,yt = test_point |