1math::geometry(n)              Tcl Math Library              math::geometry(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       math::geometry - Geometrical computations
9

SYNOPSIS

11       package require Tcl  ?8.5?
12
13       package require math::geometry  ?1.2.3?
14
15       ::math::geometry::+ point1 point2
16
17       ::math::geometry::- point1 point2
18
19       ::math::geometry::p x y
20
21       ::math::geometry::distance point1 point2
22
23       ::math::geometry::length point
24
25       ::math::geometry::s* factor point
26
27       ::math::geometry::direction angle
28
29       ::math::geometry::h length
30
31       ::math::geometry::v length
32
33       ::math::geometry::between point1 point2 s
34
35       ::math::geometry::octant point
36
37       ::math::geometry::rect nw se
38
39       ::math::geometry::nwse rect
40
41       ::math::geometry::angle line
42
43       ::math::geometry::calculateDistanceToLine P line
44
45       ::math::geometry::calculateDistanceToLineSegment P linesegment
46
47       ::math::geometry::calculateDistanceToPolyline P polyline
48
49       ::math::geometry::calculateDistanceToPolygon P polygon
50
51       ::math::geometry::findClosestPointOnLine P line
52
53       ::math::geometry::findClosestPointOnLineSegment P linesegment
54
55       ::math::geometry::findClosestPointOnPolyline P polyline
56
57       ::math::geometry::lengthOfPolyline polyline
58
59       ::math::geometry::movePointInDirection P direction dist
60
61       ::math::geometry::lineSegmentsIntersect linesegment1 linesegment2
62
63       ::math::geometry::findLineSegmentIntersection linesegment1 linesegment2
64
65       ::math::geometry::findLineIntersection line1 line2
66
67       ::math::geometry::polylinesIntersect polyline1 polyline2
68
69       ::math::geometry::polylinesBoundingIntersect polyline1 polyline2 granu‐
70       larity
71
72       ::math::geometry::intervalsOverlap y1 y2 y3 y4 strict
73
74       ::math::geometry::rectanglesOverlap P1 P2 Q1 Q2 strict
75
76       ::math::geometry::bbox polyline
77
78       ::math::geometry::pointInsidePolygon P polyline
79
80       ::math::geometry::pointInsidePolygonAlt P polyline
81
82       ::math::geometry::rectangleInsidePolygon P1 P2 polyline
83
84       ::math::geometry::areaPolygon polygon
85
86       ::math::geometry::translate vector polyline
87
88       ::math::geometry::rotate angle polyline
89
90       ::math::geometry::reflect angle polyline
91
92       ::math::geometry::degToRad angle
93
94       ::math::geometry::radToDeg angle
95
96______________________________________________________________________________
97

DESCRIPTION

99       The math::geometry package is a collection of  functions  for  computa‐
100       tions and manipulations on two-dimensional geometrical objects, such as
101       points, lines and polygons.
102
103       The geometrical objects are implemented as plain lists of  coordinates.
104       For instance a line is defined by a list of four numbers, the x- and y-
105       coordinate of a first point and the x- and y-coordinates  of  a  second
106       point on the line.
107
108       The  various types of object are recognised by the number of coordinate
109       pairs and the context in which they are used: a list of  four  elements
110       can  be regarded as an infinite line, a finite line segment but also as
111       a polyline of one segment and a point set of two points.
112
113       Currently the following types of objects are distinguished:
114
115       ·      point - a list of two coordinates representing  the  x-  and  y-
116              coordinates respectively.
117
118       ·      line  - a list of four coordinates, interpreted as the x- and y-
119              coordinates of two distinct points on the line.
120
121       ·      line segment - a list of four coordinates, interpreted as the x-
122              and  y-coordinates  of the first and the last points on the line
123              segment.
124
125       ·      polyline - a list of an even number of coordinates,  interpreted
126              as the x- and y-coordinates of an ordered set of points.
127
128       ·      polygon  -  like a polyline, but the implicit assumption is that
129              the polyline is closed (if the first  and  last  points  do  not
130              coincide, the missing segment is automatically added).
131
132       ·      point  set  - again a list of an even number of coordinates, but
133              the points are regarded without any ordering.
134

PROCEDURES

136       The package defines the following public procedures:
137
138       ::math::geometry::+ point1 point2
139              Compute the sum of the two vectors given as  points  and  return
140              it.  The result is a vector as well.
141
142       ::math::geometry::- point1 point2
143              Compute  the  difference  (point1  -  point2) of the two vectors
144              given as points and return it. The result is a vector as well.
145
146       ::math::geometry::p x y
147              Construct a point from its coordinates  and  return  it  as  the
148              result of the command.
149
150       ::math::geometry::distance point1 point2
151              Compute the distance between the two points and return it as the
152              result of the command. This is in essence the same as
153
154
155                  math::geometry::length [math::geomtry::- point1 point2]
156
157
158       ::math::geometry::length point
159              Compute the length of the vector and return it as the result  of
160              the command.
161
162       ::math::geometry::s* factor point
163              Scale  the  vector  by the factor and return it as the result of
164              the command. This is a vector as well.
165
166       ::math::geometry::direction angle
167              Given the angle in degrees this command computes and returns the
168              unit  vector  pointing into this direction. The vector for angle
169              == 0 points to the right (up), and for angle == 90 up (north).
170
171       ::math::geometry::h length
172              Returns a horizontal vector  on  the  X-axis  of  the  specified
173              length.  Positive lengths point to the right (east).
174
175       ::math::geometry::v length
176              Returns a vertical vector on the Y-axis of the specified length.
177              Positive lengths point down (south).
178
179       ::math::geometry::between point1 point2 s
180              Compute the point which is at relative distance  s  between  the
181              two  points  and return it as the result of the command. A rela‐
182              tive distance of  0  returns  point1,  the  distance  1  returns
183              point2.  Distances < 0 or > 1 extrapolate along the line between
184              the two point.
185
186       ::math::geometry::octant point
187              Compute the octant of the circle the point is in and  return  it
188              as the result of the command. The possible results are
189
190              [1]    east
191
192              [2]    northeast
193
194              [3]    north
195
196              [4]    northwest
197
198              [5]    west
199
200              [6]    southwest
201
202              [7]    south
203
204              [8]    southeast
205
206              Each  octant  is the arc of the circle +/- 22.5 degrees from the
207              cardinal direction the octant is named for.
208
209       ::math::geometry::rect nw se
210              Construct a rectangle from its northwest and  southeast  corners
211              and return it as the result of the command.
212
213       ::math::geometry::nwse rect
214              Extract the northwest and southeast corners of the rectangle and
215              return them as the result of the command (a 2-element list  con‐
216              taining the points, in the named order).
217
218       ::math::geometry::angle line
219              Calculate the angle from the positive x-axis to a given line (in
220              two dimensions only).
221
222              list line
223                     Coordinates of the line
224
225
226       ::math::geometry::calculateDistanceToLine P line
227              Calculate the distance of point P to  the  (infinite)  line  and
228              return the result
229
230              list P List of two numbers, the coordinates of the point
231
232              list line
233                     List  of  four  numbers, the coordinates of two points on
234                     the line
235
236
237       ::math::geometry::calculateDistanceToLineSegment P linesegment
238              Calculate the distance of point P to the (finite)  line  segment
239              and return the result.
240
241              list P List of two numbers, the coordinates of the point
242
243              list linesegment
244                     List  of  four  numbers, the coordinates of the first and
245                     last points of the line segment
246
247
248
249       ::math::geometry::calculateDistanceToPolyline P polyline
250              Calculate the distance of point P to the polyline and return the
251              result. Note that a polyline needs not to be closed.
252
253              list P List of two numbers, the coordinates of the point
254
255              list polyline
256                     List  of  numbers, the coordinates of the vertices of the
257                     polyline
258
259
260       ::math::geometry::calculateDistanceToPolygon P polygon
261              Calculate the distance of point P to the polygon and return  the
262              result. If the list of coordinates is not closed (first and last
263              points differ), it is automatically closed.
264
265              list P List of two numbers, the coordinates of the point
266
267              list polygon
268                     List of numbers, the coordinates of the vertices  of  the
269                     polygon
270
271
272       ::math::geometry::findClosestPointOnLine P line
273              Return the point on a line which is closest to a given point.
274
275              list P List of two numbers, the coordinates of the point
276
277              list line
278                     List  of  four  numbers, the coordinates of two points on
279                     the line
280
281
282       ::math::geometry::findClosestPointOnLineSegment P linesegment
283              Return the point on a line segment which is closest to  a  given
284              point.
285
286              list P List of two numbers, the coordinates of the point
287
288              list linesegment
289                     List  of  four  numbers, the first and last points on the
290                     line segment
291
292
293       ::math::geometry::findClosestPointOnPolyline P polyline
294              Return the point on a polyline  which  is  closest  to  a  given
295              point.
296
297              list P List of two numbers, the coordinates of the point
298
299              list polyline
300                     List of numbers, the vertices of the polyline
301
302
303       ::math::geometry::lengthOfPolyline polyline
304              Return  the  length  of the polyline (note: it not regarded as a
305              polygon)
306
307              list polyline
308                     List of numbers, the vertices of the polyline
309
310
311       ::math::geometry::movePointInDirection P direction dist
312              Move a point over a given distance  in  a  given  direction  and
313              return the new coordinates (in two dimensions only).
314
315              list P Coordinates of the point to be moved
316
317              double direction
318                     Direction (in degrees; 0 is to the right, 90 upwards)
319
320              list dist
321                     Distance over which to move the point
322
323
324       ::math::geometry::lineSegmentsIntersect linesegment1 linesegment2
325              Check  if  two line segments intersect or coincide. Returns 1 if
326              that is the case, 0 otherwise (in two dimensions  only).  If  an
327              endpoint  of  one  segment lies on the other segment (or is very
328              close to the segment), they are considered to intersect
329
330              list linesegment1
331                     First line segment
332
333              list linesegment2
334                     Second line segment
335
336
337       ::math::geometry::findLineSegmentIntersection linesegment1 linesegment2
338              Find the intersection point of two  line  segments.  Return  the
339              coordinates  or  the keywords "coincident" or "none" if the line
340              segments coincide or have no points in common (in two dimensions
341              only).
342
343              list linesegment1
344                     First line segment
345
346              list linesegment2
347                     Second line segment
348
349
350       ::math::geometry::findLineIntersection line1 line2
351              Find  the intersection point of two (infinite) lines. Return the
352              coordinates or the keywords "coincident" or "none" if the  lines
353              coincide or have no points in common (in two dimensions only).
354
355              list line1
356                     First line
357
358              list line2
359                     Second line
360
361              See  section  References  for  details on the algorithm and math
362              behind it.
363
364
365       ::math::geometry::polylinesIntersect polyline1 polyline2
366              Check if two polylines  intersect  or  not  (in  two  dimensions
367              only).
368
369              list polyline1
370                     First polyline
371
372              list polyline2
373                     Second polyline
374
375
376       ::math::geometry::polylinesBoundingIntersect polyline1 polyline2 granu‐
377       larity
378              Check whether two polylines intersect, but reduce  the  correct‐
379              ness  of  the  result  to  the  given granularity.  Use this for
380              faster, but weaker, intersection checking.
381
382              How it works:
383
384              Each polyline is split into a number of smaller polylines,  con‐
385              sisting  of  granularity points each. If a pair of those smaller
386              lines' bounding boxes intersect, then this procedure returns  1,
387              otherwise it returns 0.
388
389              list polyline1
390                     First polyline
391
392              list polyline2
393                     Second polyline
394
395              int granularity
396                     Number  of  points  in  each  part (<=1 means check every
397                     edge)
398
399
400       ::math::geometry::intervalsOverlap y1 y2 y3 y4 strict
401              Check if two intervals overlap.
402
403              double y1,y2
404                     Begin and end of first interval
405
406              double y3,y4
407                     Begin and end of second interval
408
409              logical strict
410                     Check for strict or non-strict overlap
411
412
413       ::math::geometry::rectanglesOverlap P1 P2 Q1 Q2 strict
414              Check if two rectangles overlap.
415
416              list P1
417                     upper-left corner of the first rectangle
418
419              list P2
420                     lower-right corner of the first rectangle
421
422              list Q1
423                     upper-left corner of the second rectangle
424
425              list Q2
426                     lower-right corner of the second rectangle
427
428              list strict
429                     choosing strict or non-strict interpretation
430
431
432       ::math::geometry::bbox polyline
433              Calculate the bounding box of a polyline. Returns a list of four
434              coordinates:  the  upper-left  and the lower-right corner of the
435              box.
436
437              list polyline
438                     The polyline to be examined
439
440
441       ::math::geometry::pointInsidePolygon P polyline
442              Determine if a point is completely  inside  a  polygon.  If  the
443              point  touches  the  polygon,  then  the point is not completely
444              inside the polygon.
445
446              list P Coordinates of the point
447
448              list polyline
449                     The polyline to be examined
450
451
452       ::math::geometry::pointInsidePolygonAlt P polyline
453              Determine if a point is completely  inside  a  polygon.  If  the
454              point  touches  the  polygon,  then  the point is not completely
455              inside the polygon. Note: this alternative  procedure  uses  the
456              so-called  winding  number  to  determine this. It handles self-
457              intersecting polygons in a "natural" way.
458
459              list P Coordinates of the point
460
461              list polyline
462                     The polyline to be examined
463
464
465       ::math::geometry::rectangleInsidePolygon P1 P2 polyline
466              Determine if a rectangle is  completely  inside  a  polygon.  If
467              polygon  touches  the  rectangle, then the rectangle is not com‐
468              plete inside the polygon.
469
470              list P1
471                     Upper-left corner of the rectangle
472
473              list P2
474                     Lower-right corner of the rectangle
475
476
477              list polygon
478                     The polygon in question
479
480
481       ::math::geometry::areaPolygon polygon
482              Calculate the area of a polygon.
483
484              list polygon
485                     The polygon in question
486
487
488       ::math::geometry::translate vector polyline
489              Translate a polyline over a given vector
490
491              list vector
492                     Translation vector
493
494              list polyline
495                     The polyline to be rotated
496
497
498       ::math::geometry::rotate angle polyline
499              Rotate a polyline over a given angle (degrees) around the origin
500
501              list angle
502                     Angle over which to rotate the polyline (degrees)
503
504              list polyline
505                     The polyline to be translated
506
507
508       ::math::geometry::reflect angle polyline
509              Reflect a polyline in a line through the origin at a given angle
510              (degrees) to the x-axis
511
512              list angle
513                     Angle of the line of reflection (degrees)
514
515              list polyline
516                     The polyline to be reflected
517
518
519       ::math::geometry::degToRad angle
520              Convert from degrees to radians
521
522              list angle
523                     Angle in degrees
524
525
526       ::math::geometry::radToDeg angle
527              Convert from radians to degrees
528
529              list angle
530                     Angle in radians
531

REFERENCES

533       [1]    Polygon Intersection [http:/wiki.tcl.tk/12070]
534
535       [2]    http://en.wikipedia.org/wiki/Line-line_intersection
536
537       [3]    http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
538

BUGS, IDEAS, FEEDBACK

540       This  document,  and the package it describes, will undoubtedly contain
541       bugs and other problems.  Please report such in the  category  math  ::
542       geometry of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist].
543       Please also report any ideas for enhancements you may have  for  either
544       package and/or documentation.
545
546       When proposing code changes, please provide unified diffs, i.e the out‐
547       put of diff -u.
548
549       Note further that  attachments  are  strongly  preferred  over  inlined
550       patches.  Attachments  can  be  made  by  going to the Edit form of the
551       ticket immediately after its creation, and  then  using  the  left-most
552       button in the secondary navigation bar.
553

KEYWORDS

555       angle, distance, line, math, plane geometry, point
556

CATEGORY

558       Mathematics
559
561       Copyright (c) 2001 by Ideogramic ApS and other parties
562       Copyright (c) 2004 by Arjen Markus
563       Copyright (c) 2010 by Andreas Kupries
564       Copyright (c) 2010 by Kevin Kenny
565
566
567
568
569tcllib                               1.2.2                   math::geometry(n)
Impressum