-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
conversion_function.pde
74 lines (59 loc) · 2.02 KB
/
conversion_function.pde
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
// Conversion Function
// The Coding Train / Daniel Shiffman
// Processing Intro Series
float measurement, fahrenheit, celsius;
float minNum = 32; //min measurement
float maxNum = 330; //max measurement
void setup() {
size(640, 360);
pixelDensity(2);
measurement = selectNum();
}
void mousePressed() {
measurement = selectNum();
}
void draw() {
background(255);
//Position of lines
float x1 = width/6;
float x2 = width - width/6;
float fLine = height/4; //top line (fahrenheit)
float cLine = height - height/4; //bottom line (celsius)
strokeWeight(80);
/*blendMode() blends the pixels in the display window according to a defined mode.
blendMode(BLEND) is the default. */
//Gray background lines
blendMode(BLEND);
stroke(230);
line(x1, fLine, x2, fLine);
line(x1, cLine, x2, cLine);
//Black lines (update with every new measurement)
stroke(0);
/*lerp() calculates a number between two numbers at a specific increment.
It is convenient for creating motion along a straight path.
Try un-commenting the alternative to see how the line length changes from
a gradual motion to instantaneous update.*/
fahrenheit = lerp(fahrenheit, measurement, 0.05);
celsius = lerp(celsius, fahrenheitToCelsius(measurement), 0.05);
//----ALTERNATIVE-----
//fahrenheit = measurement;
//celsius = fahrenheitToCelsius(measurement);
line(x1, fLine, x1 + fahrenheit, fLine);
line(x1, cLine, x1 + celsius, cLine);
textSize(14);
fill(255);
/*blendMode(DIFFERENCE) subtracts colors from the underlying image/shape so
when the black line is over the text, the text is white. Otherwise,
the text is black (legibility). */
blendMode(DIFFERENCE);
textAlign(CENTER, CENTER);
//Use the round() function to round to the nearest degree.
text(round(fahrenheitToCelsius(measurement)) + "° CELSIUS", width/2, cLine);
text(round(measurement) + "° FAHRENHEIT", width/2, fLine);
}
float selectNum() {
return (random(minNum, maxNum));
}
float fahrenheitToCelsius(float degF) {
return (degF - 31) * 5/9 ;
}