April 02, 2020, 10:29:03 AM

Author Topic: Vector (2D) manipulations  (Read 253 times)

Offline _PJ_

  • Jr. Member
  • **
  • Posts: 53
Vector (2D) manipulations
« on: March 08, 2020, 02:07:19 PM »
For geometric functions, the documentation (for SVec2D for example) suggests there is no Cross Product, Scalar product, Component summation or direct component declaration (outside of new vector creation)

Therefore, it seems the only way to achieve this is to generate custom Methods which generate new vectors as return type ?
For a simple change to a vector property, this seems quite an overkill...

Is there some way to perform the following, for example:

Vector:SVec2D[n] = x:Double



Offline _PJ_

  • Jr. Member
  • **
  • Posts: 53
Re: Vector (2D) manipulations
« Reply #1 on: March 08, 2020, 02:18:57 PM »
As a brief example, the Function "UpdateVector" here seems unnecessarily complex when all that's required would be a modification to the existing components...

Code: [Select]
SuperStrict

Const oX:Int = 512
Const oY:Int = 384

Global Vector:SVec2D

Global ChosenVector:Byte=False

RunTime()

Function GetVector()
While (Not(ChosenVector))
draw
If (WaitMouse()=1)
Vector = New SVec2D(MouseX()-oX, MouseY()-oY)
DebugLog(Vector[0]+","+Vector[1])
ChosenVector=1
End If
Wend
End Function

Function RunTime()
Graphics(oX * 2, oY * 2)

FlushMouse()

SetOrigin oX,oY

GetVector

While Not (KeyHit(KEY_ESCAPE))
updatevector
draw
Wend

EndGraphics
End
End Function

Function Draw()
Cls
DrawVector
DrawGrid
Flip
End Function

Function DrawVector()
SetColor(255,0,0)
DrawLine 0,0,Vector[0],Vector[1]
End Function

Function UpdateVector()
Local dX:Double = Sin(270 - (MilliSecs() / 1000)) * Vector.Length()
Local dY:Double = Cos(270 - (MilliSecs() / 1000)) * Vector.Length()

Vector = Null
GCCollect

Vector = New SVec2D(dX,dY)

DebugLog(Vector[0]+","+Vector[1])

End Function

Function DrawGrid()
SetColor(192,192,192)
DrawLine -oX,0,ox,0
DrawLine 0,-oY,0,oY
End Function

Offline Brucey

  • Full Member
  • ***
  • Posts: 118
Re: Vector (2D) manipulations
« Reply #2 on: March 10, 2020, 06:20:59 AM »
Structs (like SVec2D) are not managed by the garbage collector, and so are fairly cheap to create. They are also passed by value, unless you specify "Var" in the argument declaration.

I am not sure a 2D cross product would be of much value. There is one for 3D though.

Offline ImJustAnotherCoder

  • Jr. Member
  • **
  • Posts: 13
Re: Vector (2D) manipulations
« Reply #3 on: March 11, 2020, 02:22:40 AM »
Brucey, I come from a c++ background and have a couple of queries:

Quote
Structs (like SVec2D) are not managed by the garbage collector

If structs are created on the stack what does this line of code do?
Code: [Select]
Vector = Null
and this one
Code: [Select]
Vector = New SVec2D(dX,dY)

Offline _PJ_

  • Jr. Member
  • **
  • Posts: 53
Re: Vector (2D) manipulations
« Reply #4 on: March 31, 2020, 07:08:38 PM »
Structs (like SVec2D) are not managed by the garbage collector, and so are fairly cheap to create.
How is their memory use managed then? Do they return once out of scope?

Offline Brucey

  • Full Member
  • ***
  • Posts: 118
Re: Vector (2D) manipulations
« Reply #5 on: April 01, 2020, 02:45:43 PM »
Structs live on the stack. They are passed by value, and cease to exist when they go out of scope.

Offline Brucey

  • Full Member
  • ***
  • Posts: 118
Re: Vector (2D) manipulations
« Reply #6 on: April 01, 2020, 02:49:06 PM »
Quote
If structs are created on the stack what does this line of code do?
Code: [Select]
Vector = Null
Vector will contain 0 value fields.

Quote
and this one
Code: [Select]
Vector = New SVec2D(dX,dY)
Vector will be set to dX, dY.

"New" in this regard does not actually create a new instance of something - but it keeps the language for writing Types and Structs the same, even though they are two different kinds of storage.

Offline _PJ_

  • Jr. Member
  • **
  • Posts: 53
Re: Vector (2D) manipulations
« Reply #7 on: April 01, 2020, 07:40:03 PM »
Thanks very much for your answers, Brucey. Very clear and understood.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal