さて、この章の最後のプロジェクト:
Chapter3で作成したインテグレータクラスを用いて、タブ選択の移行にゆっくりと遷移描画させる。setup()関数内で、Integratorクラスのintegの生成を列ごとに行い、interpolators配列に収める。
1 2 3 4 5 |
for row in range(rowCount): initialValue = data.getFloat(row, 0) integ = Integrator.Integrator(initialValue) interpolators.append(integ) interpolators[row].attraction = 0.1 |
drawDataArea()関数内で、
1 2 3 4 5 6 7 8 9 |
for row in range(rowCount): if (data.isValid(row, col)): value = interpolators[row].value x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) vertex(x, y) vertex(plotX2, plotY2) vertex(plotX1, plotY2) endShape(CLOSE) |
遷移描画を実行させる。
step_17_interpolate_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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
import FloatTable import Integrator width = 720 height = 405 dataMin = 0 dataMax = 0 #Corners of the plotted time series plotX1 = 120 plotX2 = width - 80 labelX = 50 plotY1 = 60 plotY2 = height - 70 labelY = height - 25 currentColumn = 0 yearInterval = 10 volumeInterval = 10 volumeIntervalMinor = 5 interpolators = [] years =[] value =[] def setup(): global interpolators global integ global data global dataMin global dataMax global value global years global years_str global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 global labelX global labelY global currentColumn global columnCount global rowCount global yearInterval global volumeIntervalMinor global volumeInterval size(width, height) data = FloatTable.FloatTable("milk-tea-coffee.tsv") rowCount = data.getRowCount() columnCount = data.getColumnCount() years_str = data.getRowNames() for i in range(len(years_str)): years.append(int(years_str[i])) print "years=:" print years yearMin = years[0] yearMax = years[len(years) - 1] dataMin = 0 #dataMax = data.getTableMax() dataMax = ceil(data.getTableMax() / volumeInterval) * volumeInterval #dataMax = 100 for row in range(rowCount): initialValue = data.getFloat(row, 0) #print initialValue integ = Integrator.Integrator(initialValue) interpolators.append(integ) interpolators[row].attraction = 0.1 #print interpolators plotFont = createFont("SansSerif", 20) textFont(plotFont) smooth() def draw(): global plotX1 global plotX2 global plotY1 global plotY2 global rowCount global currentColumn background(224) #Show the plot area as a white box fill(255) rectMode(CORNERS) noStroke() rect(plotX1, plotY1, plotX2, plotY2) drawTitleTabs() drawAxisLabels() for row in range(rowCount): interpolators[row].update() drawYearLabels() drawVolumeLabels() noStroke() fill('#5679C1') drawDataArea(currentColumn) def drawTitleTabs(): global tabLeft global tabRight global title global tabTop global tabBottom global tabPad global currentColumn global columnCount global rowCount global plotX1 global plotX2 global plotY1 global plotY2 global labelX global labelY tabLeft = [] tabRight = [] tabPad = 10 tabImageNormal = [] tabImageHighlight = [] rectMode(CORNERS) noStroke() textSize(20) textAlign(LEFT) for col in range(0, int(columnCount)): title = data.getColumnName(col) tabImageNormal.append(loadImage(title + "-unselected.png")) tabImageHighlight.append(loadImage(title + "-selected.png")) runningX = plotX1 tabBottom = plotY1 tabTop = int(plotY1 - tabImageNormal[0].height) for col in range(columnCount): title = data.getColumnName(col) tabLeft.append(runningX) titleWidth = int(tabImageNormal[col].width) tabRight.append(int(tabLeft[col] + tabPad + titleWidth + tabPad)) if (col == currentColumn): tabImage = tabImageHighlight[col] else: tabImage = tabImageNormal[col] image(tabImage, tabLeft[col], tabTop) runningX = tabRight[col]; def mousePressed(): global columnCount global tabLeft global tabRight global tabTop global tabBottom if (mouseY > tabTop and mouseY < tabBottom): for col in range(columnCount): if (mouseX > tabLeft[col] and mouseX < tabRight[col]): setCurrent(col) def setCurrent(col): global currentColumn global interpolators global rowCount currentColumn = col for row in range(rowCount): interpolators[row].target(data.getFloat(row, col)) def drawAxisLabels(): global plotX1 global plotX2 global plotY1 global plotY2 global labelX global labelY fill(0) textSize(13) textLeading(15) textAlign(CENTER, CENTER) text("Gallons\nconsumed\nper capita", labelX, (plotY1+plotY2)/2) textAlign(CENTER) text("Year", (plotX1+plotX2)/2, labelY) def drawYearLabels(): global years global years_str global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 global rowCount global data global yearInterval fill(0) textSize(10) textAlign(CENTER, TOP) stroke(224) strokeWeight(1) rowCount = data.getRowCount() for row in range(rowCount): if (years[row] % yearInterval == 0): x = map(years[row], yearMin, yearMax, plotX1, plotX2) text(years[row], x, plotY2 + textAscent()+ 10) #line(x, plotY1, x, plotY2) def drawVolumeLabels(): global dataMin global dataMax global plotX1 global plotX2 global plotY1 global plotY2 global volumeInterval global volumeIntervalMinor volumeInterval = 10 volumeIntervalMinor = 5 fill(0) textSize(10) textAlign(RIGHT) stroke(128) strokeWeight(1) for v in range(dataMin, dataMax+1, volumeIntervalMinor): if (v % volumeIntervalMinor == 0): y = map(v, dataMin, dataMax, plotY2, plotY1) if (v % volumeInterval == 0): if (v == dataMin): textAlign(RIGHT) elif (v == dataMax): textAlign(RIGHT, TOP) else: textAlign(RIGHT, CENTER) text(floor(v), plotX1 - 10, y) line(plotX1 - 4, y, plotX1, y) else: line(plotX1 - 2, y, plotX1, y) def drawDataArea(col): global rowCount global data global dataMin global dataMax global value global years global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 global interpolators noStroke() beginShape() for row in range(rowCount): if (data.isValid(row, col)): value = interpolators[row].value x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) vertex(x, y) vertex(plotX2, plotY2) vertex(plotX1, plotY2) endShape(CLOSE) |
Integrator.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 |
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 * (float(self.target_value) - float(self.value)) self.accel = self.force / self.mass self.vel = (self.vel + self.accel) * self.damping self.value = float(self.value) + float(self.vel) self.force = 0 def target(self, t): self.targeting = True self.target_value = t def noTarget(self): self.targeting = False |