Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding elements

  • Loading branch information...
commit 1cd35eabd6ec2cdb2db249e2bb97893d21077101 1 parent 4f06e0c
nick orton authored
40 src/techne/geometria/elements.clj
View
@@ -0,0 +1,40 @@
+(ns techne.geometria.elements)
+
+
+(defprotocol Point
+ (x-cord [p])
+ (y-cord [p]))
+
+(defprotocol Line
+ (slope [l])
+ (offset [l]))
+
+(defprotocol Line-Segment
+ (line-slope [ls])
+ (points [ls])
+ (segment->line [ls]))
+
+(defn point [x y]
+ (reify Point
+ (x-cord [_] x)
+ (y-cord [_] y)))
+
+(defn line [m c]
+ (reify Line
+ (slope [_] m)
+ (offset [_] c)))
+
+(defn line-segment [point1 point2]
+ (reify Line-Segment
+ (line-slope [_]
+ (let [rise (- (y-cord point1) (y-cord point2))
+ run (- (x-cord point1) (x-cord point2))]
+ (/ rise run)))
+ (points [_]
+ #{point1 point2})
+ (segment->line [seg]
+ (let [m (line-slope seg)
+ y (y-cord point1)
+ x (x-cord point1)
+ c (- y (* m x))]
+ (line m c)))))
17 test/techne/geometria/elements_test.clj
View
@@ -0,0 +1,17 @@
+(ns techne.geometria.elements-test
+ (:use [clojure.test]
+ [techne.geometria.elements]))
+
+(def seg
+ (let [p1 (point 1 2)
+ p2 (point 2 5)]
+ (line-segment p1 p2)))
+
+(deftest test-line-segment-slope
+ (is (= 3 (line-slope seg))))
+
+(deftest test-segment->line
+ (let [line (segment->line seg)]
+ (is (= 3 (slope line)))
+ (is (= -1 (offset line)))))
+
Please sign in to comment.
Something went wrong with that request. Please try again.