Android Question Matrix to Vector

Discussion in 'Android Questions' started by wonder, May 28, 2015.

  1. wonder

    wonder Expert Licensed User

    Hi guys!

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

    Input A:
    Code:
    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:
    Code:
    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:
    Code:
    Vec1(6,   0'6 Right
    Vec2(4,  90'4 Down
    Vec3(6180'6 Left
    Vec3(4270'4 Up
    Any idea on how to do this?
     
    JordiCP likes this.
  2. JordiCP

    JordiCP Well-Known Member Licensed 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)

    Code:
    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
     
    wonder likes this.
  3. wonder

    wonder Expert Licensed 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. :)
     
    Peter Simpson likes this.
  4. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Last edited: May 31, 2015
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice