| 1 |
(* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 |
| 2 |
Free Software Foundation, Inc. *) |
| 3 |
(* This file is part of GNU Modula-2. |
| 4 |
|
| 5 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License as published by the Free |
| 7 |
Software Foundation; either version 3, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License along |
| 16 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 17 |
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *) |
| 18 |
IMPLEMENTATION MODULE Geometry ; |
| 19 |
|
| 20 |
|
| 21 |
FROM Assertion IMPORT Assert ; |
| 22 |
|
| 23 |
|
| 24 |
(* |
| 25 |
IsSubLine - returns true if the range i1..i2 or j1..j2 are ranges |
| 26 |
of each other. |
| 27 |
*) |
| 28 |
|
| 29 |
PROCEDURE IsSubLine (i1, i2, j1, j2: CARDINAL) : BOOLEAN ; |
| 30 |
BEGIN |
| 31 |
RETURN( ((i1<=j1) AND (i2>=j2)) OR ((j1<=i1) AND (j2>=i2)) ) |
| 32 |
END IsSubLine ; |
| 33 |
|
| 34 |
|
| 35 |
(* |
| 36 |
IsIntersectingRange - returns true if the ranges i1..i2 j1..j2 |
| 37 |
overlap. |
| 38 |
*) |
| 39 |
|
| 40 |
PROCEDURE IsIntersectingRange (i1, i2, j1, j2: CARDINAL) : BOOLEAN ; |
| 41 |
BEGIN |
| 42 |
(* Easier to prove NOT outside limits!! *) |
| 43 |
RETURN( NOT ((i1>j2) OR (i2<j1)) ) |
| 44 |
END IsIntersectingRange ; |
| 45 |
|
| 46 |
|
| 47 |
(* |
| 48 |
IntersectionLength - returns the intersection length |
| 49 |
of the overlapping ranges i1..i2 j1..j2. |
| 50 |
*) |
| 51 |
|
| 52 |
PROCEDURE IntersectionLength (i1, i2, j1, j2: CARDINAL) : CARDINAL ; |
| 53 |
BEGIN |
| 54 |
IF IsSubRange(i1, i2, j1) |
| 55 |
THEN |
| 56 |
RETURN( Abs(j1, Min(i2, j2)) ) |
| 57 |
ELSIF IsSubRange(i1, i2, j2) |
| 58 |
THEN |
| 59 |
RETURN( Abs(Max(i1, j1), j2) ) |
| 60 |
ELSE |
| 61 |
RETURN( 0 ) |
| 62 |
END |
| 63 |
END IntersectionLength ; |
| 64 |
|
| 65 |
|
| 66 |
(* |
| 67 |
IsPointOnLine - returns true if point x, y is on line (x1, y1) , (x2, y2) |
| 68 |
*) |
| 69 |
|
| 70 |
PROCEDURE IsPointOnLine (x, y: CARDINAL; x1, y1, x2, y2: CARDINAL) : BOOLEAN ; |
| 71 |
BEGIN |
| 72 |
IF (x1=x2) AND (x=x1) |
| 73 |
THEN |
| 74 |
RETURN( IsSubRange(y1, y2, y) ) |
| 75 |
ELSIF (y1=y2) AND (y=y1) |
| 76 |
THEN |
| 77 |
RETURN( IsSubRange(x1, x2, x) ) |
| 78 |
ELSE |
| 79 |
RETURN( FALSE ) |
| 80 |
END |
| 81 |
END IsPointOnLine ; |
| 82 |
|
| 83 |
|
| 84 |
(* |
| 85 |
IsSubRange - returns true if i lies inbetween High and Low. |
| 86 |
*) |
| 87 |
|
| 88 |
PROCEDURE IsSubRange (Low, High, i: CARDINAL) : BOOLEAN ; |
| 89 |
BEGIN |
| 90 |
Assert(High>=Low) ; |
| 91 |
RETURN( (i>=Low) AND (i<=High) ) |
| 92 |
END IsSubRange ; |
| 93 |
|
| 94 |
|
| 95 |
(* |
| 96 |
Max - returns the largest cardinal number from i and j. |
| 97 |
*) |
| 98 |
|
| 99 |
PROCEDURE Max (i, j: CARDINAL) : CARDINAL ; |
| 100 |
BEGIN |
| 101 |
IF i>j |
| 102 |
THEN |
| 103 |
RETURN( i ) |
| 104 |
ELSE |
| 105 |
RETURN( j ) |
| 106 |
END |
| 107 |
END Max ; |
| 108 |
|
| 109 |
|
| 110 |
(* |
| 111 |
Min - returns the smallest cardinal number from i and j. |
| 112 |
*) |
| 113 |
|
| 114 |
PROCEDURE Min (i, j: CARDINAL) : CARDINAL ; |
| 115 |
BEGIN |
| 116 |
IF i<j |
| 117 |
THEN |
| 118 |
RETURN( i ) |
| 119 |
ELSE |
| 120 |
RETURN( j ) |
| 121 |
END |
| 122 |
END Min ; |
| 123 |
|
| 124 |
|
| 125 |
(* |
| 126 |
Abs - returns the difference between i and j. |
| 127 |
*) |
| 128 |
|
| 129 |
PROCEDURE Abs (i, j: CARDINAL) : CARDINAL ; |
| 130 |
BEGIN |
| 131 |
IF i>j |
| 132 |
THEN |
| 133 |
RETURN( i-j ) |
| 134 |
ELSE |
| 135 |
RETURN( j-i ) |
| 136 |
END |
| 137 |
END Abs ; |
| 138 |
|
| 139 |
|
| 140 |
(* |
| 141 |
Swap - swaps two cardinal numbers i and j. |
| 142 |
*) |
| 143 |
|
| 144 |
PROCEDURE Swap (VAR i, j: CARDINAL) ; |
| 145 |
VAR |
| 146 |
t: CARDINAL ; |
| 147 |
BEGIN |
| 148 |
t := i ; |
| 149 |
i := j ; |
| 150 |
j := t |
| 151 |
END Swap ; |
| 152 |
|
| 153 |
|
| 154 |
END Geometry. |