| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #Import "<mojo>"
- #Import "<std>"
- Using mojo..
- Using std..
- Class PVector2D
- Field x:Float
- Field y:Float
-
- Method New()
- End method
-
- Method New(x:Float,y:Float)
- Set(x,y)
- End Method
-
- Method New(v:PVector2D)
- Set(v)
- End Method
-
- Method Set:PVector2D(v:PVector2D)
- x = v.x
- y = v.y
- Return Self
- End Method
- Method Set:PVector2D(x:Float,y:Float)
- Self.x = x
- Self.y = y
- Return Self
- End Method
-
- Method Add:PVector2D(x:Float,y:Float)
- Self.x += x
- Self.y += y
- Return Self
- End Method
-
- Method Add:PVector2D(v:PVector2D)
- Self.x += v.x
- Self.y += v.y
- Return Self
- End Method
-
- Method Add:PVector2D(value:Float)
- Self.x += value
- Self.y += value
- Return Self
- End Method
-
- Method Subtract:PVector2D(x:Float,y:Float)
- Self.x -= x
- Self.y -= y
- Return Self
- End Method
-
- Method Subtract:PVector2D(v:PVector2D)
- Self.x -= v.x
- Self.y -= v.y
- Return Self
- End Method
-
- Method Subtract:PVector2D(value:Float)
- Self.x -= value
- Self.y -= value
- Return Self
- End Method
-
- Method Multiply:PVector2D(x:Float,y:Float)
- Self.x *= x
- Self.y *= y
- Return Self
- End Method
-
- Method Multiply:PVector2D(v:PVector2D)
- Self.x *= v.x
- Self.y *= v.y
- Return Self
- End Method
-
- Method Multiply:PVector2D(value:Float)
- Self.x *= value
- Self.y *= value
- Return Self
- End Method
-
- Method Divide:PVector2D(x:Float,y:Float)
- Self.x /= x
- Self.y /= y
- Return Self
- End Method
-
- Method Divide:PVector2D(v:PVector2D)
- Self.x /= v.x
- Self.y /= v.y
- Return Self
- End Method
-
- Method Divide:PVector2D(value:Float)
- Self.x /= value
- Self.y /= value
- Return Self
- End Method
-
- Method DotProduct:Float(x:Float,y:Float)
- Return Self.x * x + Self.y * y
- End Method
-
- Method DotProduct:Float(v:PVector2D)
- Return Self.x * v.x + Self.y * v.y
- End Method
-
- Method PerpDotProduct:Float(x:Float,y:Float)
- Return Self.x * y - Self.y * x
- End Method
-
- Method PerpDotProduct:Float(v:PVector2D)
- Return Self.x * v.y - Self.y * v.x
- End Method
-
- Method MagnitudeSquare:Float()
- Return Self.x * Self.x + Self.y * Self.y
- End Method
-
- Method Magnitude:Float()
- Return Sqrt(MagnitudeSquare())
- End Method
-
- Method LeftNormal:PVector2D()
- Local n:Float = y
- y = -x
- x = n
- Return Self
- End Method
-
- Method RightNormal:PVector2D()
- Local n:Float = y
- y = x
- x = -n
- Return Self
- End Method
-
- Method Normalize:PVector2D()
- Divide(Magnitude())
- Return Self
- End Method
-
- Method GetAngle:Float()
- Return ATan2(y,x)
- End Method
-
- Method Rotate:PVector2D(angle:Float)
- Local c:Float = Cos(angle)
- Local s:Float = Sin(angle)
- Local t:Float = x * c - y * s
- y = y * c + x * s
- x = t
- Return Self
- End Method
-
- Method ToString:String()
- Return x+", "+y
- End Method
-
- Method Draw:Void(canvas:Canvas,x:Float=0,y:Float=0)
- canvas.DrawLine(x,y,x+Self.x,y+Self.y)
- End Method
-
- End Class
- Function Projection:PVector2D(this:PVector2D,into:PVector2D,destination:PVector2D=Null)
-
- If destination = Null destination = New PVector2D
-
- Return destination.Set(into).Normalize().Multiply(destination.DotProduct(this))
-
- End Function
- Function CrossProjection:PVector2D(this:PVector2D,into:PVector2D,destination:PVector2D=Null)
-
- If destination = Null destination = New PVector2D
-
- Return destination.Set(into.y,-into.x).Normalize().Multiply(destination.DotProduct(this))
-
- End Function
|