eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Polygon':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: Polygon


Inheritance:

   Object
   |
   +--Geometric
      |
      +--Polygon
         |
         +--Polyline

Package:
stx:libbasic2
Category:
Graphics-Geometry-Objects
Version:
rev: 1.38 date: 2022/03/08 22:45:44
user: cg
file: Polygon.st directory: libbasic2
module: stx stc-classLibrary: libbasic2

Description:


Polygon - an array of points

Adds simple boundary checking methods to Array.
(needs much more - such as inside check, area computation etc.)

copyright

COPYRIGHT (c) 1988 by Claus Gittinger All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

Class protocol:

instance creation
o  fromRectangle: aRectangle
return a new polygon, taking the rectangles vertices

Usage example(s):

     Polygon fromRectangle:(50@50 corner:100@100)

o  vertices: anArrayOfPoints
return a new polygon, given a collection of vertices

Usage example(s):

     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]).

Usage example(s):

     |p v|

     v := View new openAndWaitUntilVisible.
     p := Polygon 
            vertices:(Array with:10@10
                            with:20@10
                            with:20@30).
     p displayOn:v


Instance protocol:

accessing
o  add: aPoint

o  vertices
return the array containing my points

o  vertices: anArrayOfPoints
set the array containing my points

converting
o  asChaikinCurve
return a new polygon, which is generated by applying
the chaikin corner cutting algorithm once
(see the example on the class side)

o  asChaikinCurve: level
return a new polygon, which is generated by applying
the corner cutting algorithm n-times.
(see the example on the class side)

Usage example(s):

     |p p2 p3 v|
     
     p := Polygon 
            vertices:{
                0 @ 0 .
                0 @ 100 .
                100 @ 100 .
                110 @ -10 .
                200 @ 150 
            }.
            
     v := (View extent:300@300) openAndWaitUntilVisible.
     v translateBy:50@250.
     v scale:(1 @ -1).
     v paint:Color blue.
     p displayStrokedOn:v.

     p2 := p asChaikinCurve:1.
     v paint:Color black.
     p2 displayStrokedOn:v.

     p3 := p asChaikinCurve:10.
     v paint:Color red.
     p3 displayStrokedOn:v.

o  asPointArray
return an array containing my vertex points.
Notice, that no copy of my vertices is created - you should not
modify the returned collections points (unless you want to affect
the polygon ...).

displaying
o  displayFilledOn: aGC
display a filled polygin as represented by the receiver in
the graphicsContext, aGC

Usage example(s):

     |v|
     v := View new openAndWaitUntilVisible.

     (Polygon vertices:(
          Array
            with:10@10
            with:60@10
            with:35@60)) displayFilledOn:v

     |v|
     v := View new openAndWaitUntilVisible.

     (Polygon vertices:(
        Array
            with:10@10
            with:60@10
            with:35@60
            with:10@10)) displayStrokedOn:v

o  displayStrokedOn: aGC
display an unfilled polygin as represented by the receiver in
the graphicsContext, aGC

Usage example(s):

     |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].

     (Polygon vertices:(
        Array
            with:10@10
            with:60@10
            with:35@60
            with:10@10)) displayStrokedOn:v

enumerating
o  edgesDo: aTwoArgBlock
evaluate aTwoArgBlock for each pair of vertices

Usage example(s):

     |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] 

o  verticesDo: aBlock
evaluate aBlock for each point

Usage example(s):

     |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] 

queries
o  bottom
return the bottom boundary of the polygon,
that is the maximum y coordinate of all its points

Usage example(s):

     (Polygon vertices:(
	Array
	    with:10@10
	    with:60@10
	    with:35@60)) bottom 

o  computeBounds
return the smallest enclosing rectangle

o  containsPoint: aPoint
return true, if the argument, aPoint is contained in the receiver

Usage example(s):

     |p|

     p := Polygon vertices:(Array 
                             with:10@10
                             with:30@10
                             with:20@20).
     TestCase assert:(p containsPoint:14@11).     
     TestCase assert:(p containsPoint:15@15).     
     TestCase assert:(p containsPoint:5@15) not.
     TestCase assert:(p containsPoint:15@5) not.
     TestCase assert:(p containsPoint:30@15) not. 
     TestCase assert:(p containsPoint:20@15).       

o  left
return the left boundary of the polygon,
that is the minimum x coordinate of all its points

Usage example(s):

     (Polygon vertices:(
	Array
	    with:10@10
	    with:60@10
	    with:35@60)) left  

o  right
return the right boundary of the polygon,
that is the maximum x coordinate of all its points

Usage example(s):

     (Polygon vertices:(
	Array
	    with:10@10
	    with:60@10
	    with:35@60)) right  

o  top
return the top boundary of the polygon,
that is the minimum y coordinate of all its points

Usage example(s):

     (Polygon vertices:(
	Array
	    with:10@10
	    with:60@10
	    with:35@60)) top  

testing
o  canBeFilled
return true, if the receiver can be drawn as a filled geometric.
Always true here.

transformations
o  roundedToNearestIntegerPoint
return a copy of the receiver, where all points are rounded to ly on the nearest integer coordinate

o  transformedBy: aMatrix
return a copy of the receiver, which is transformed using aMatrix


Examples:


simple polygon; filled & unfilled:
  |v p|

  v := (View extent:200@200) openAndWaitUntilVisible.

  p := Polygon vertices:
              (Array with:(10@10)
                     with:(90@90)
                     with:(10@90)).

  v scale:2.
  v paint:Color blue.
  p displayFilledOn:v.

  v paint:Color red.
  p displayStrokedOn:v.

  v scale:1; translation:100@0.
  v paint:Color green.
  p displayFilledOn:v.

  v paint:Color black.
  p displayStrokedOn:v.
arbitrary polygon; filled & unfilled:
  |v p|

  v := (View extent:200@200) openAndWaitUntilVisible.
  v scale:2.

  p := Polygon vertices:
              (Array with:(10@10)
                     with:(90@90)
                     with:(50@90)
                     with:(90@10)
                     with:(10@90)
                                 ).

  v paint:Color blue.
  p displayFilledOn:v.

  v paint:Color red.
  p displayStrokedOn:v.
chaikin curve fitting:
   |p p2 v|

   p := Polygon 
          vertices:{
              0 @ 0 .
              0 @ 100 .
              100 @ 100 .
              110 @ -10 .
              200 @ 150 
          }.

   v := (View extent:300@300) openAndWaitUntilVisible.
   v translateBy:50@250.
   v scale:(1 @ -1).
   v paint:Color blue.
   p displayStrokedOn:v.

   p2 := p asChaikinCurve:10.
   v paint:Color red.
   p2 displayStrokedOn:v.


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Tue, 16 Jul 2024 06:58:42 GMT