OS2 World.Com Forum
2010.09.07, 09:10:25 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Finding the border  (Read 566 times)
jep
Global Moderator
Sr. Member
*****
Posts: 331


View Profile
« on: 2010.01.14, 16:45:56 »

Marked as:Home work
(knowledge about Algorithms help)
Hi,

this time I need your help to solve a little problem I've stumbled upon.

I've got an image (represented here by ascii art) made of different characters. I've written some code to find the points (coordinates) that can be used to create borders around different colored areas of an image.

Each area/color is represented by a character and should be treated as an "island".

Question for you to help me with: How do I find out how to connect each point to a border for every "island"?

The code doesn't create ▓ for every point, only "essential corners" as the code scan the image horizontally.
I wouldn't mind if you can eliminate redundant points ▓ that appear vertically, but note that a border may contain shapes that need both an entry and an exit point* (see image further down).

Prerequisite:
  • Image what contain differently colored areas, but with some "islands" in the same color
  • copy the example below or convert some image to ascii art (80 pixels wide) and save it on your computer as borders.ascii

You'll have to replace the character that represent the variable "blank" with the character that contain/represent the areas in the ascii image to scan. You should also replace the variable "border" with some character that isn't present within the image elsewhere.

Code:
/* Code to find areas and create a border */

call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
call SysLoadFuncs

f_name = 'borders.ascii'
blank = '+'
border = '▓'
row = 0
counter = 0
DO WHILE LINES( f_name ) > 0
    row = row + 1
    data.row = LINEIN( f_name )
END
cls
data.0 = row
d_len = LENGTH( data.row )
fillen = LENGTH( data.0 )
filler = COPIES( '0', fillen )
DO row = 1 TO data.0
    chr = '-'
    DO col = 1 TO d_len + 1
        curr = SUBSTR( data.row, col, 1 )
        IF curr <> chr THEN
        DO
            IF curr = blank | chr = blank THEN
            DO
                edge = col - ( chr = blank )
                counter = counter + 1
                stem.counter = RIGHT( filler||row, fillen )||':'||RIGHT( filler||edge, fillen )||':'||( chr = blank )
                DO adjacent = row - ( row > 1 ) TO row + ( 1 < data.0 ) BY 1 + ( 1 < data.0 )
                    IF SUBSTR( data.adjacent, edge, 1 ) = blank THEN
                    DO
                        counter = counter + 1
                        stem.counter = RIGHT( filler||adjacent, fillen )||':'||RIGHT( filler||edge, fillen )||':'||( chr = blank )
                    END
                END
            END
        END
        chr = curr
    END
END
stem.0 = counter
DO counter = 1 TO stem.0
    PARSE VALUE stem.counter WITH row':'col':'
    row = FORMAT( row )
    col = FORMAT( col )
    data.row = OVERLAY( border, data.row, col )
END
DO row = 1 TO data.0
    SAY data.row
END    
IF SysStemSort( 'stem.' ) = 0 THEN
DO row = 1 TO stem.0
    SAY stem.row
END

How do I connect the pointes represented by ▓ for each closed area?
The result will look similar to:

▓+++▓▓█▓▓+++▓██████▓+++▓
▓++▓▓█▓█▓▓++▓█▓▓+▓█▓+++▓
▓+▓▓█▓▓▓█▓▓+▓▓█▓+▓█▓+++▓
▓▓▓█▓▓+▓▓█▓+▓▓█▓+▓█▓+++▓
▓▓█▓▓+▓▓█▓▓▓▓█▓▓+▓█▓███▓
▓█▓▓+▓▓█▓▓+▓█▓▓++▓█▓█▓█▓
█▓▓++▓█▓████▓▓+++▓█▓█▓█▓
▓▓+++▓█▓▓+▓▓█▓+++▓▓█▓▓█▓
▓++++▓▓█▓▓▓█▓▓++++▓+▓▓█▓
▓+++++▓█▓▓█▓▓++++++++▓+▓
▓+++++▓▓██▓▓+++++++++++▓
▓++++++▓++▓++++++++++++▓

from
++++++█++++++██████+++++
+++++█+█+++++█++++█+++++
++++█+++█+++++█+++█+++++
+++█+++++█++++█+++█+++++
++█+++++█++++█++++█+███+
+█+++++█++++█+++++█+█+█+
█+++++█+████++++++█+█+█+
++++++█+++++█++++++█++█+
+++++++█+++█++++++++++█+
+++++++█++█+++++++++++++
++++++++██++++++++++++++
++++++++++++++++++++++++


*Example of Entry->Exit point:

▓+▓█▓▓+▓
▓+▓▓█▓+▓
▓▓▓█▓▓+▓
▓▓█▓▓++▓
▓▓+▓+++▓

I need one or more stems that contain coordinates grouped and ordered so that they form closed shapes similar to the example below. The provided code create all the "gray squares", while your code has to group and connect them. Opposite direction is also ok.


//Jan-Erik
« Last Edit: 2010.01.14, 16:47:28 by jep » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!