import { Geometry } from "./geometry" ; import { Bound } from "../util/bound" ; import { Projection } from "../projection/projection" ; export class Polygon extends Geometry { private _lnglats : number [ ] [ ] [ ] ; private _coordinates : number [ ] [ ] [ ] ; private _projection : Projection ; private _bound : Bound ; get bound ( ) : Bound { return this . _bound ; } constructor ( lnglats ) { super ( ) ; this . _lnglats = lnglats ; } ; addTo ( map ) { this . _projection = map . projection ; this . _coordinates = this . _lnglats . map ( ( ring ) => ring . map ( ( point ) => this . _projection . project ( point ) ) ) ; let xmin = Number . MAX_VALUE , ymin = Number . MAX_VALUE , xmax = - Number . MAX_VALUE , ymax = - Number . MAX_VALUE ; this . _coordinates . forEach ( ring => { ring . forEach ( point => { xmin = Math . min ( xmin , point [ 0 ] ) ; ymin = Math . min ( ymin , point [ 1 ] ) ; xmax = Math . max ( xmax , point [ 0 ] ) ; ymax = Math . max ( ymax , point [ 1 ] ) ; } ) ; } ) ; this . _bound = new Bound ( xmin , ymin , xmax , ymax ) ; map . addGeometry ( this ) ; } draw ( ctx ) { ctx . save ( ) ; ctx . strokeStyle = "#ff0000" ; ctx . fillStyle = "#ff0000" ; ctx . lineWidth = 2 ; ctx . beginPath ( ) ; const matrix = ( ctx as any ) . getTransform ( ) ; ctx . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ; this . _coordinates . forEach ( ring => { ring . forEach ( ( point , index ) => { let x = point [ 0 ] , y = point [ 1 ] ; if ( index === 0 ) { ctx . moveTo ( ( matrix . a * x + matrix . e ) , ( matrix . d * y + matrix . f ) ) ; } else { ctx . lineTo ( ( matrix . a * x + matrix . e ) , ( matrix . d * y + matrix . f ) ) ; } } ) ; } ) ; ctx . closePath ( ) ; ctx . fill ( "evenodd" ) ; ctx . stroke ( ) ; ctx . restore ( ) ; } }