Home > Net >  Algorithm for drawing profiles using long thin rectangles
Algorithm for drawing profiles using long thin rectangles

Time:04-30

I want to draw profiles similar to the ones at 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, x, 2*half_thickness)
        x -= lens[i]

CodePudding user response:

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
  • Related