home *** CD-ROM | disk | FTP | other *** search
- class smashing.Line
- {
- var p0;
- var p1;
- var vector;
- var lineNormal;
- var vN;
- var nLength;
- var normal;
- var bChanged = true;
- function Line(x0, y0, x1, y1)
- {
- this.p0 = new smashing.Point(x0,y0);
- this.p1 = new smashing.Point(x1,y1);
- this.vector = new smashing.Point(this.p1.x - this.p0.x,this.p1.y - this.p0.y);
- this.lineNormal = this.vector.normalize();
- this.vN = new smashing.Point(this.vector.y,- this.vector.x);
- this.vN.normalizeMe();
- this.nLength = this.vector.length;
- }
- function get normal()
- {
- return this.vN;
- }
- function setLine(x0, y0, x1, y1)
- {
- this.p0.x = x0;
- this.p0.y = y0;
- this.p1.x = x1;
- this.p1.y = y1;
- this.vector.x = this.p1.x - this.p0.x;
- this.vector.y = this.p1.y - this.p0.y;
- this.lineNormal = this.vector.normalize();
- this.vN = new smashing.Point(this.vector.y,- this.vector.x);
- this.vN.normalizeMe();
- this.nLength = this.vector.length;
- this.bChanged = true;
- }
- function move(nX, nY)
- {
- this.p0.x += nX;
- this.p0.y += nY;
- this.p1.x += nX;
- this.p1.y += nY;
- }
- function collisionPointSwept(p, m, radius)
- {
- var _loc26_ = m.dot(this.normal);
- if(_loc26_ >= 0)
- {
- return null;
- }
- var _loc19_ = new smashing.Point(p.x - this.p0.x,p.y - this.p0.y);
- var _loc9_ = this.normal.copy();
- _loc9_.multiplyMe(radius);
- var _loc4_ = _loc19_.copy();
- _loc4_.subtractPointMe(_loc9_);
- var _loc6_ = ((- _loc4_.y) * m.x + _loc4_.x * m.y) / (m.y * this.vector.x + (- m.x) * this.vector.y);
- var _loc5_ = undefined;
- if(_loc6_ >= 0 && _loc6_ <= 1)
- {
- var _loc3_ = new smashing.Point(this.vector.x * _loc6_,this.vector.y * _loc6_);
- var _loc8_ = _loc3_.x - _loc4_.x;
- var _loc7_ = _loc3_.y - _loc4_.y;
- var _loc13_ = m.x;
- var _loc12_ = m.y;
- var _loc28_ = new smashing.Point(_loc3_.x - _loc4_.x,_loc3_.y - _loc4_.y);
- if(_loc8_ * _loc13_ + _loc7_ * _loc12_ < 0)
- {
- return null;
- }
- _loc8_ *= _loc8_;
- _loc7_ *= _loc7_;
- _loc13_ *= _loc13_;
- _loc12_ *= _loc12_;
- var _loc25_ = _loc8_ + _loc7_;
- if(_loc25_ > _loc13_ + _loc12_)
- {
- return null;
- }
- _loc3_.x += this.p0.x + _loc9_.x;
- _loc3_.y += this.p0.y + _loc9_.y;
- _loc5_ = new smashing.CollisionResult(_loc3_,this.normal,_loc25_);
- _loc5_.gizmo = this;
- _loc5_.lHit = _loc3_;
- }
- else
- {
- var _loc18_ = m.normalize();
- if(_loc6_ < 0)
- {
- var _loc23_ = this.p0;
- var _loc20_ = _loc19_.reverse();
- }
- else if(_loc6_ > 0)
- {
- _loc23_ = this.p1;
- _loc20_ = new smashing.Point(this.p1.x - p.x,this.p1.y - p.y);
- }
- var _loc14_ = _loc20_.dot(_loc18_);
- if(m.length < Math.abs(_loc14_) - radius)
- {
- return null;
- }
- var _loc17_ = _loc20_.length;
- var _loc24_ = _loc17_ * _loc17_ - _loc14_ * _loc14_;
- var _loc21_ = radius * radius;
- if(_loc24_ > _loc21_)
- {
- return null;
- }
- var _loc10_ = Math.sqrt(_loc21_ - _loc24_) - _loc14_;
- if(Math.abs(_loc10_) > m.length)
- {
- return null;
- }
- var _loc15_ = _loc18_.multiply(_loc10_);
- if(_loc15_.dot(m) > 0)
- {
- return null;
- }
- var _loc16_ = new smashing.Point(p.x - _loc15_.x,p.y - _loc15_.y);
- var _loc27_ = new smashing.Point(_loc16_.x - _loc23_.x,_loc16_.y - _loc23_.y).normalize();
- _loc5_ = new smashing.CollisionResult(_loc16_,_loc27_,_loc10_ * _loc10_);
- _loc5_.gizmo = this;
- _loc5_.lHit = _loc15_;
- }
- return _loc5_;
- }
- function collisionPoint(p, m, r)
- {
- var _loc18_ = m.dot(this.normal);
- if(_loc18_ > 0)
- {
- return null;
- }
- var _loc3_ = new smashing.Point(p.x + m.x,p.y + m.y);
- var _loc12_ = this.vector.x * (_loc3_.x - this.p0.x) + this.vector.y * (_loc3_.y - this.p0.y);
- var _loc13_ = this.vector.dot(this.vector);
- var _loc2_ = undefined;
- var _loc9_ = undefined;
- var _loc7_ = undefined;
- if(_loc12_ < 0)
- {
- _loc2_ = new smashing.Point(_loc3_.x - this.p0.x,_loc3_.y - this.p0.y);
- _loc9_ = _loc2_.dot(_loc2_);
- if(_loc9_ > r * r)
- {
- return null;
- }
- var _loc11_ = _loc2_.normalize();
- _loc2_.length = r;
- var _loc5_ = this.p0.addPoint(_loc2_);
- var _loc14_ = _loc5_.x - p.x;
- var _loc16_ = _loc5_.y - p.y;
- _loc7_ = new smashing.CollisionResult(_loc5_,_loc11_,_loc14_ * _loc14_ + _loc16_ * _loc16_);
- _loc7_.lHit = this.p0;
- _loc7_.nRatio = 0;
- _loc7_.gizmo = this;
- }
- else if(_loc12_ > _loc13_)
- {
- _loc2_ = new smashing.Point(_loc3_.x - this.p1.x,_loc3_.y - this.p1.y);
- _loc9_ = _loc2_.dot(_loc2_);
- if(_loc9_ > r * r)
- {
- return null;
- }
- _loc11_ = _loc2_.normalize();
- _loc2_.length = r;
- _loc5_ = this.p1.addPoint(_loc2_);
- _loc14_ = _loc5_.x - p.x;
- _loc16_ = _loc5_.y - p.y;
- _loc7_ = new smashing.CollisionResult(_loc5_,_loc11_,_loc14_ * _loc14_ + _loc16_ * _loc16_);
- _loc7_.lHit = this.p1;
- _loc7_.nRatio = 1;
- _loc7_.gizmo = this;
- }
- else
- {
- var _loc10_ = _loc12_ / _loc13_;
- var _loc8_ = new smashing.Point(this.p0.x + this.vector.x * _loc10_,this.p0.y + this.vector.y * _loc10_);
- var _loc17_ = _loc3_.x - _loc8_.x;
- var _loc15_ = _loc3_.y - _loc8_.y;
- if(_loc17_ * _loc17_ + _loc15_ * _loc15_ > r * r)
- {
- return null;
- }
- _loc5_ = new smashing.Point(_loc8_.x + this.normal.x * r,_loc8_.y + this.normal.y * r);
- _loc14_ = _loc5_.x - p.x;
- _loc16_ = _loc5_.y - p.y;
- _loc7_ = new smashing.Line(_loc5_.x + _loc11_.x,_loc5_.y + _loc11_.y,_loc5_.x,_loc5_.y);
- var _loc19_ = _loc14_ * _loc14_ + _loc16_ * _loc16_;
- _loc7_ = new smashing.CollisionResult(_loc5_,this.normal,_loc19_);
- _loc7_.lHit = _loc8_;
- _loc7_.nRatio = _loc10_;
- _loc7_.gizmo = this;
- }
- return _loc7_;
- }
- function collisionPointThresh(p, m, r)
- {
- var _loc16_ = m.dot(this.normal);
- if(_loc16_ > 0)
- {
- return null;
- }
- var _loc12_ = new smashing.Point(p.x + m.x,p.y + m.y);
- var _loc2_ = new smashing.Point(_loc12_.x - this.p0.x,_loc12_.y - this.p0.y);
- var _loc13_ = _loc2_.dot(this.vN);
- if(Math.abs(_loc13_) > r)
- {
- return null;
- }
- if(_loc13_ < r / 2)
- {
- return null;
- }
- var _loc10_ = _loc2_.dot(this.lineNormal);
- var _loc7_ = undefined;
- var _loc6_ = undefined;
- if(_loc10_ < 0)
- {
- _loc7_ = _loc2_.dot(_loc2_);
- if(_loc7_ > r * r)
- {
- return null;
- }
- var _loc9_ = _loc2_.normalize();
- _loc2_.length = r;
- var _loc4_ = this.p0.addPoint(_loc2_);
- var _loc14_ = _loc4_.x - p.x;
- var _loc15_ = _loc4_.y - p.y;
- _loc6_ = new smashing.CollisionResult(_loc4_,_loc9_,_loc14_ * _loc14_ + _loc15_ * _loc15_);
- _loc6_.lHit = this.p0;
- _loc6_.nRatio = 0;
- _loc6_.gizmo = this;
- }
- else if(_loc10_ > this.nLength)
- {
- _loc7_ = _loc2_.dot(_loc2_);
- if(_loc7_ > r * r)
- {
- return null;
- }
- _loc9_ = _loc2_.normalize();
- _loc2_.length = r;
- _loc4_ = this.p1.addPoint(_loc2_);
- _loc14_ = _loc4_.x - p.x;
- _loc15_ = _loc4_.y - p.y;
- _loc6_ = new smashing.CollisionResult(_loc4_,_loc9_,_loc14_ * _loc14_ + _loc15_ * _loc15_);
- _loc6_.lHit = this.p1;
- _loc6_.nRatio = 1;
- _loc6_.gizmo = this;
- }
- else
- {
- var _loc8_ = _loc10_ / this.nLength;
- var _loc11_ = new smashing.Point(this.p0.x + this.vector.x * _loc8_,this.p0.y + this.vector.y * _loc8_);
- _loc4_ = new smashing.Point(_loc11_.x + this.normal.x * r,_loc11_.y + this.normal.y * r);
- _loc14_ = _loc4_.x - p.x;
- _loc15_ = _loc4_.y - p.y;
- _loc6_ = new smashing.Line(_loc4_.x + _loc9_.x,_loc4_.y + _loc9_.y,_loc4_.x,_loc4_.y);
- var _loc17_ = _loc14_ * _loc14_ + _loc15_ * _loc15_;
- _loc6_ = new smashing.CollisionResult(_loc4_,this.normal,_loc17_);
- _loc6_.lHit = _loc11_;
- _loc6_.nRatio = _loc8_;
- _loc6_.gizmo = this;
- }
- return _loc6_;
- }
- function nearestPointSeg(m0)
- {
- var _loc5_ = new smashing.Point(m0.x - this.p0.x,m0.y - this.p0.y);
- var _loc2_ = this.vector.dot(_loc5_);
- var _loc4_ = this.vector.dot(this.vector);
- if(_loc2_ < 0)
- {
- return this.p0;
- }
- if(_loc2_ > _loc4_)
- {
- return this.p1;
- }
- var _loc3_ = _loc2_ / _loc4_;
- return new smashing.Point(this.p0.x + this.vector.x * _loc3_,this.p0.y + this.vector.y * _loc3_);
- }
- function nearestPointLine(m0)
- {
- var _loc5_ = new smashing.Point(m0.x - this.p0.x,m0.y - this.p0.y);
- var _loc3_ = this.vector.dot(_loc5_);
- var _loc4_ = this.vector.dot(this.vector);
- var _loc2_ = _loc3_ / _loc4_;
- return new smashing.Point(this.p0.x + this.vector.x * _loc2_,this.p0.y + this.vector.y * _loc2_);
- }
- function nearestPointRatio(p)
- {
- var _loc5_ = new smashing.Point(p.x - this.p0.x,p.y - this.p0.y);
- var _loc3_ = this.vector.dot(_loc5_);
- var _loc4_ = this.vector.dot(this.vector);
- var _loc2_ = _loc3_ / _loc4_;
- return _loc2_;
- }
- function distanceFrom(p)
- {
- var _loc3_ = new smashing.Point(p.x - this.p0.x,p.y - this.p0.y);
- var _loc2_ = this.normal.dot(_loc3_);
- return _loc2_;
- }
- function intersection(op0, op1, oVector)
- {
- var _loc11_ = undefined;
- var _loc9_ = undefined;
- var _loc7_ = undefined;
- var _loc10_ = undefined;
- var _loc8_ = undefined;
- var _loc6_ = undefined;
- var _loc5_ = undefined;
- var _loc4_ = undefined;
- var _loc3_ = undefined;
- var _loc12_ = undefined;
- var _loc13_ = undefined;
- if(this.vector.x != 0)
- {
- _loc4_ = this.vector.y / this.vector.x;
- }
- else
- {
- _loc4_ = Infinity;
- }
- if(op1.x - op0.x != 0)
- {
- _loc3_ = (op1.y - op0.y) / (op1.x - op0.x);
- }
- else
- {
- _loc3_ = Infinity;
- }
- _loc11_ = _loc4_;
- _loc10_ = _loc3_;
- _loc9_ = -1;
- _loc8_ = -1;
- _loc7_ = this.p0.y - _loc4_ * this.p0.x;
- _loc6_ = op0.y - _loc3_ * op0.x;
- _loc5_ = 1 / (_loc11_ * _loc8_ - _loc10_ * _loc9_);
- _loc12_ = (_loc9_ * _loc6_ - _loc8_ * _loc7_) * _loc5_;
- _loc13_ = (_loc10_ * _loc7_ - _loc11_ * _loc6_) * _loc5_;
- return new smashing.Point(_loc12_,_loc13_);
- }
- function toString()
- {
- return "Line(" + this.p0.x + ", " + this.p0.y + ", " + this.p1.x + ", " + this.p1.y + ")";
- }
- }
-