BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
genemroz
Quartz | Level 8

Esteemed Advisers:

I'm looking for some advice on solving the following problem:

I want to translate a polygon to a new position on 2-dimensional grid. Specifically I want to move the square polygon with corners at (1,1),(1,2),(2,1),(2,2) to a new location with corners at (2,2),(2,3),(3,2),(3,3). k is binary(0 (signifying an empty location),1 (signifying an occupied location)).

I need advice in how to solve this problem in SAS-speak.

I'll try to illustrate with this "text graph":

HAVE:

     3 0 0 0
Ty 2 1 1 0
     1 1 1 0
        1 2 3
          Tx

Want:

     3 0 1 1
Ty 2 0 1 1
     1 0 0 0
        1 2 3
          Tx

Expressed as SAS Datsets:

Set Have;
Input TargetID Tx Ty k;
datalines;
1 1 1 1
2 1 2 1
3 1 3 0
4 2 1 1
5 2 2 1
6 2 3 0
7 3 1 0
8 3 2 0
9 3 3 0
;
run;

Data Want
Input Target ID Tx Ty k
1 1 1 0
2 1 2 0
3 1 3 0
4 2 1 0
5 2 2 1
6 2 3 1
7 3 1 0
8 3 2 1
9 3 3 1

Thanks in advance for any guidance you can provided,

Regards,

Gene

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

OK. I assume that the grid is large enough that the translated vertices are still within the range of the grid. That is, in your example, the translated vertices are within the range [1, 3].

 

The following program copies the entire grid (including TargetID) but sets k=0 for all locations. Thus it is a "blank" grid.  A separate DATA step translates all cells for which k=1. These two data sets are then merged by using the (X,Y) coordinates, which are already sorted by X and Y:

data Have;
Input TargetID Tx Ty k;
datalines;
1 1 1 1
2 1 2 1
3 1 3 0
4 2 1 1
5 2 2 1
6 2 3 0
7 3 1 0
8 3 2 0
9 3 3 0
;

/* assume the original grid is padded so that the translation 
   will not exceed the grid range */
data Blank;
set Have;
k = 0;
run;

data Translate;
set Have;
if k=1;
Tx = Tx + 1;         /* translate all X coords */
Ty = Ty + 1;         /* translate all Y coords */
drop TargetID;
if 1 <= Tx <= 3 &    /* what to do with points outside the range of grid? */
   1 <= Ty <= 3;     /* for now, exclude them */
run;

data Want;
merge Blank Translate;
by Tx Ty;
run;

proc print; run;

View solution in original post

5 REPLIES 5
Rick_SAS
SAS Super FREQ

Some suggestions:

1. Specify the vertices of your polygons in order. So the vertices are (1,1), (1,2), (2,2) and (1,2)

2. To translate a polygon by the vector v=(vx, vy), you add vx to each X coordinate and vy to each Y coordinate. In your example, v=(1,1), so you want to add 1 to all the X coordinates and also add 1 to each Y coordinate.

 

I don't know what k is in your data set, but here is how to define (and plot!) a square in SAS. A separate data set translates the square by the vector v=(1,1)

 

data Have;
Input ID $ Tx Ty;
datalines;
P1 1 1
P1 2 1
P1 2 2
P1 1 2
;

title "Polygon";
proc sgplot data=Have;
polygon x=Tx y=Ty ID=ID / fill;
xaxis min=0 max=3 grid;
yaxis min=0 max=3 grid;
run;

Data Want;
set Have;
ID = "P2";
Tx = Tx + 1;  /* translate X component by 1 unit */
Ty = Ty + 1;  /* translate Y component by 1 unit */
run;

data Both;
set Have Want;
run;

title "Initial and Final Polygons";
proc sgplot data=Both;
polygon x=Tx y=Ty ID=ID / fill GROUP=id;
xaxis min=0 max=3 grid;
yaxis min=0 max=3 grid;
run;

EDIT: I guess you are using k for "occupied" or "unoccupied." Using the scheme, the code is the same. The variable k is not used or changed.

genemroz
Quartz | Level 8

Rick:

Thanks for your suggestions.

Yes, k is a binary variable signifying "occupied" or "unoccupied". It is important in the application of this exercise to the desired result. A polygon is to be translated over a 2D Cartesian grid. Each gridpoint has a TargetID and associated Tx and Ty coordinates. A gridpoint can be either "occupied" (k=1) or "unoccupied" (k=0). The goal is to translate the polygon and identify, by TargetID, which gridpoints are "occupied" (k=1) and which are "unoccupied" (k=0) both before and after the translation.

 

Thanks again for your help,

 

Gene

Rick_SAS
SAS Super FREQ

OK. I assume that the grid is large enough that the translated vertices are still within the range of the grid. That is, in your example, the translated vertices are within the range [1, 3].

 

The following program copies the entire grid (including TargetID) but sets k=0 for all locations. Thus it is a "blank" grid.  A separate DATA step translates all cells for which k=1. These two data sets are then merged by using the (X,Y) coordinates, which are already sorted by X and Y:

data Have;
Input TargetID Tx Ty k;
datalines;
1 1 1 1
2 1 2 1
3 1 3 0
4 2 1 1
5 2 2 1
6 2 3 0
7 3 1 0
8 3 2 0
9 3 3 0
;

/* assume the original grid is padded so that the translation 
   will not exceed the grid range */
data Blank;
set Have;
k = 0;
run;

data Translate;
set Have;
if k=1;
Tx = Tx + 1;         /* translate all X coords */
Ty = Ty + 1;         /* translate all Y coords */
drop TargetID;
if 1 <= Tx <= 3 &    /* what to do with points outside the range of grid? */
   1 <= Ty <= 3;     /* for now, exclude them */
run;

data Want;
merge Blank Translate;
by Tx Ty;
run;

proc print; run;

genemroz
Quartz | Level 8

Rick,

 

Thank you for taking the time to figure this out.  I applied your solution to the larger dataset I'm working with and it works there just fine.  I  was going down a similar path but your approach is simpler than mine.  I'm marking your solution as Accepted.

 

BTW and FWIW, when I started crawling up the SAS learning curve a couple of years ago, I stumbled over multiple topics where your blog helped to show me the way.  Thanks again for your help with this issue.

 

Regards,

 

Gene

Rick_SAS
SAS Super FREQ

Great. Glad it worked.

 

Thanks for the kind feedback on my blog. It is always nice to get a Thank You from someone who was helped. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1233 views
  • 4 likes
  • 2 in conversation