Skip to content
Snippets Groups Projects
Select Git revision
  • 33d8ccdf438469968c4b982da3c7f98c3049ad13
  • master default protected
  • simonlyfix
  • simonly
  • julia1
  • julia1mattias
6 results

tankgraphic.jl

Blame
  • Jacob Wikmark's avatar
    Jacob Wikmark authored
    bf1d3931
    History
    tankgraphic.jl 4.11 KiB
    
    const lightblue = (150, 150, 255)
    const wall_thickness = 5
    const mouth_width = 20
    const animationspeed = 1500
    
    inspectdr(show=false)
    
    const tankattr = Dict("y" => 0,
                          "x" => 0,
                          "height" => 800,
                          "width" => 300)
    
    function stream(width, height, x, y)
        bigstyle = Dict(:fill => "rgb$s_blue")
        smallstyle = Dict(:fill => "rgb$lightblue")
        lines = 10
        sp = "$(height/animationspeed)s"
        thickness = 10
        speed = 10
        ht = height - thickness
        attr_small = Dict("width"=> "$(width)", "height" => "$(thickness)", "x" => "$(x)", "y"=>"$(y)")
    
        #we may want to rethink this a little bit
        animate1 = Dict("attributeType" => "XML", "attributeName" => "y", "from" => "$(y)", 
                        "to" => "$(y+ht/2)", "dur" => sp, "repeatCount" => "indefinite")
        animate2 = Dict("attributeType" => "XML", "attributeName" => "y", "from" => "$(y+ht/2)", 
                       "to" => "$(y+ht)", "dur" => sp, "repeatCount" => "indefinite")
        anim1 = dom"svg:animate"(attributes=animate1)
        anim2 = dom"svg:animate"(attributes=animate2)
        stline1 = Node(instanceof(dom"svg:rect"()), anim1, style = smallstyle, attributes = attr_small)
        stline2 = Node(instanceof(dom"svg:rect"()), anim2, style = smallstyle, attributes = attr_small)
        (svg_rect(width, height, x, y, bigstyle), stline1, stline2)
    end
    
    
    function water_container(width, height, fill, x=0, y=0)
        #Produces an SVG water container of size height, width, filled from the
        #bottom to the height fill (as a proportion of height
        fill_level = fill*height
        water_style = Dict(:fill => "rgb$s_blue")
        wall_style  = Dict(:fill => "rgb$s_black")
        container_attr =  Dict("x" => "$x",
                               "y" => "$y",
                               "width" => "$width",
                              "height" => "$height")
        Node(svgsvg_symb, 
             svg_rect(width, fill_level, 0, height-fill_level, water_style),
             svg_rect(wall_thickness, height, 0, 0, wall_style),
             svg_rect(wall_thickness, height, width-wall_thickness, 0, wall_style),
             svg_rect((width-mouth_width)/2, wall_thickness, 0, height-wall_thickness, wall_style),
             svg_rect((width-mouth_width)/2, wall_thickness, (width+mouth_width)/2, height-wall_thickness, wall_style),
             attributes = container_attr)
    end
    
    
    function tank_construct(upper, lower, pump_speed = 1, tnum = 0, r = -1)
        height = 250
        width  = 200
    
        arrowwidth = 0
        arrowheight= 20
        upper_x = 75
        upper_y = 100
        lower_x = 75
        lower_y = upper_y + height+50
    
        tank_x      = 0
        tank_y      = upper_y + height*2 + 100
        tank_width  = 2*lower_x+width
        tank_height = 50
        pipe_thickness = 25
        pd = (pipe_thickness-mouth_width)/2
        pt = pipe_thickness/2
    
        if tnum != 0
            arrowwidth = 20
        end
        redarrow = svg_polygon([(upper_x+width, 50+tnum*(height+50)-r*height), 
                                (upper_x+width+arrowwidth, 50+tnum*(height+50)-r*height-arrowheight),
                                (upper_x+width+arrowwidth, 50+tnum*(height+50)-r*height+arrowheight)], s_red)
        pipe = svg_polyline([(pipe_thickness/2, height*2+250), (pt,pt), (tank_width/2, pt), (tank_width/2, 50-pt)],
                            pipe_thickness, s_black)
        funnel = svg_polygon([(tank_width/2, 2*pt), (tank_width/2-pipe_thickness*2, 4*pt), 
                              (tank_width/2+pipe_thickness*2, 4*pt)], s_black)
    
    
        w1 = mouth_width*sqrt(max(0,pump_speed))
        w2 = mouth_width*sqrt(max(0, upper))
        w3 = mouth_width*sqrt(max(0, lower))
    
        container1 = water_container(width, height, upper, upper_x, upper_y)
        container2 = water_container(width, height, lower, lower_x, lower_y)
    
    
        #The order of svg objects is important, latter elements are laid on top of existing ones
        Node(svgsvg_symb, 
             stream(w1, height+50, upper_x+(width-w1)/2, 50)...,
        stream(w2, height+50, upper_x+(width-w2)/2, height+100)...,
        stream(w3, 100, upper_x+(width-w3)/2, 2*height+150)...,
        container1, container2, redarrow, 
        svg_rect(tank_width, tank_height, tank_x, tank_y),
        pipe,
        funnel,
        id = "tank",
        attributes = tankattr)
    end