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
#!/usr/bin/env python

import cairo as c
import numpy as n

class Map:

    def __init__(self,width=600,height=400,max_mass=1e3):
        self.width = width
        self.height = height
        self.frame_count = 0
        self.max_mass = max_mass
        self.surface = c.ImageSurface (c.FORMAT_ARGB32, self.width, self.height)
        self.ctx = c.Context (self.surface)
        self.ctx.set_source_rgb(0,0,0)
        self.ctx.rectangle(0,0,self.width,self.height)
        self.ctx.fill()
        self.ctx.translate(.5*self.width,.5*self.height)
        self.ctx.set_source_rgba(1,1,1,.5)
        self.ctx.rectangle(-2,-2,2,2)
        self.ctx.fill()
        

    def draw_body(self,body):
        self.ctx.save()
        self.ctx.translate(body.coord[0],body.coord[1])
        self.ctx.set_source_rgb(*body.color)
        radius = 10*n.sqrt(body.mass/self.max_mass)
        if radius < 1.5:
            radius = 1.5
        self.ctx.arc(0,0,radius,0,2*n.pi)
        self.ctx.fill()
        self.ctx.restore()

    def draw_map(self):
        self.surface.write_to_png('/tmp/frame_%06d.png' % self.frame_count)
        self.frame_count += 1
        self.ctx.save()
        self.ctx.translate(-.5*self.width,-.5*self.height)
        self.ctx.set_source_rgba(0,0,0,.08)
        self.ctx.rectangle(0,0,self.width,self.height)
        self.ctx.fill()
        self.ctx.restore()