March 05, 2021, 07:29:08 PM

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

Offline _PJ_

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_

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

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

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_

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

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

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_

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.

Offline ImJustAnotherCoder

Re: Vector (2D) manipulations
« Reply #8 on: April 02, 2020, 10:48:44 AM »
Quote
Vector 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.

Offline _PJ_

Re: Vector (2D) manipulations
« Reply #9 on: January 30, 2021, 07:05:44 PM »
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:

Code: [Select]
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:
Code: [Select]
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

Code: [Select]
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?

Offline TomToad

Re: Vector (2D) manipulations
« Reply #10 on: January 30, 2021, 07:39:52 PM »
Appears to be a bug to me.  It works with types but seems to fail with structs.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Derron

Re: Vector (2D) manipulations
« Reply #11 on: January 30, 2021, 10:08:22 PM »
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

Offline Derron

Re: Vector (2D) manipulations
« Reply #12 on: February 08, 2021, 06:10:21 PM »
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

Offline _PJ_

Re: Vector (2D) manipulations
« Reply #13 on: February 14, 2021, 12:48:01 AM »
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.

Code: [Select]
SuperStrict

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

Offline Derron

Re: Vector (2D) manipulations
« Reply #14 on: February 14, 2021, 06:38:41 AM »
but executing "bcc.exe" from command line results in the line
Code: [Select]
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

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal