Vector (2D) manipulations

Started by _PJ_, March 08, 2020, 14:07:19

Previous topic - Next topic

_PJ_

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



_PJ_

As a brief example, the Function "UpdateVector" here seems unnecessarily complex when all that's required would be a modification to the existing components...

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

Brucey

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.

ImJustAnotherCoder

#3
Brucey, I come from a c++ background and have a couple of queries:

QuoteStructs (like SVec2D) are not managed by the garbage collector

If structs are created on the stack what does this line of code do?
Vector = Null

and this one
Vector = New SVec2D(dX,dY)

_PJ_

Quote from: Brucey on March 10, 2020, 06:20:59
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?

Brucey

Structs live on the stack. They are passed by value, and cease to exist when they go out of scope.

Brucey

Quote
If structs are created on the stack what does this line of code do?
Vector = Null
Vector will contain 0 value fields.

Quote
and this one
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.

_PJ_

Thanks very much for your answers, Brucey. Very clear and understood.

ImJustAnotherCoder

QuoteVector will contain 0 value field
Got ya.

Quote"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.
I see, and it's still used to call constructors and has nothing to do with memory allocation for Structs. That's cool.

Thank you for the explanation.

_PJ_

New questions but very related to the earlier Vector syntax queries:::

According to the documentation:

Method Operator[]:Double(index:Int)
Description Retrieves the x or y component using [ 0 ] or [ 1 ] respectively.


((( EDIT::  I added some spaces above due to the hypertext and formatting )))
____________

This to me implies that this should work:


Local MyVectorInstance:SVec2d = New sVec2D(1.0,-1.0)
Local X:Double = MyVectorInstance[0]
Local Y:Double = MyVectorInstance[1]

However, this results in 'SVec2d cannot be indexed' error

This is even more problematic, when considering arrays of vector objects:

Local MyArray:SVec2D[] = New SVec2D[10]
MyArray[0] = New SVec2D(1.0,-1.0)

Local X:Double = MyArray[0][0]
Local Y:Double = MyArray[0][1]


or just in case, expanded as


Local MyArray:SVec2D[] = New SVec2D[10]
Local AddVector:SVec2D = New SVec2D(1.0,-1.0)
MyArray[0] = AddVector

Local MyVectorInstance:SVec2D = MyArray[0] ' Yes, this would be identical to AddVector, but explicit declaration here to show it is retrieved from array
Local X:Double = 'MyVectorInstance[0]
Local Y:Double = MyVectorInstance[1]


What is correct syntax to retrieve individual Vector components and particularly when that vector is an array element?

TomToad

Appears to be a bug to me.  It works with types but seems to fail with structs.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Derron

Raised issue for it: https://github.com/bmx-ng/bcc/issues/543

But I cannot say when this gets fixed or not - judging on the kind of absence of Brucey these days.


bye
Ron

Derron

https://github.com/bmx-ng/bcc/commit/52cfdb4cf5f82ae3ea6391adbbdb140f765e7427
Should fix the issue.

So you need to compile a fresh bcc
https://github.com/bmx-ng/bcc

Compile it and replace your current bcc with the new one.


bye
Ron

_PJ_

Quote from: Derron on February 08, 2021, 18:10:21
https://github.com/bmx-ng/bcc/commit/52cfdb4cf5f82ae3ea6391adbbdb140f765e7427
Should fix the issue.

So you need to compile a fresh bcc
https://github.com/bmx-ng/bcc

Compile it and replace your current bcc with the new one.
Hello.

I downloaded the bcc soruce and placed into the src directory of BlitzMax.
From here I opened my existing BlitzMax-NG IDE and seldected BUILD with options "Verbose Build"
This appeared to succeed without error
I placed the generated BCC.exe into the BIN directory of my BlitzMax installation directory...

Unfortunately, I am having numerous issues with compiling anything. In attmepting to identify the cause, at the very least, the following will result in "EXCEPTION_ACCESS_VIOLATION" with no further identifiers as to where the root of the issue lies.


SuperStrict

Local a:String = StripSlash("C:\Windows\")


Derron

#14
but executing "bcc.exe" from command line results in the line

bcc[ng] Release Version 0.129


Or does that segfault already too?

Asking as it compiled fine here. And your sample code then - after replacing - compiles and executes fine too

Edit: attached my build of bcc.exe (tested on a win7 lite VM)

bye
Ron