Polygon.st
changeset 78 1be30ee4b5cc
parent 68 6650e0d50a1a
child 84 d401ce0001dc
--- a/Polygon.st	Thu Jun 01 21:41:40 1995 +0200
+++ b/Polygon.st	Tue Jun 06 06:02:23 1995 +0200
@@ -10,8 +10,8 @@
  hereby transferred.
 "
 
-Array subclass:#Polygon
-       instanceVariableNames:''
+Geometric subclass:#Polygon
+       instanceVariableNames:'vertices'
        classVariableNames:''
        poolDictionaries:''
        category:'Graphics-Geometry'
@@ -21,7 +21,7 @@
 COPYRIGHT (c) 1988 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.8 1995-03-18 05:08:10 claus Exp $
+$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.9 1995-06-06 04:02:10 claus Exp $
 '!
 
 !Polygon class methodsFor: 'documentation'!
@@ -42,76 +42,231 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.8 1995-03-18 05:08:10 claus Exp $
+$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.9 1995-06-06 04:02:10 claus Exp $
 "
 !
 
 documentation
 "
     Polygon - an array of points
+
     Adds simple boundary checking methods to Array.
     (needs much more - such as inside check, area computation etc.)
 "
 ! !
 
+!Polygon class methodsFor: 'instance creation'!
+
+vertices:anArrayOfPoints
+    ^ self new vertices:anArrayOfPoints
+
+    "
+     Polygon vertices:(Array with:10@10
+			     with:20@20
+			     with:30@30)
+
+     Polygon vertices:(#(10 10  100 0  50 50) pairWiseCollect:[:x :y | x @ y])
+    "
+! 
+
+fromRectangle:aRectangle
+    ^ self new vertices:(aRectangle asArrayOfPoints)
+
+    "
+     Polygon fromRectangle:(50@50 corner:100@100)
+    "
+! !
+
+!Polygon methodsFor: 'enumerating'!
+
+verticesDo:aBlock
+    "evaluate aBlock for each point"
+
+    vertices do:aBlock
+
+    "
+     |v p|
+
+     v := View new open.
+     [v shown] whileFalse:[Processor yield].
+
+     p := Polygon vertices:(Array with:5@5 
+				  with:50@5 
+				  with:30@30
+				  with:5@5).
+
+     p displayOn:v.
+     (Delay forSeconds:3) wait.
+
+     p verticesDo:[:p | v displayRectangleX:p x -3  y:p y -3  width:6 height:6] 
+    "
+!
+
+edgesDo:aTwoArgBlock
+    "evaluate aTwoArgBlock for each pair of vertices"
+
+    1 to:vertices size-1 do:[:i |
+	aTwoArgBlock value:(vertices at:i) value:(vertices at:i+1)
+    ].
+
+    "
+     |v p|
+
+     v := View new open.
+     [v shown] whileFalse:[Processor yield].
+
+     p := Polygon vertices:(Array with:5@5 
+				  with:50@5 
+				  with:30@30
+				  with:5@5).
+
+     p displayOn:v.
+     (Delay forSeconds:3) wait.
+
+     p edgesDo:[:p1 :p2 | v lineWidth:3. v displayLineFrom:p1 to:p2] 
+    "
+! !
+
 !Polygon methodsFor: 'accessing'!
 
+vertices
+    "return the array containing my points"
+
+    ^ vertices
+!
+
+vertices:anArrayOfPoints
+    "set the array containing my points"
+
+    vertices := anArrayOfPoints
+! !
+
+!Polygon methodsFor: 'queries'!
+
 top
     "return the top boundary of the polygon,
      that is the minimum y coordinate of all its points"
 
-    |minY|
+    (vertices size == 0) ifTrue: [^ nil].
+    ^ vertices inject:(vertices at:1) y into:[:minSoFar :p | minSoFar min:(p y)]
 
-    (self size == 0) ifTrue: [^ nil].
-    minY := (self at:1) y.
-    self do:[:p |
-	(p y < minY) ifTrue:[minY := p y].
-	0 "stc hint"
-    ].
-    ^ minY
+    "
+     (Polygon vertices:(
+	Array
+	    with:10@10
+	    with:60@10
+	    with:35@60)) top  
+    "
+
 !
 
 bottom
     "return the bottom boundary of the polygon,
      that is the maximum y coordinate of all its points"
 
-    |maxY|
+    (vertices size == 0) ifTrue: [^ nil].
+    ^ vertices inject:(vertices at:1) y into:[:maxSoFar :p | maxSoFar max:(p y)]
 
-    (self size == 0) ifTrue: [^ nil].
-    maxY := (self at:1) y.
-    self do:[:p |
-	(p y > maxY) ifTrue:[maxY := p y].
-	0 "stc hint"
-    ].
-    ^ maxY
+    "
+     (Polygon vertices:(
+	Array
+	    with:10@10
+	    with:60@10
+	    with:35@60)) bottom 
+    "
 !
 
 left
     "return the left boundary of the polygon,
      that is the minimum x coordinate of all its points"
 
-    |minX|
+    (vertices size == 0) ifTrue: [^ nil].
+    ^ vertices inject:(vertices at:1) x into:[:minSoFar :p | minSoFar min:(p x)]
 
-    (self size == 0) ifTrue: [^ nil].
-    minX := (self at:1) x.
-    self do:[:p |
-	(p x < minX) ifTrue:[minX := p x].
-	0 "stc hint"
-    ].
-    ^ minX
+    "
+     (Polygon vertices:(
+	Array
+	    with:10@10
+	    with:60@10
+	    with:35@60)) left  
+    "
+
 !
 
 right
     "return the right boundary of the polygon,
      that is the maximum y coordinate of all its points"
 
-    |maxX|
+    (vertices size == 0) ifTrue: [^ nil].
+    ^ vertices inject:(vertices at:1) x into:[:maxSoFar :p | maxSoFar max:(p x)]
+
+    "
+     (Polygon vertices:(
+	Array
+	    with:10@10
+	    with:60@10
+	    with:35@60)) right  
+    "
+
+! !
+
+!Polygon methodsFor:'displaying'!
+
+displayStrokedOn:aGC
+    aGC displayPolygon:vertices 
+
+    "
+     |v|
+
+     v := View new open.
+     [v shown] whileFalse:[Processor yield].
+
+     (Polygon vertices:(
+	Array
+	    with:10@10
+	    with:60@10
+	    with:35@60)) displayStrokedOn:v
+
+     |v|
+
+     v := View new open.
+     [v shown] whileFalse:[Processor yield].
 
-    (self size == 0) ifTrue: [^ nil].
-    maxX := (self at:1) x.
-    self do:[:p |
-	(p x > maxX) ifTrue:[maxX := p x].
-	0 "stc hint"
-    ].
-    ^ maxX
+     (Polygon vertices:(
+	Array
+	    with:10@10
+	    with:60@10
+	    with:35@60
+	    with:10@10)) displayStrokedOn:v
+    "
+
+!
+
+displayFilledOn:aGC
+    aGC fillPolygon:vertices 
+
+    "
+     |v|
+
+     v := View new open.
+     [v shown] whileFalse:[Processor yield].
+
+     (Polygon vertices:(
+	  Array
+	    with:10@10
+	    with:60@10
+	    with:35@60)) displayFilledOn:v
+
+     |v|
+
+     v := View new open.
+     [v shown] whileFalse:[Processor yield].
+
+     (Polygon vertices:(
+	Array
+	    with:10@10
+	    with:60@10
+	    with:35@60
+	    with:10@10)) displayFilledOn:v
+    "
 ! !