#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ***************************************************************************
# *   Copyright (C) 2013, Paul Lutus                                        *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU General Public License as published by  *
# *   the Free Software Foundation; either version 2 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU General Public License for more details.                          *
# *                                                                         *
# *   You should have received a copy of the GNU General Public License     *
# *   along with this program; if not, write to the                         *
# *   Free Software Foundation, Inc.,                                       *
# *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
# ***************************************************************************

# version 1.1 (01.28.2013)

import re,sys,os

import matplotlib.pyplot as plt

from math import *

# steps in large and small radius profiles

# (more steps, more accuracy, but slower computation)

st = 50

# wall thickness
w = 16
# cylinder section height
ch = 2500 + 2*60
# tank diameter (internal)
d = 2400 - w*2
# tank radius
r = d/2.0
# r_a = small radius
r_a = d/6.5
# r_b = large radius
r_b = 0.8*d
# right triangle hypotenuse
hyp = r_b-r_a
# right triangle opposite side
opp = r-r_a
# right triangle adjacent side
adj = sqrt(hyp**2-opp**2)
# hyp-adj angle
ang = atan(opp/adj)

p = []

# compute large radius data points

for n in range(st):
  a = ang * n / st
  x = r_b * cos(a) - adj
  y = r_b * sin(a)
  p.append([x,y])

# compute small radius data points

for n in range(st+1):
  a = (ang-pi/2) * (st-n) / st
  x = -sin(a) * r_a
  y = (r - r_a) + cos(a) * r_a
  p.append([x,y])

# assemble points for complete tank profile
  
tp = []
for q in p:
    tp.append([(r_b-adj)-q[0],q[1]])
tp.append([(r_b-adj)+ch,r])
for q in p[::-1]:
    tp.append([(r_b-adj)+ch+q[0],q[1]])
    
# format profile table and save it

s = ''
    
for q in tp:
  s += '%f,%f\n' % (q[0],q[1])
  
with open('profile.txt','w') as f:
  f.write(s)
  
# draw graphic image

cp = []

for q in tp:
  cp.append([q[0],q[1]])
  
for q in tp[::-1]:
  cp.append([q[0],-q[1]])
  
# separate x and y coordinates
  
px = [q[1] for q in cp]
py = [q[0] for q in cp]

# set 1:1 aspect ratio

plt.figure().add_subplot(111).set_aspect(1)

plt.plot(px,py,'-',color='blue')
dy = r_b-adj
plt.plot([-r,r],[dy,dy],'-.',color='red')
dy += ch
plt.plot([-r,r],[dy,dy],'-.',color='red')
plt.plot([0,0],[0,dy+r_b-adj],'-.',color='green')
plt.grid(True)
plt.savefig('tank.png')
plt.show()