Python道場:インテグレータの登場です。
ジワーッと変化させます。まずはClass IntegratorsのPython化からトライします。
Class Integrators
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 |
class Integrator(): def __init__(self, value): self.DAMPING = 0.5 self.ATTRACTION = 0.2 self.value=value self.vel=0 self.accel=0 self.force=0 self.mass = 1 self.damping = self.DAMPING self.attraction = self.ATTRACTION self.targeting=0 self.target_value=0 def Integrator():{} def Integrator(self, value): value = value def Integrator(self, value, damping, attraction): self.value = value self.damping = damping self.attraction = attraction def set(self, v): self.value = v def update(self): if (self.targeting): self.force = self.force + self.attraction * (self.target_value - self.value) self.accel = self.force / self.mass self.vel = (self.vel + self.accel) * self.damping self.value += self.vel self.force = 0 def target(self, t): self.targeting = True self.target_value = t def noTarget(self): self.targeting = False |
step14_integrators_py.pyde
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import Table import Integrator dataMin = -10 dataMax = 10 interpolators = [] def setup(): global locationTable global nameTable global mapImage global rowCount global dataTable global dataMin global dataMax global closestDist global closestText global closestText2 global closestTextX global closestTextY global interpolators size(640, 400) mapImage = loadImage("map.png") locationTable = Table.Table("locations.tsv") nameTable = Table.Table("names.tsv") rowCount = locationTable.getRowCount() dataTable = Table.Table("random.tsv") for row in range(rowCount): initialValue = dataTable.getFloat(row, 1) #print initialValue integ = Integrator.Integrator(initialValue) interpolators.append(integ) #print interpolators font = loadFont("Univers-Bold-12.vlw") textFont(font) smooth() noStroke() def draw(): global locationTable global nameTable global mapImage global rowCount global dataTable global dataMin global dataMax global closestDist global closestText global closestText2 global closestTextX global closestTextY global interpolators background(255) #print(rowCount) image(mapImage, 0, 0) for row in range(rowCount): interpolators[row].update() closestDist = width*height #fill(192, 0, 0) #noStroke() for row in range(0,rowCount): abbrev = dataTable.getRowName(row) x = locationTable.getFloat2(abbrev, 1) y = locationTable.getFloat2(abbrev, 2) drawData(x, y, abbrev) if (closestDist != width*height): fill(0) textAlign(CENTER) text(closestText, closestTextX, closestTextY-15) text(closestText2, closestTextX, closestTextY) # Map the size of the ellipse to the data value def drawData(x, y, abbrev): global dataTable global dataMin global dataMax global closestDist global closestText global closestText2 global closestTextX global closestTextY global interpolators #Get data value for state row = int(dataTable.getRowIndex(abbrev)) print row value = interpolators[row].value radius = 0 if value >= 0: radius = map(value, 0, dataMax, 1.5, 15) fill('#333366') #blue else: radius = map(value, 0, dataMin, 1.5, 15) fill('#ec5166') #red ellipseMode(RADIUS) ellipse(x, y, radius, radius) d = dist(x, y, mouseX, mouseY) # Because the following check is done each time a new # circle is drawn, we end up with the values of the # circle closest to the mouse. if ((d < radius + 2) and (d < closestDist)): closestDist = d name = nameTable.getString2(abbrev, 1) val = nfp(interpolators[row].target_value, 0, 2) closestText = name closestText2 = val closestTextX = x closestTextY = y-radius-4 def keyPressed(): if (key == ' '): updateTable() def updateTable(): global rowCount global dataMin global dataMax global interpolators for row in range(rowCount): newValue = random(dataMin, dataMax) print newValue print interpolators[row] interpolators[row].target(newValue) |
step15_framerate_py
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import Table import Integrator dataMin = -10 dataMax = 10 interpolators = [] def setup(): global locationTable global nameTable global mapImage global rowCount global dataTable global dataMin global dataMax global closestDist global closestText global closestText2 global closestTextX global closestTextY global interpolators size(640, 400) mapImage = loadImage("map.png") locationTable = Table.Table("locations.tsv") nameTable = Table.Table("names.tsv") rowCount = locationTable.getRowCount() dataTable = Table.Table("random.tsv") for row in range(rowCount): initialValue = dataTable.getFloat(row, 1) #print initialValue integ = Integrator.Integrator(initialValue) interpolators.append(integ) #print interpolators font = loadFont("Univers-Bold-12.vlw") textFont(font) smooth() noStroke() frameRate(30) def draw(): global locationTable global nameTable global mapImage global rowCount global dataTable global dataMin global dataMax global closestDist global closestText global closestText2 global closestTextX global closestTextY global interpolators background(255) #print(rowCount) image(mapImage, 0, 0) for row in range(rowCount): interpolators[row].update() closestDist = width*height #fill(192, 0, 0) #noStroke() for row in range(0,rowCount): abbrev = dataTable.getRowName(row) x = locationTable.getFloat2(abbrev, 1) y = locationTable.getFloat2(abbrev, 2) drawData(x, y, abbrev) if (closestDist != width*height): fill(0) textAlign(CENTER) text(closestText, closestTextX, closestTextY-15) text(closestText2, closestTextX, closestTextY) # Map the size of the ellipse to the data value def drawData(x, y, abbrev): global dataTable global dataMin global dataMax global closestDist global closestText global closestText2 global closestTextX global closestTextY global interpolators #Get data value for state row = int(dataTable.getRowIndex(abbrev)) print row value = interpolators[row].value radius = 0 if value >= 0: radius = map(value, 0, dataMax, 1.5, 15) fill('#333366') #blue else: radius = map(value, 0, dataMin, 1.5, 15) fill('#ec5166') #red ellipseMode(RADIUS) ellipse(x, y, radius, radius) d = dist(x, y, mouseX, mouseY) # Because the following check is done each time a new # circle is drawn, we end up with the values of the # circle closest to the mouse. if ((d < radius + 2) and (d < closestDist)): closestDist = d name = nameTable.getString2(abbrev, 1) val = nfp(interpolators[row].target_value, 0, 2) closestText = name closestText2 = val closestTextX = x closestTextY = y-radius-4 def keyPressed(): if (key == ' '): updateTable() def updateTable(): global rowCount global dataMin global dataMax global interpolators for row in range(rowCount): newValue = random(dataMin, dataMax) print newValue print interpolators[row] interpolators[row].target(newValue) |