'Algorithm for drawing profiles using long thin rectangles

I want to draw profiles similar to the ones at https://steeldoor.org like:

img

programmatically. (Probably in FreeSCAD, but possibly Python).

I want to define a global thickness and then specify the profiles with a vector of directions and another one of lengths (specifically the lengths of the outer edges). The vectors for the second drawing in the example might be ['N', 'E', 'S', 'W', 'S', 'W', 'N'] and [10,100,80,20,70,80,10].

Given a primitive for drawing a rectangle starting at (x,y) with length l and width (corresponding to line thickness) t, say, rect(x,y,l,t), which would draw a horizontal rectangle to the right with l and t positive, is there a generic algorithm that would take the two vectors as input and draw the profile using the primitive? I can solve for a specific profile, but not for the general case.



Solution 1:[1]

Perhaps you need something like this approach.
I assume that Y axis is up, and base line (current coordinate x,y) goes through middle of thick lines (E-F-K at the picture), rectangle is defined by left-down corner (points A and H), width and height.

enter image description here

dirs = ['N', 'E', 'S', 'W', 'S', 'W', 'N']
lens = [10,100,80,20,70,80,10]
half_thickness = 3
x = 0
y = 0
for i in range(dirs):
    if dirs == 'N':
        rect(x-half_thickness, y, 2*half_thickness, lens[i])
        y += lens[i]
    elif dirs == 'S':
        rect(x-half_thickness, y - lens[i], 2*half_thickness, lens[i])
        y -= lens[i]
    elif dirs == 'E':
        rect(x, y-half_thickness, lens[i], 2*half_thickness)
        x += lens[i]
    else:
        rect(x - lens[i], y-half_thickness, lens[i], 2*half_thickness)
        x -= lens[i]

Solution 2:[2]

I ended up using the answer above (fixing a mistake in the else clause). The Python code below generates the OpenSCAD file that in turn produces the desired profile.

def rect(x,y,w,h,hh):
    print("translate([", x,",",y,"])")
    print("\tlinear_extrude(height=",hh,")")
    print("\t\tsquare([", w,",",h,"]);")


dirs = ['W', 'S', 'E', 'S', 'E', 'N', 'E', 'N', 'E', 'N', 'W']
lens = [14,68,14,6,90,13,35,7,13,68,14]
thickness = 4
height=20
ht = thickness/2;
x = 0
y = 0
li=0;
for dir in dirs:
    len = lens[li]
    li += 1
    if dir == 'N':
        rect(x-ht, y, 2*ht, len, height)
        y += len
    elif dir == 'S':
        rect(x-ht, y - len, 2*ht, len, height)
        y -= len
    elif dir == 'E':
        rect(x, y-ht, len, 2*ht, height)
        x += len
    else:
        rect(x - len, y-ht, len, 2*ht, height)
        x -= len

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Quentin V