@@ -8,6 +8,7 @@ export class RegionBuilder implements RegionRef {
88 this . data = {
99 id,
1010 bounds : null ,
11+ polygon : null ,
1112 center : null ,
1213 width : null ,
1314 height : null ,
@@ -27,17 +28,50 @@ export class RegionBuilder implements RegionRef {
2728
2829 rect ( b : Bounds ) : this {
2930 this . data . bounds = { ...b }
31+ this . data . polygon = null
3032 // Clear center/size if rect is used
3133 this . data . center = null
3234 this . data . width = null
3335 this . data . height = null
3436 return this
3537 }
3638
39+ polygon ( points : { x : number ; y : number } [ ] ) : this {
40+ if ( points . length < 3 ) {
41+ throw new TopologyError (
42+ `Region "${ this . data . id } " has invalid polygon: at least 3 points required` ,
43+ {
44+ regionIds : [ this . data . id ] ,
45+ suggestion : "Provide at least three polygon vertices" ,
46+ } ,
47+ )
48+ }
49+
50+ for ( const point of points ) {
51+ if ( ! Number . isFinite ( point . x ) || ! Number . isFinite ( point . y ) ) {
52+ throw new TopologyError (
53+ `Region "${ this . data . id } " has invalid polygon point` ,
54+ {
55+ regionIds : [ this . data . id ] ,
56+ suggestion : "Use finite numeric x/y values" ,
57+ } ,
58+ )
59+ }
60+ }
61+
62+ this . data . polygon = points . map ( ( p ) => ( { x : p . x , y : p . y } ) )
63+ this . data . bounds = null
64+ this . data . center = null
65+ this . data . width = null
66+ this . data . height = null
67+ return this
68+ }
69+
3770 center ( x : number , y : number ) : this {
3871 this . data . center = { x, y }
3972 // Clear bounds if center/size approach is used
4073 this . data . bounds = null
74+ this . data . polygon = null
4175 return this
4276 }
4377
@@ -56,6 +90,7 @@ export class RegionBuilder implements RegionRef {
5690 this . data . anchor = anchor
5791 // Clear bounds if center/size approach is used
5892 this . data . bounds = null
93+ this . data . polygon = null
5994 return this
6095 }
6196
0 commit comments