Polygon.st
changeset 2091 24965fa2be83
parent 1430 baf6bfed6d56
child 2158 cc8120b2afc9
equal deleted inserted replaced
2090:4b87ce97bb6a 2091:24965fa2be83
     7  inclusion of the above copyright notice.   This software may not
     7  inclusion of the above copyright notice.   This software may not
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
       
    13 "{ Package: 'stx:libbasic2' }"
    12 "{ Package: 'stx:libbasic2' }"
    14 
    13 
    15 Geometric subclass:#Polygon
    14 Geometric subclass:#Polygon
    16 	instanceVariableNames:'vertices'
    15 	instanceVariableNames:'vertices'
    17 	classVariableNames:''
    16 	classVariableNames:''
   363 
   362 
   364     "Modified: 8.5.1996 / 20:51:42 / cg"
   363     "Modified: 8.5.1996 / 20:51:42 / cg"
   365     "Created: 12.2.1997 / 11:44:11 / cg"
   364     "Created: 12.2.1997 / 11:44:11 / cg"
   366 !
   365 !
   367 
   366 
       
   367 containsPoint:aPoint
       
   368     "return true, if the argument, aPoint is contained in the receiver"
       
   369 
       
   370     |angle pPrev p1 p2 angle2D|
       
   371 
       
   372     angle2D := [:p1 :p2 |
       
   373         "/   Return the angle between two vectors on a plane
       
   374         "/   The angle is from vector 1 to vector 2, positive anticlockwise
       
   375         "/   The result is between -pi -> pi
       
   376         |theta theta1 theta2|
       
   377 
       
   378         theta1 := p1 x arcTan2:p1 y.
       
   379         theta2 := p2 x arcTan2:p2 y.
       
   380         theta := theta2 - theta1.
       
   381         [theta > Float pi] whileTrue:[
       
   382             theta := theta - (Float pi * 2)
       
   383         ].
       
   384         [ theta < (Float pi negated) ] whileTrue:[
       
   385             theta := theta + (Float pi * 2)
       
   386         ].
       
   387         theta
       
   388     ].
       
   389 
       
   390     angle := 0.
       
   391 
       
   392     pPrev := vertices last.
       
   393     vertices do:[:pI |
       
   394         p1 := pPrev - aPoint.
       
   395         p2 := pI - aPoint.
       
   396         angle := angle + (angle2D value:p1 value:p2).
       
   397         pPrev := pI.
       
   398     ].
       
   399 
       
   400     ^ angle abs >= Float pi
       
   401 
       
   402     "
       
   403      |p|
       
   404 
       
   405      p := Polygon vertices:(Array 
       
   406                              with:10@10
       
   407                              with:30@10
       
   408                              with:20@20).
       
   409      TestCase assert:(p containsPoint:14@11).     
       
   410      TestCase assert:(p containsPoint:15@15).     
       
   411      TestCase assert:(p containsPoint:5@15) not.
       
   412      TestCase assert:(p containsPoint:15@5) not.
       
   413      TestCase assert:(p containsPoint:30@15) not. 
       
   414      TestCase assert:(p containsPoint:20@15).       
       
   415     "
       
   416 !
       
   417 
   368 left
   418 left
   369     "return the left boundary of the polygon,
   419     "return the left boundary of the polygon,
   370      that is the minimum x coordinate of all its points"
   420      that is the minimum x coordinate of all its points"
   371 
   421 
   372     (vertices size == 0) ifTrue: [^ nil].
   422     (vertices size == 0) ifTrue: [^ nil].
   417 ! !
   467 ! !
   418 
   468 
   419 !Polygon class methodsFor:'documentation'!
   469 !Polygon class methodsFor:'documentation'!
   420 
   470 
   421 version
   471 version
   422     ^ '$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.26 2004-03-20 13:19:25 cg Exp $'
   472     ^ '$Header: /cvs/stx/stx/libbasic2/Polygon.st,v 1.27 2009-01-21 23:22:24 cg Exp $'
   423 ! !
   473 ! !