Merge: new `with` statement
[nit.git] / lib / geometry / points_and_lines.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Provides interfaces and classes to represent basic geometry needs.
18 module points_and_lines
19
20 # An abstract 2d point, strongly linked to its implementation `Point`
21 interface IPoint[N: Numeric]
22 # horizontal coordinate
23 fun x: N is abstract
24 # vertical coordinate
25 fun y: N is abstract
26
27 redef fun to_s do return "({x}, {y})"
28 end
29
30 # A 2d point and an implementation of `IPoint`
31 class Point[N: Numeric]
32 super IPoint[N]
33
34 redef var x: N
35 redef var y: N
36 end
37
38 # An abstract 3d point, strongly linked to its implementation `Point3d`
39 interface IPoint3d[N: Numeric]
40 super IPoint[N]
41
42 # depth coordinate
43 fun z: N is abstract
44
45 redef fun to_s do return "({x}, {y}, {z})"
46 end
47
48 # A 3d point and an implementation of `IPoint3d`
49 class Point3d[N: Numeric]
50 super IPoint3d[N]
51 super Point[N]
52
53 redef var z: N
54 end
55
56 # An abstract 2d line segment
57 interface ILine[N: Numeric]
58 # The type of points that ends the segment
59 type P: IPoint[N]
60
61 # The point that is the left-end of the segment
62 fun point_left: P is abstract
63
64 # The point that is the right-end of the segment
65 fun point_right: P is abstract
66
67 redef fun to_s do return "{point_left}--{point_right}"
68 end
69
70 # A 2d line segment
71 class Line[N: Numeric]
72 super ILine[N]
73
74 redef var point_left: P
75 redef var point_right: P
76
77 init
78 do
79 var a = point_left
80 var b = point_right
81 if a.x > b.x then
82 point_left = b
83 point_right = a
84 end
85 end
86
87 end
88
89 # An abstract 3d line segment
90 interface ILine3d[N: Numeric]
91 super ILine[N]
92
93 redef type P: IPoint3d[N]
94 end
95
96 # A 3d line segment
97 class Line3d[N: Numeric]
98 super Line[N]
99 super ILine3d[N]
100 end