forked from schell/GeoAlgLib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLine.lhs
More file actions
267 lines (219 loc) · 9.5 KB
/
Line.lhs
File metadata and controls
267 lines (219 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
%------------------------------------------------------------------------------
% Copyright (C) 1997, 1998, 2008 Joern Dinkla, www.dinkla.net
%------------------------------------------------------------------------------
%
% see
% Joern Dinkla, Geometrische Algorithmen in Haskell, Diploma Thesis,
% University of Bonn, Germany, 1998.
%
\subsection{Strecken, Strahlen und Geraden (|Line|)}
\module{Line}
\begin{code}
module Line where
import Point ( Point ((<+>),(<*>)), Point3, xcoord, ycoord, zcoord,
distance, sqrDistance )
import Point2 ( Point2 (..), P2, Orientation (..), orientation, angle2 )
import Data.Maybe ( isJust, fromJust )
import qualified Point2 (translate, rotateOrg, reflect, rotate)
import Prelude2010
import Prelude ()
\end{code}
\begin{code}
data (Point p, Num a, Eq a) => Line p a
= Segment { point1, point2 :: p a }
| Ray { point1, point2 :: p a }
| Line { point1, point2 :: p a }
type Line2 a = Line Point2 a
type L2 a = Line Point2 a
type Line2D = Line2 Double
type Line3 a = Line Point3 a
\end{code}
\begin{code}
segmentToRay, segmentToLine,
rayToLine :: (Point p, Num a, Eq a) => Line p a -> Line p a
segmentToRay (Segment x y) = Ray x y
segmentToLine (Segment x y) = Line x y
rayToLine (Ray x y) = Line x y
source, target :: (Point p, Num a, Eq a) => Line p a -> p a
source (Segment a _) = a
target (Segment _ b) = b
mapLine :: (Point p, Num a, Num b, Eq a, Eq b) => (p a -> p b) -> Line p a -> Line p b
mapLine f (Segment x y) = Segment (f x) (f y)
mapLine f (Ray x y) = Ray (f x) (f y)
mapLine f (Line x y) = Line (f x) (f y)
\end{code}
\begin{code}
xcoord1, xcoord2,
ycoord1, ycoord2,
zcoord1, zcoord2 :: (Point p, Num a, Eq a) => Line p a -> a
xcoord1 = xcoord . point1
ycoord1 = ycoord . point1
zcoord1 = zcoord . point1
xcoord2 = xcoord . point2
ycoord2 = ycoord . point2
zcoord2 = zcoord . point2
\end{code}
|dx|, |dy| sind die Differenzen der $x$- bzw. der $y$-Koordinaten der zwei Punkte.
\begin{code}
dx,dy :: (Num a, Eq a) => L2 a -> a
dx s = xcoord2 s - xcoord1 s
dy s = ycoord2 s - ycoord1 s
isVertical, isHorizontal :: (Num a, Eq a) => L2 a -> Bool
isVertical s = ycoord1 s == ycoord2 s
isHorizontal s = xcoord1 s == xcoord2 s
horizontal, vertical :: (Num a, Eq a) => a -> L2 a
horizontal y = Line (Point2 (0, y)) (Point2 (1, y))
vertical x = Line (Point2 (x, 0)) (Point2 (x,1))
\end{code}
Die Steigung einer Geraden wird als Datentyp |Slope| gespeichert. Ein Nachteil dieser
Darstellung ist, daß die Darstellung nicht eindeutig ist, $-0=+0$.
\begin{code}
data Fractional a => Slope a = Vertical | Slope a
deriving (Eq, Show)
slope :: (Fractional a, Eq a) => L2 a -> Slope a
slope s | dx' == 0 = Vertical
| otherwise = Slope (dy s / dx')
where dx' = dx s
areParallel :: (Fractional a, Eq a) => L2 a -> L2 a -> Bool
areParallel s t = slope s == slope t
\end{code}
|direction| ist die Richtung bezüglich der $x$-Achse.
\begin{code}
direction :: RealFloat a => L2 a -> a
direction s | a/=0 || b/=0 = atan2 b a
| otherwise = 0
where a = dx s
b = dy s
\end{code}
|angle| ermittelt den Winkel zwischen zwei Linien.
\begin{code}
angle :: Line2D -> Line2D -> Double
angle s t = angle2 (point2 s - point1 s) (point2 t - point1 t)
\end{code}
|translate|, |rotate| und |reflect| sind kanonische Erweiterungen der
entsprechenden Funktionen auf Punkten.
\begin{code}
translate :: (Floating a, Ord a) => L2 a -> a -> a -> L2 a
translate s phi d = mapLine (\ x -> Point2.translate x phi d) s
rotate :: (Floating a, Ord a) => L2 a -> P2 a -> a -> L2 a
rotate s r phi = mapLine (\ x -> Point2.rotate x r phi) s
rotateOrg :: (Floating a, Ord a) => L2 a -> a -> L2 a
rotateOrg s phi = mapLine (\ x -> Point2.rotateOrg x phi) s
reflect :: (Fractional a, Eq a) => L2 a -> P2 a -> P2 a -> L2 a
reflect s p q = mapLine (\ x -> Point2.reflect x p q) s
\end{code}
|fromPDL| erstellt die Zwei-Punkte-Form aus einem Punkt, einer Richtung und einer Länge.
\begin{code}
fromPDL :: (Floating a, Ord a) => (P2 a -> P2 a -> b) -> P2 a -> a -> a -> b
fromPDL c p phi d = c p (p + (oriented phi d))
where oriented phi d = Point2.rotateOrg (Point2 (d, 0)) phi
\end{code}
%
% ORIENTIERUNG
%
\subsubsection{Orientierung}
\begin{code}
orientationOfLines :: (Num a, Ord a) => L2 a -> L2 a -> Orientation
orientationOfLines s t
| p == p' = orientation p (point2 s) (point2 t)
| otherwise = error "Line2.orientationOfLines: point1 s/=point1 t"
where p = point1 s
p' = point1 t
\end{code}
%
% SCHNITTPUNKTE
%
\subsubsection{Schnittpunkte}
nach \cite[1.01]{cga-faq}
\begin{code}
intersect, strictIntersect :: (Ord a, Fractional a) => L2 a -> L2 a -> Maybe (Point2 a)
doIntersect,doStrictIntersect :: (Ord a, Fractional a) => L2 a -> L2 a -> Bool
intersect s1 s2
| isJust res && ok s1 r && ok s2 s = Just i
| otherwise = Nothing
where
res = intersection s1 s2
(i,r,s) = fromJust res
ok (Segment _ _) r = 0<=r && r<=1
ok (Ray _ _) r = r>=0
ok (Line _ _) r = True
{-
intersect = interAux paramOk
where paramOk (Segment _ _) r = 0<=r && r<=1
paramOk (Ray _ _) r = r>=0
paramOk (Line _ _) r = True
-}
doIntersect s t = isJust (intersect s t)
strictIntersect = interAux paramOk
where
paramOk (Segment _ _) r = 0<r && r<1
paramOk (Ray _ _) r = r>0
paramOk (Line _ _) r = True
doStrictIntersect s t = isJust (strictIntersect s t)
interAux :: (Fractional a, Eq a) => (Line2 a -> a -> Bool) -> Line2 a
-> Line2 a -> Maybe (Point2 a)
interAux ok s1 s2 = if isJust res && ok s1 r && ok s2 s then Just i else Nothing
where
res = intersection s1 s2
(i,r,s) = fromJust res
intersection :: (Fractional a, Eq a) => L2 a -> L2 a -> Maybe (Point2 a,a,a)
intersection s1 s2
| denom == 0 = Nothing
| otherwise = Just (i, r, s)
where a = point1 s1
b = point2 s1
c = point1 s2
d = point2 s2
xa = xcoord a
ya = ycoord a
xb = xcoord b
yb = ycoord b
xc = xcoord c
yc = ycoord c
xd = xcoord d
yd = ycoord d
denom = (xb-xa)*(yd-yc)-(yb-ya)*(xd-xc)
r = ((ya-yc)*(xd-xc)-(xa-xc)*(yd-yc)) / denom
s = ((ya-yc)*(xb-xa)-(xa-xc)*(yb-ya)) / denom
-- i = Point2 (xa + r*(xb-xa), ya + r*(yb-ya))
i = a + (r <*> (b - a))
\end{code}
\cite[1.02]{cga-faq}
\begin{code}
distanceFromLine :: (Ord a, Floating a) => L2 a -> P2 a -> a
distanceFromLine l c = sqrt (sqrDistanceFromLine l c)
sqrDistanceFromLine :: (Ord a, Fractional a) => L2 a -> P2 a -> a
sqrDistanceFromLine (Line a b) c = abs (s*s*l2)
where (r,s,l2,_) = distanceAux a b c
sqrDistanceFromLine (Ray a b) c
| r >= 0 = abs (s*s*l2)
| r < 0 = sqrDistance a c
where (r,s,l2,_) = distanceAux a b c
sqrDistanceFromLine (Segment a b) c
| r < 0 = sqrDistance a c
| r > 1 = sqrDistance b c
| 0<=r && r<=1 = abs (s*s*l2)
where (r,s,l2,_) = distanceAux a b c
distanceAux :: (Fractional a, Eq a) => P2 a -> P2 a -> P2 a -> (a,a,a,P2 a)
distanceAux a@(Point2 (xa,ya)) b@(Point2 (xb,yb)) c@(Point2 (xc,yc))
= (r, s, l2, i)
where l2 = sqrLengthOfSegment (Segment a b)
r = ((ya-yc)*(ya-yb)-(xa-xc)*(xb-xa)) / l2
s = ((ya-yc)*(xb-xa)-(xa-xc)*(yb-ya)) / l2
-- i = Point2 (xa + r*(xb-xa), ya + r*(yb-ya))
i = a + (r <*> (b - a))
\end{code}
\begin{code}
lengthOfSegment :: (Point p, Floating a, Eq a) => Line p a -> a
lengthOfSegment (Segment p q) = distance p q
sqrLengthOfSegment :: (Point p, Num a, Eq a) => Line p a -> a
sqrLengthOfSegment (Segment p q) = sqrDistance p q
centerOfSegment :: (Point p, Fractional a, Eq a) => Line p a -> p a
centerOfSegment (Segment p q) = 0.5 <*> (p <+> q)
perpendicular :: (Fractional a, Eq a) => L2 a -> L2 a
perpendicular s@(Segment p q) = Line p (p - (Point2 (ycoord q - ycoord p, xcoord p - xcoord q)))
where c = centerOfSegment s
bisector :: (Fractional a, Eq a) => L2 a -> L2 a
bisector s@(Segment p q) = Line c (c + (Point2 (ycoord q - ycoord p, xcoord p - xcoord q)))
where c = centerOfSegment s
\end{code}