comparison doodle/tk/test/test1.d @ 33:157b4ad5615d

Added intersection code for lines and segments. Wrote my first unit test to for geometry stuff.
author David Bryant <bagnose@gmail.com>
date Sun, 30 Aug 2009 01:34:14 +0930
parents
children 1b4c9ba58673
comparison
equal deleted inserted replaced
32:705817d8514a 33:157b4ad5615d
1 private {
2 import doodle.tk.geometry;
3 import std.stdio;
4 }
5
6 void test1() {
7 writefln("*** Test1 ***");
8
9 Point p1 = Point.DEFAULT;
10 Point p2 = p1; // copy construction
11 assert(p1 == p2); // equality
12 assert(!(p1 != p2)); // inequality
13 p2 = p1; // assignment
14 assert(p1 == p2);
15
16 Point p3 = Point(3.0, 5.0); // standard constructor
17 assert(p3 - p3 == Vector.DEFAULT); // subtraction (point minus point)
18
19 Vector v3 = Vector(3.0, 5.0);
20 assert(p3 - v3 == Point.DEFAULT); // subtraction (point minus vector)
21
22 Point p4 = Point(1.0, 10.0);
23 Point p5 = Point(10.0, 1.0);
24 assert(min_extents(p4, p5) == Point(1.0, 1.0)); // min extents
25 assert(max_extents(p4, p5) == Point(10.0, 10.0)); // max extents
26
27 writefln("p1: %s", p1); // toString
28 }
29
30 void test2() {
31 writefln("*** Test2 ***");
32
33 Vector v1 = Vector.DEFAULT;
34 Vector v2 = v1; // copy construction
35 assert(v1 == v2); // equality
36 assert(!(v1 != v2)); // inequality
37 v2 = v1; // assignment
38
39 Vector v3 = Vector(3.0, 4.0); // standard construction
40 assert(v3 + v3 == Vector(6.0, 8.0)); // addition
41 assert(v3 - v3 == Vector.DEFAULT); // subtraction
42 assert(-v3 == Vector(-3.0, -4.0)); // negation
43 assert(v3.length == 5.0); // length
44 assert(2.0 * v3 == Vector(6.0, 8.0)); // scalar multiplication
45 assert(v3 / 2.0 == Vector(1.5, 2.0)); // scalar division
46
47 writefln("v1: %s", v1); // toString
48 }
49
50 void test3() {
51 writefln("*** Test3 ***");
52
53 // Horizontal axis
54 Line l1 = Line(Point(0.0, 0.0), Vector(1.0, 0.0));
55 // Vertical axis
56 Line l2 = Line(Point(0.0, 0.0), Vector(0.0, 1.0));
57
58 Point p;
59 bool b = intersection(l1, l2, p);
60 assert(b);
61 assert(p == Point(0.0, 0.0));
62 }
63
64 void test4() {
65 writefln("*** Test4 ***");
66
67 Line l1 = Line(Point(-1.0, -1.0), Vector( 1.0, 1.0));
68 Line l2 = Line(Point( 1.0, -1.0), Vector(-1.0, 1.0));
69
70 Point p;
71 bool b = intersection(l1, l2, p);
72 assert(b);
73 assert(p == Point(0.0, 0.0));
74 }
75
76 void test5() {
77 writefln("*** Test5 ***");
78
79 // Here the segments intersect
80
81 Segment s1 = Segment(Point(2.0, 2.0), Point(4.0, 4.0));
82 Segment s2 = Segment(Point(2.0, 4.0), Point(4.0, 2.0));
83
84 Point p;
85 bool b = intersection(s1, s2, p);
86 assert(b);
87 writefln("p: %s", p);
88 assert(p == Point(3.0, 3.0));
89 }
90
91 void test6() {
92 writefln("*** Test6 ***");
93
94 // Here the lines of the segments intersect but the segments don't
95
96 Segment s1 = Segment(Point(2.0, 2.0), Point(4.0, 4.0));
97 Segment s2 = Segment(Point(4.0, 2.0), Point(6.0, 0.0));
98
99 Point p;
100 bool b = intersection(s1, s2, p);
101 assert(!b);
102 }
103
104 void main(string[] args) {
105 test1();
106 test2();
107 test3();
108 test4();
109 test5();
110 test6();
111 }