Android Question Matrix to Vector

wonder

Expert
Licensed User
Longtime User
Hi guys!

I want to create an algorithm which converts a two-dimensional binary matrix into vectors.

Input A:
B4X:
0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 0
0 1 0 0 0 0 1 0
0 1 0 0 0 0 1 0
0 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0

Input B:
B4X:
0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0

Output for either A or B:
B4X:
Vec1(6,   0) '6 Right
Vec2(4,  90) '4 Down
Vec3(6, 180) '6 Left
Vec3(4, 270) '4 Up

Any idea on how to do this?
 

JordiCP

Expert
Licensed User
Longtime User
Not sure if I understood correctly. Are these vectors the path of consecutive '1's in a given direction, given a starting point?

The start point for one vector is the end point of the former.
Not sure if the direction is always clockwise or just follows the path ,depending on the matrix

if so, something similar to this (not tested)

B4X:
Sub FindVector( M as matrix, StartingPoint as Pxy, dir as int )

  Dim CurrentPoint as Pxy
  CurrentPoint.initialize(Pxy.x,Pxy.y)
  Dim myVector as Vector
  myVector.initialize(0,dir)

  'pseudocode
  if dir=0 then
    incx=1:incy=0
  else if dir=90 then
    incx=0:incy=1
  else if dir=180 then
    incx=-1:incy=0
  else if dir=270 then
    incx=0:incy=-1
  endif

  while Matrix(CurrentPoint.x,CurrentPoint.y)>0
    myvec.len=myvec.len+1
    CurrentPoint.x = CurrentPoint.x + incx
    CurrentPoint.y = CurrentPoint.y + incy
  Loop

  return myvec

end sub

Sub FindAllVectors

  Dim myPoint as Pxy
  myPoint.initialize(1,1)
 
  Dim myVec1 as Vector = FindVector( myMatrix, myPoint, 0)
  myPoint.x = myPoint.x + (myVec1.len -1) 'or exit if len==0

  Dim myVec2 as Vector = FindVector( myMatrix, myPoint, 90)
  myPoint.y = myPoint.y + (myVec2.len-1)'or exit if len==0

  Dim myVec3 as Vector = FindVector( myMatrix, myPoint, 180)
  myPoint.x = myPoint.x - (myVec3.len-1)'or exit if len==0

  Dim myVec4 as Vector = FindVector( myMatrix, myPoint, 270)

end Sub
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Hi Jordi,

Thank you for your reply. What I'm trying to achieve is a rudimentary "image tracer".
The poor-man's version of this: http://vectormagic.com/home

I'll give your code a test-drive once I get home. :)
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Last edited:
Upvote 0
Top