forked from williamngan/pts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug fixes on Pt constructor and CanvasSpace
- Loading branch information
1 parent
1680d5e
commit bdff227
Showing
9 changed files
with
1,377 additions
and
137 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,99 @@ | ||
import {Pt, IPt} from "./Pt" | ||
|
||
|
||
export class Bound extends Pt { | ||
export class Bound implements IPt{ | ||
|
||
protected _center:Pt = new Pt(); | ||
protected _size:Pt = new Pt(); | ||
protected _topLeft:Pt = new Pt(); | ||
protected _bottomRight:Pt = new Pt(); | ||
|
||
constructor(x?:number|Array<number>|IPt, y=0, z=0, w=0) { | ||
super(x,y,z,w); | ||
if (!this.z) this.z = 0; | ||
this._update(); | ||
constructor( p1?:IPt, p2?:IPt ) { | ||
if (!p2) { | ||
this._size = new Pt(p1); | ||
} else if (p1) { | ||
this._topLeft = new Pt(p1); | ||
this._bottomRight = new Pt(p2); | ||
this._updateSize(); | ||
} | ||
} | ||
|
||
bound( x?:number|Array<number>|IPt, y?:number, z?:number, w?:number ) { | ||
let b = Pt.getArgs( [x,y,z,w] ); | ||
this.set(5, (b[0] || 0)); | ||
this.set(6, (b[1] || 0)); | ||
this.set(7, (b[2] || 0)); | ||
this._update(); | ||
protected _updateSize() { | ||
this._size = this._bottomRight.$subtract( this._topLeft ).abs(); | ||
this._updateCenter(); | ||
} | ||
|
||
static fromBoundingRect( rect:ClientRect ) { | ||
let b = new Bound(); | ||
if (rect.top) b.y = rect.top; | ||
if (rect.left) b.x = rect.left; | ||
b.width = (rect.width) ? rect.width : ((rect.right) ? rect.right-rect.left : 0); | ||
b.height = (rect.height) ? rect.height : ((rect.bottom) ? rect.bottom-rect.top : 0); | ||
return b; | ||
protected _updateCenter() { | ||
this._center = this._size.$scale(0.5).add( this._topLeft ); | ||
} | ||
|
||
protected _updatePosFromTop() { | ||
this._bottomRight = this._topLeft.$add( this._size ); | ||
this._updateCenter(); | ||
} | ||
|
||
protected _updatePosFromBottom() { | ||
this._topLeft = this._bottomRight.$subtract( this._size ); | ||
this._updateCenter(); | ||
} | ||
|
||
protected _updatePosFromCenter() { | ||
let half = this._size.$scale(0.5); | ||
this._topLeft = this._center.$subtract( half ); | ||
this._bottomRight = this._center.$add( half ); | ||
} | ||
|
||
|
||
public get size():Pt { return new Pt(this._size); } | ||
public set size(p: Pt) { | ||
this._size = new Pt(p); | ||
this._updatePosFromTop(); | ||
} | ||
|
||
protected _update( which?:string ) { | ||
if (!which || which == "x" || which == "w") this.set(8, this.x + this.width/2); | ||
if (!which || which == "y" || which == "h") this.set(9, this.y + this.height/2); | ||
if (!which || which == "z" || which == "d") this.set(10, this.z + this.depth/2); | ||
public get center():Pt { return new Pt(this._center); } | ||
public set center( p:Pt ) { | ||
this._center = new Pt(p); | ||
this._updatePosFromCenter(); | ||
} | ||
|
||
setSize( w:number, h:number, d?:number ):this { | ||
this.width = w; | ||
this.height = h; | ||
if (d != undefined) this.depth = d; | ||
return this; | ||
public get topLeft():Pt { return new Pt(this._topLeft); } | ||
public set topLeft( p:Pt ) { | ||
this._topLeft = new Pt(p); | ||
this._updateSize(); | ||
} | ||
|
||
set x( _x:number ) { this.set(0, _x); this._update('x'); } | ||
set y( _y:number ) { this.set(0, _y); this._update('y'); } | ||
set z( _z:number ) { this.set(0, _z); this._update('z'); } | ||
public get bottomRight():Pt { return new Pt(this._bottomRight); } | ||
public set bottomRight( p:Pt ) { | ||
this._bottomRight = new Pt(p); | ||
this._updateSize(); | ||
} | ||
|
||
get width():number { return this.get(5); } | ||
set width( _w:number ) { this.set(5, _w); this._update('w'); } | ||
public get width():number { return this._size.x; } | ||
public set width( w:number ) { | ||
this._size.x = w; | ||
this._updatePosFromTop(); | ||
} | ||
|
||
get height():number { return this.get(6); } | ||
set height( _h:number ) { this.set(6, _h); this._update('h'); } | ||
public get height():number { return this._size.y; } | ||
public set height( h:number ) { | ||
this._size.y = h; | ||
this._updatePosFromTop(); | ||
} | ||
|
||
get size():Pt { return new Pt( this.width, this.height, this.depth ); } | ||
public get depth():number { return this._size.z; } | ||
public set depth( d:number ) { | ||
this._size.z = d; | ||
this._updatePosFromTop(); | ||
} | ||
|
||
get depth():number { return this.get(7); } | ||
set depth( _d:number ) { this.set(7, _d); this._update('d'); } | ||
public get x():number { return this.topLeft.x; } | ||
public get y():number { return this.topLeft.y; } | ||
public get z():number { return this.topLeft.z; } | ||
|
||
get center():Pt { return new Pt( this.get(8), this.get(9), this.get(10) ); } | ||
get centerX(): number { return this.get(8); } | ||
get centerY(): number { return this.get(9); } | ||
get centerZ(): number { return this.get(10); } | ||
static fromBoundingRect( rect:ClientRect ) { | ||
let b = new Bound( new Pt( rect.left||0, rect.top||0 ), new Pt( rect.right||0, rect.bottom||0 ) ); | ||
if (rect.width && rect.height) b.size = new Pt(rect.width, rect.height); | ||
return b; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.