summaryrefslogtreecommitdiffstats
path: root/cookie_generator.py
blob: 87fbd5404fe5efa0e4842d0bda3e663d391f039e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#print "Hello"
from __future__ import division
from math import sqrt
from math import atan


def distance(pos1, pos2):
    return sqrt(sum([ (x[0] -x[1])**2 for x in zip(pos1, pos2)]))
    
def compute_lengths(positions):
    lengths = []
    for i in range(len(positions)):
        if (i < len(positions) -1):
            lengths.append(distance(positions[i], positions[i+1]))
        else:
            lengths.append(distance(positions[i], positions[0]))
            
    return lengths

def direction(pos1, pos2):
    if ((pos1[1] - pos2[1]) != 0):
        if (atan((pos1[0]-pos2[0])/(pos1[1] - pos2[1]))!= 0):
            return atan((pos1[0]-pos2[0])/(pos1[1] - pos2[1]))*180/3.1415
    
    
def compute_angles(positions):
    return [direction(x, positions[(i+1)% len(positions)]) for i, x in enumerate(positions)]


def generate_scad_script(positions, angles, lengths):
    #positions[len(positions) -1] = positions[0]
    text = ""
    for element in zip(positions, angles, lengths):
        text += "translate(" + str(element[0])  + ") "
        text += "rotate([0, 0," + str(element[1]) +"]) "
        text += "cube([" + str(element[2]) + ", 2, 6]);\n"
        
    return text
        

def main():
    sample_positions = [
        [0, 3, 0], 
        [5, 5, 0], 
        [-8, 6, 0]
        ]
    lengths = compute_lengths(sample_positions)
    
    angles = compute_angles(sample_positions)
    print lengths
    print angles
    print distance([1, 0, 3], [2, 5, 5])
    print direction([1, 0, 3], [2, 5, 5])
    
    print generate_scad_script(sample_positions, angles, lengths )
main()